Created
March 5, 2010 12:55
-
-
Save reactormonk/322689 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'erector' | |
| module Rango | |
| module ErectorMixin | |
| # Erects a html using the given class | |
| # | |
| # @param klass [Erector::Widget] the class to render with | |
| # @param options [Hash] | |
| # :exclude Array<Symbols> | |
| # ivars not to pass via serializing | |
| # :explicit Hash<Symbol, Object> | |
| # explicitly pass context to the Erector::Widget class | |
| def erect(klass, hash = {}, &block) | |
| unless context = hash[:explicit] | |
| context.replace(serialize_ivars_to_hash(hash[:exclude])) | |
| end | |
| klass.new(context).to_s | |
| end | |
| private | |
| def serialize_ivars_to_hash(*to_exclude) | |
| hash = {} | |
| instance_variables.map do |name| | |
| key = name.to_s[1..-1].to_sym | |
| next if to_exclude.include?(key) | |
| hash[key] = instance_variable_get(name) | |
| end | |
| hash.compact! | |
| end | |
| end | |
| end | |
| class Erector::Widget | |
| include Rango::Helpers | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment