module Otis::Object::ClassExtension
Public Instance Methods
attributes(*args)
click to toggle source
# File lib/otis/otis_object.rb, line 18 def attributes(*args) args.each do |m| class_eval %(attr_accessor :#{m} ) end end
collection(opts ={})
click to toggle source
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
# 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)
click to toggle source
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
# 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
Private Instance Methods
camelize(string)
click to toggle source
# File lib/otis/otis_object.rb, line 68 def camelize(string) return string if string !~ /_/ && self =~ /[A-Z]+.*/ string.split('_').map{|e| e.capitalize}.join end