Class: WargamingApi::Node
- Inherits:
-
Object
- Object
- WargamingApi::Node
- Includes:
- Concern::Attributable
- Defined in:
- lib/wargaming_api/node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Instance Method Summary collapse
- #define_array(array) ⇒ Object
-
#initialize(node) ⇒ Node
constructor
A new instance of Node.
- #initialize_as_array(array) ⇒ Object
- #initialize_as_hash(hash) ⇒ Object
- #initialize_attributes(hash) ⇒ Object
Methods included from Concern::Attributable
Constructor Details
#initialize(node) ⇒ Node
Returns a new instance of Node.
24 25 26 27 28 29 |
# File 'lib/wargaming_api/node.rb', line 24 def initialize(node) case node when Array then initialize_as_array(node) when Hash then initialize_as_hash(node) end end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
22 23 24 |
# File 'lib/wargaming_api/node.rb', line 22 def node @node end |
Instance Method Details
#define_array(array) ⇒ Object
50 51 52 |
# File 'lib/wargaming_api/node.rb', line 50 def define_array(array) array.map { |hash| hash ? WargamingApi::Node.new(hash) : nil } end |
#initialize_as_array(array) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/wargaming_api/node.rb', line 31 def initialize_as_array(array) @node = define_array(array) self.singleton_class.include Enumerable self.define_singleton_method(:each) do |&blk| @node.each(&blk) end self.define_singleton_method(:[]) do |value| @node[value] end end |
#initialize_as_hash(hash) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/wargaming_api/node.rb', line 42 def initialize_as_hash(hash) if hash.keys.any? { |key| key =~ /[A-Z\d]+/ && (hash[key].is_a?(Hash) || hash[key].is_a?(Array) || hash[key].is_a?(NilClass)) } initialize_as_array(hash.values) else initialize_attributes(hash) end end |
#initialize_attributes(hash) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/wargaming_api/node.rb', line 54 def initialize_attributes(hash) @node = hash @node.keys.each do |attribute| define_singleton_method(attribute) do case @node[attribute] when Array then WargamingApi::Node.new(@node[attribute]) when Hash then WargamingApi::Node.new(@node[attribute]) else apply_type(attribute, @node[attribute]) end end end end |