Class: LastFm::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/last_fm/models/base.rb

Direct Known Subclasses

Album, Artist, Chart, Event, Tag, Track, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Base

Returns a new instance of Base.



31
32
33
34
# File 'lib/last_fm/models/base.rb', line 31

def initialize(name)
  @name       = name
  @attributes = Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



59
60
61
# File 'lib/last_fm/models/base.rb', line 59

def method_missing(sym, *args, &block)
  return @attributes[sym.to_s] if @attributes.has_key?(sym.to_s)
end

Class Method Details

.create_from_hash(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/last_fm/models/base.rb', line 9

def create_from_hash(data)
  if data.class == Array
    models = []
    data.each do |d|
      if d.class == Hash
        name         = (d['name'] || d['title']) || d['id']
        n            = self.new(name) rescue self.new(nil, name)
        n.attributes = d
        models.push(n)
      end
    end 
    return models 
  else
    name         = (data['name'] || data['title']) || data['id']
    n            = self.new(name) 
    n.attributes = data
    return n          
  end
end

.search(name) ⇒ Object



5
6
7
# File 'lib/last_fm/models/base.rb', line 5

def search(name)
  self.new(name)
end

Instance Method Details

#attributes=(h) ⇒ Object



44
45
46
# File 'lib/last_fm/models/base.rb', line 44

def attributes=(h)
  @attributes = h
end

#get_xml_attributes(elements) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/last_fm/models/base.rb', line 48

def get_xml_attributes(elements)
  h = Hash.new
  elements.each { |e| 
    # do somethings about nested and cdata !
    name    = e.name == "image" ? 'image_'+e.attributes['size'] : e.name
    h[name] = e.text
  }
  return h
end

#methodsObject



63
64
65
# File 'lib/last_fm/models/base.rb', line 63

def methods
  @attributes.keys
end

#nameObject



36
37
38
# File 'lib/last_fm/models/base.rb', line 36

def name
  @name
end

#name=(n) ⇒ Object



40
41
42
# File 'lib/last_fm/models/base.rb', line 40

def name=(n)
  @name = n
end