Class: LastFM::Struct
- Inherits:
-
Struct
- Object
- Struct
- LastFM::Struct
- Defined in:
- lib/lastfm/struct.rb
Overview
Prodives modifications to Ruby’s Struct class for use within the LastFM module space. Must be called ‘Struct’ to play nice with YARD’s @attr documentation.
Class Method Summary collapse
-
.from_xml(xml, initial_attributes = {}) ⇒ LastFM::Struct
Construct a LastFM::Struct object from XML, using each inheritor’s attr_from_node method to determine how to manipulate individual attributes.
-
.package ⇒ String
Get the Last.fm package name for a Ruby class.
Instance Method Summary collapse
-
#initialize(h = {}) ⇒ Struct
constructor
Override Struct’s initialize method to accept a hash of members instead.
Constructor Details
#initialize(h = {}) ⇒ Struct
Override Struct’s initialize method to accept a hash of members instead.
8 9 10 |
# File 'lib/lastfm/struct.rb', line 8 def initialize(h={}) members.each{|m| self[m] = h[m.to_sym]} end |
Class Method Details
.from_xml(xml, initial_attributes = {}) ⇒ LastFM::Struct
Construct a LastFM::Struct object from XML, using each inheritor’s attr_from_node method to determine how to manipulate individual attributes.
25 26 27 28 29 30 31 |
# File 'lib/lastfm/struct.rb', line 25 def self.from_xml(xml, initial_attributes={}) raise NotImplementedError unless self.method_defined?(:update_from_node) xml = xml.find_first(self.package) if xml.is_a?(LibXML::XML::Document) model = self.new(initial_attributes) xml.find('*').each{|child| model.update_from_node(child)} model end |
.package ⇒ String
Get the Last.fm package name for a Ruby class
15 16 17 |
# File 'lib/lastfm/struct.rb', line 15 def self.package self.to_s.downcase.split('::').last end |