Module: ROXML::AttributeInitializable

Included in:
ShipMe::FedEx::RequestElement
Defined in:
lib/roxml/attribute_initializable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
# File 'lib/roxml/attribute_initializable.rb', line 2

def self.included base
  base.class_eval do
    alias_method_chain :initialize, :attributes unless instance_methods.include?("initialize_without_attributes")
  end
end

Instance Method Details

#initialize_with_attributes(attributes = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/roxml/attribute_initializable.rb', line 8

def initialize_with_attributes(attributes={})
  initialize_without_attributes()
  return unless attributes.respond_to?(:each)
  attributes.each do |attr, value|
    roxml_attr = self.class.roxml_attrs.find { |r| r.attr_name == attr.to_s.chomp('?') }
    if roxml_attr && sought_type = roxml_attr.sought_type
      value = case sought_type
      when :text then value
      else
        unless roxml_attr.array?
          sought_type.new(value)
        else
          value.map { |v| sought_type.new(v) }
        end if value
      end
      self.send(roxml_attr.setter, value)
    elsif self.respond_to?(:"#{attr}=")
      self.send(:"#{attr}=", value)
    end
  end
end