Class: SportsDataApi::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/sports_data_api/stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Stats

Returns a new instance of Stats.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/sports_data_api/stats.rb', line 3

def initialize(xml)
  if xml.is_a? Nokogiri::XML::Element
    stat_ivar = self.instance_variable_set("@#{xml.name}", {})
    self.class.class_eval { attr_reader :"#{xml.name}" }

    xml.attributes.each { |attr_name, attr_value| stat_ivar[attr_name.to_sym] = attr_value.value }
    xml.children.each do |child_stat|
      if child_stat.is_a? Nokogiri::XML::Element
        child_stat.attributes.each { |attr_name, attr_value| stat_ivar["#{child_stat.name}_#{attr_name}".to_sym] = attr_value.value }
      end
    end
  end
end