class DACPClient::Model

Public Class Methods

build_dmap(params = {}) click to toggle source
# File lib/dacpclient/model.rb, line 78
def build_dmap(params = {})
  new(params).to_dmap
end
dmap_attribute(method, key) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/dacpclient/model.rb, line 70
def dmap_tag(tag = nil)
  if tag
    @dmap_tag = tag
  else
    @dmap_tag
  end
end
new(params = {}) click to toggle source
# 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

Public Instance Methods

inspect() click to toggle source
# 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
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# 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
respond_to?(method) click to toggle source
# File lib/dacpclient/model.rb, line 41
def respond_to?(method)
  dmap_attributes.key?(method)
end
to_dmap() click to toggle source
# 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_s() click to toggle source
# 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

Private Instance Methods

deserialize(data) click to toggle source
# File lib/dacpclient/model.rb, line 85
def deserialize(data)
  warn 'Invalid tag' if data.type.tag.to_sym != dmap_tag
  dmap_attributes.values.each do |value|
    value.value = get_value(data, value) if data.respond_to? value.tag
  end
  self
end
dmap_attributes() click to toggle source
# File lib/dacpclient/model.rb, line 104
def dmap_attributes
  @dmap_attributes ||= initialize_attributes
end
dmap_tag() click to toggle source
# File lib/dacpclient/model.rb, line 117
def dmap_tag
  self.class.instance_variable_get(:@dmap_tag)
end
get_value(data, value) click to toggle source
# File lib/dacpclient/model.rb, line 93
def get_value(data, value)
  item_class = value.item_class
  if item_class
    data.send(value.tag).to_a.map do |item|
      item_class.new(item) if item_class.dmap_tag == item.type.tag.to_sym
    end.compact
  else
    data.send(value.tag)
  end
end
initialize_attributes() click to toggle source
# File lib/dacpclient/model.rb, line 108
def initialize_attributes
  class_attributes = self.class.instance_variable_get(:@dmap_attributes)
  attributes = {}
  class_attributes.map do |key, value|
    attributes[key] = DMAPAttribute.new(*value)
  end
  attributes
end