Class: ITuner::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/ituner/model.rb

Direct Known Subclasses

ITunes, Playlist, Source, Track

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_object) ⇒ Model

Returns a new instance of Model.



5
6
7
# File 'lib/ituner/model.rb', line 5

def initialize(app_object)
  @app_object = app_object
end

Instance Attribute Details

#app_objectObject

Returns the value of attribute app_object.



9
10
11
# File 'lib/ituner/model.rb', line 9

def app_object
  @app_object
end

Class Method Details

.action(name, app_name = name) ⇒ Object



48
49
50
51
52
# File 'lib/ituner/model.rb', line 48

def action(name, app_name = name)
  define_method(name) do |*args|
    app_object.send(app_name, *args)
  end
end

.belongs_to(name, app_name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/ituner/model.rb', line 32

def belongs_to(name, app_name)
  define_method(name) do
    parent_class = ITuner.const_get(name.to_s.capitalize)
    app_parent = app_object.send(app_name).get
    parent_class.new(app_parent) if app_parent
  end
end

.has_many(name, app_name = name, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/ituner/model.rb', line 40

def has_many(name, app_name = name, &block)
  item_type = name.to_s.sub(/s$/,'').to_sym
  item_class = ITuner.const_get(item_type.to_s.capitalize)
  define_method(name) do
    Collection.new(app_object.send(app_name), item_type, item_class, &block)
  end
end

.property(name, app_name = name) ⇒ Object



26
27
28
29
30
# File 'lib/ituner/model.rb', line 26

def property(name, app_name = name)
  define_method(name) do
    app_object.send(app_name).get
  end
end

Instance Method Details

#==(other) ⇒ Object



11
12
13
# File 'lib/ituner/model.rb', line 11

def ==(other)
  other && (app_object == other.app_object)
end

#inspectObject



15
16
17
18
19
20
21
22
# File 'lib/ituner/model.rb', line 15

def inspect
  s = "{" + self.class.name
  if respond_to?(:name)
    s += " #{name.inspect}"
  end
  s += "}"
  s
end