class WargamingApi::Node
Attributes
node[R]
Public Class Methods
new(node)
click to toggle source
# 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
Public Instance Methods
define_array(array)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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