Class: AmazonProductAdvertisingApi::Operations::Base::Element
- Inherits:
-
Object
- Object
- AmazonProductAdvertisingApi::Operations::Base::Element
- Includes:
- Enumerable
- Defined in:
- lib/amazon_product_advertising_api/operations/base.rb
Overview
XML data that is returned by Amazon gets built into a tree of nodes, which are made up of instances of this class. They represent the ‘response element’ entity within the API docs.
As well as various having attributes it can also contain a collection and behave like an array.
I think I might have got a bit confused with all this, will come back to this shortly to check.
Instance Method Summary collapse
-
#<<(element) ⇒ Object
Add an item to the element’s internal collection.
-
#[](position) ⇒ Object
Return the value of the internal collection’s element at the given position.
-
#add_element(name, value = nil) ⇒ Object
Defines a new accessor on the element and if supplied assigns that attribute a value.
-
#each(&block) ⇒ Object
Iterate over the element’s internal collection.
-
#first ⇒ Object
Return the first element of the internal collection.
-
#initialize ⇒ Element
constructor
A new instance of Element.
- #method_missing(method, *args) ⇒ Object
-
#size ⇒ Object
Return the number of elements in the internal collection.
Constructor Details
#initialize ⇒ Element
Returns a new instance of Element.
141 142 143 |
# File 'lib/amazon_product_advertising_api/operations/base.rb', line 141 def initialize @contained_elements = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/amazon_product_advertising_api/operations/base.rb', line 194 def method_missing(method, *args) if AmazonProductAdvertisingApi::Operations::Base::RESPONSE_ELEMENTS.include?(method.to_sym) if AmazonProductAdvertisingApi::Operations::Base::CONTAINER_RESPONSE_ELEMENTS.include?(method.to_sym) self.class.new else nil end else super end end |
Instance Method Details
#<<(element) ⇒ Object
Add an item to the element’s internal collection.
168 169 170 |
# File 'lib/amazon_product_advertising_api/operations/base.rb', line 168 def << element @contained_elements << element end |
#[](position) ⇒ Object
Return the value of the internal collection’s element at the given position.
180 181 182 |
# File 'lib/amazon_product_advertising_api/operations/base.rb', line 180 def [] position @contained_elements[position] end |
#add_element(name, value = nil) ⇒ Object
Defines a new accessor on the element and if supplied assigns that attribute a value.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/amazon_product_advertising_api/operations/base.rb', line 146 def add_element(name, value = nil) name = name.underscore self.instance_eval %{ def self.#{name} @#{name} end def self.#{name}=(value) @#{name} ||= value end } if !value.nil? value = value.to_s if value.is_a?(Symbol) self.send("#{name}=", value) end # Return the element self.instance_eval("self.#{name}") end |
#each(&block) ⇒ Object
Iterate over the element’s internal collection.
173 174 175 176 177 |
# File 'lib/amazon_product_advertising_api/operations/base.rb', line 173 def each(&block) @contained_elements.each do |element| yield element end end |
#first ⇒ Object
Return the first element of the internal collection.
185 186 187 |
# File 'lib/amazon_product_advertising_api/operations/base.rb', line 185 def first @contained_elements.first end |
#size ⇒ Object
Return the number of elements in the internal collection.
190 191 192 |
# File 'lib/amazon_product_advertising_api/operations/base.rb', line 190 def size @contained_elements.size end |