Class: OandaAPI::ResourceCollection
- Inherits:
-
Object
- Object
- OandaAPI::ResourceCollection
- Includes:
- Enumerable
- Defined in:
- lib/oanda_api/resource_collection.rb
Overview
A collection of a specific resource. Returned by API requests that return collections. See the Oanda Development Guide for documentation about resource attributes expected for specific requests.
Instance Attribute Summary collapse
- #location ⇒ String readonly
Instance Method Summary collapse
- #each {|OandaAPI::ResourceBase| ... } ⇒ Enumerator
-
#initialize(attributes, resource_descriptor) ⇒ ResourceCollection
constructor
A new instance of ResourceCollection.
-
#method_missing(sym, *args) ⇒ Object
Responds to collection-scoped accessor methods that are specific to the type of resource collection.
-
#respond_to?(sym) ⇒ Boolean
Returns +true+ for concrete, delegated and dynamic methods.
Constructor Details
#initialize(attributes, resource_descriptor) ⇒ ResourceCollection
29 30 31 32 33 34 35 36 |
# File 'lib/oanda_api/resource_collection.rb', line 29 def initialize(attributes, resource_descriptor) attributes = {} if attributes.nil? || attributes.respond_to?(:empty) && attributes.empty? fail ArgumentError, "Expecting a Hash" unless attributes.respond_to? :each_pair @attributes = Utils.rubyize_keys attributes @collection = @attributes.delete(resource_descriptor.collection_name) || [] @collection.map! { |resource| resource_descriptor.resource_klass.new resource } @location = attributes.location if attributes.respond_to? :location end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
Responds to collection-scoped accessor methods that are specific to the type of resource collection. For example, a +Candle+ collection includes the collection-scoped methods +granularity+ and +instrument+.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/oanda_api/resource_collection.rb', line 52 def method_missing(sym, *args) case when @attributes.keys.include?(sym) @attributes[sym] when @collection.respond_to?(sym) @collection.send sym else super end end |
Instance Attribute Details
#location ⇒ String (readonly)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/oanda_api/resource_collection.rb', line 20 class ResourceCollection include Enumerable attr_reader :location # @param [Hash] attributes collection of resource attributes # # @param [OandaAPI::Client::ResourceDescriptor] resource_descriptor metadata # about the resource collection and its elements. def initialize(attributes, resource_descriptor) attributes = {} if attributes.nil? || attributes.respond_to?(:empty) && attributes.empty? fail ArgumentError, "Expecting a Hash" unless attributes.respond_to? :each_pair @attributes = Utils.rubyize_keys attributes @collection = @attributes.delete(resource_descriptor.collection_name) || [] @collection.map! { |resource| resource_descriptor.resource_klass.new resource } @location = attributes.location if attributes.respond_to? :location end # @yield [OandaAPI::ResourceBase] # @return [Enumerator] def each if block_given? @collection.each { |el| yield el } else @collection.each end end # @private # Responds to collection-scoped accessor methods that are specific to the # type of resource collection. For example, a +Candle+ collection includes # the collection-scoped methods +granularity+ and +instrument+. def method_missing(sym, *args) case when @attributes.keys.include?(sym) @attributes[sym] when @collection.respond_to?(sym) @collection.send sym else super end end # Returns +true+ for concrete, delegated and dynamic methods. # @return [Boolean] def respond_to?(sym) case when @attributes.keys.include?(sym) true when @collection.respond_to?(sym) true else super end end end |
Instance Method Details
#each {|OandaAPI::ResourceBase| ... } ⇒ Enumerator
40 41 42 43 44 45 46 |
# File 'lib/oanda_api/resource_collection.rb', line 40 def each if block_given? @collection.each { |el| yield el } else @collection.each end end |
#respond_to?(sym) ⇒ Boolean
Returns +true+ for concrete, delegated and dynamic methods.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/oanda_api/resource_collection.rb', line 66 def respond_to?(sym) case when @attributes.keys.include?(sym) true when @collection.respond_to?(sym) true else super end end |