Module: GoGoodreads::Attributes::ClassMethods
- Defined in:
- lib/go_goodreads/attributes.rb
Instance Method Summary collapse
- #attribute(name, opts = {}) ⇒ Object
- #Boolean(val) ⇒ Object
- #to_attributes!(xml, options = {}) ⇒ Object
Instance Method Details
#attribute(name, opts = {}) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/go_goodreads/attributes.rb', line 14 def attribute(name, opts = {}) = { :type => String, :map_from => name } setting = .merge(opts) setting[:using] ||= lambda {|xml, from| (xml > from.to_s).text } @attributes ||= {} @attributes[name] = setting attr name end |
#Boolean(val) ⇒ Object
41 42 43 |
# File 'lib/go_goodreads/attributes.rb', line 41 def Boolean(val) {"true" => true, "false" => false}[val] end |
#to_attributes!(xml, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/go_goodreads/attributes.rb', line 25 def to_attributes!(xml, = {}) attrs = {} @attributes.each do |attr, config| opts = config.merge([attr] || {}) map_from = opts[:map_from] callable = opts[:using] val = callable.call(xml, map_from) unless val.to_s.empty? attrs[attr] = _convert(val, opts[:type]) rescue val end end attrs end |