Class: AttributeSerializer::Context
- Inherits:
-
Object
- Object
- AttributeSerializer::Context
- Defined in:
- lib/attribute_serializer.rb
Instance Method Summary collapse
- #generate(object) ⇒ Object
-
#initialize(klass, attribs, &delegate_methods) ⇒ Context
constructor
A new instance of Context.
Constructor Details
#initialize(klass, attribs, &delegate_methods) ⇒ Context
Returns a new instance of Context.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/attribute_serializer.rb', line 11 def initialize(klass, attribs, &delegate_methods) @attribs = attribs @delegate_class = DelegateClass(klass) @delegate_class.class_eval do def delegatee; __getobj__ end def id; formatee.id end # DelegateClass was written before object_id became the new world order alias_method :formatee, :delegatee # this was a stupid name end @delegate_class.class_eval(&delegate_methods) if delegate_methods end |
Instance Method Details
#generate(object) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/attribute_serializer.rb', line 22 def generate(object) @delegate = @delegate_class.new(object) ActiveSupport::OrderedHash.new.tap do |h| @attribs.each do |attrib| h[attrib.to_s] = @delegate.send(attrib) end end end |