Module: Otis::Object::ClassExtension
- Defined in:
- lib/otis/otis_object.rb
Instance Method Summary collapse
- #attributes(*args) ⇒ Object
-
#collection(opts = {}) ⇒ Object
This method allows a dynamic generation a list of node in a parent one.
-
#tag_attributes(*args) ⇒ Object
When Savon receives a response with attributes in its tags, it creates keys that start with ‘@’ sign, which breaks in the Virtus object transformation.
Instance Method Details
#attributes(*args) ⇒ Object
18 19 20 21 22 |
# File 'lib/otis/otis_object.rb', line 18 def attributes(*args) args.each do |m| class_eval %(attr_accessor :#{m} ) end end |
#collection(opts = {}) ⇒ Object
This method allows a dynamic generation a list of node in a parent one. It differs slightly from Virtus mapping in a way that it guarantess that a return of a collection is always an array and never a nil object
29 30 31 32 33 |
# File 'lib/otis/otis_object.rb', line 29 def collection(opts ={}) collection = opts[:as].to_s klass = opts[:of] class_eval %( def #{collection}; @#{collection} ||= [@response[:#{collection}]].compact.flatten.map{|c| #{klass}.new(c)}; end ) end |
#tag_attributes(*args) ⇒ Object
When Savon receives a response with attributes in its tags, it creates keys that start with ‘@’ sign, which breaks in the Virtus object transformation. This method allows the mapping of an attribute that would be originally ignored by Virtus. See the example below:
<response duration=“123”>
<tag>Foo</tag>
</response>
In order to create the right mapper, the following needs to be done:
class YourObject < Otis::Model
tag_attribute :duration
attribute :tag
end
yourObject.tag # => “Foo” yourObject.duration #=> 123
57 58 59 60 61 62 63 64 65 |
# File 'lib/otis/otis_object.rb', line 57 def tag_attributes(*args) args.each do |m| class_eval %( def #{m} @response[:@#{m}] end ) end end |