Skip to content

Instantly share code, notes, and snippets.

@timothy1ee
Created April 19, 2011 03:38
Show Gist options
  • Save timothy1ee/926776 to your computer and use it in GitHub Desktop.
Save timothy1ee/926776 to your computer and use it in GitHub Desktop.
# Returns a hash based representation of any data object given ejs template block
# object_to_hash(@user) { attribute :full_name } => { ... }
# object_to_hash(@user, :source => "...") { attribute :full_name } => { ... }
def object_to_hash(object, options={}, &block)
return object unless is_record?(object) || object.respond_to?(:each)
object = { data_object(object) => data_name(object) } if object.is_a?(Hash) && object.keys.first.is_a?(Symbol)
engine_options = { :format => "hash", :root => (options[:root] || false) }
Rabl::Engine.new(options[:source], engine_options).render(@_scope, :object => object, &block)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment