Skip to content

Instantly share code, notes, and snippets.

@reactormonk
Created March 5, 2010 12:55
Show Gist options
  • Select an option

  • Save reactormonk/322689 to your computer and use it in GitHub Desktop.

Select an option

Save reactormonk/322689 to your computer and use it in GitHub Desktop.
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