Class: DACPClient::Model

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

Defined Under Namespace

Classes: DMAPAttribute

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Model

Returns a new instance of Model.



9
10
11
12
13
14
15
16
17
# File 'lib/dacpclient/model.rb', line 9

def initialize(params = {})
  if params.is_a? DMAPParser::TagContainer
    deserialize(params)
  elsif params
    params.each do |attr, value|
      public_send("#{attr}=", value)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dacpclient/model.rb', line 45

def method_missing(method, *args, &block)
  if method.to_s =~ /(.*)\=$/ &&
     dmap_attributes.key?(Regexp.last_match[1].to_sym)
    dmap_attributes[Regexp.last_match[1].to_sym].value = args.first
  elsif method.to_s =~ /(.*)\?$/ &&
        dmap_attributes.key?(Regexp.last_match[1].to_sym)
    dmap_attributes[Regexp.last_match[1].to_sym].value
  elsif dmap_attributes.key? method
    dmap_attributes[method].value
  else
    super
  end
end

Class Method Details

.build_dmap(params = {}) ⇒ Object



78
79
80
# File 'lib/dacpclient/model.rb', line 78

def build_dmap(params = {})
  new(params).to_dmap
end

.dmap_attribute(method, key) ⇒ Object



60
61
62
63
# File 'lib/dacpclient/model.rb', line 60

def dmap_attribute(method, key)
  @dmap_attributes ||= {}
  @dmap_attributes[method] = key
end

.dmap_container(method, key, item_class) ⇒ Object



65
66
67
68
# File 'lib/dacpclient/model.rb', line 65

def dmap_container(method, key, item_class)
  @dmap_attributes ||= {}
  @dmap_attributes[method] = [key, item_class]
end

.dmap_tag(tag = nil) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/dacpclient/model.rb', line 70

def dmap_tag(tag = nil)
  if tag
    @dmap_tag = tag
  else
    @dmap_tag
  end
end

Instance Method Details

#inspectObject



19
20
21
22
23
24
# File 'lib/dacpclient/model.rb', line 19

def inspect
  puts self.class.name
  dmap_attributes.each do |key, value|
    puts "  #{key}: #{value.value}"
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dacpclient/model.rb', line 41

def respond_to?(method)
  dmap_attributes.key?(method)
end

#to_dmapObject



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

def to_dmap
  attributes = dmap_attributes
  DMAPParser::Builder.send dmap_tag do
    attributes.values.each do |value|
      send(value.tag, value.value)
    end
  end.to_dmap
end

#to_sObject



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

def to_s
  "#<#{self.class.name} " +  dmap_attributes.map do |key, value|
    "#{key}: #{value.value}"
  end.join(', ') + '>'
end