Class: Alula::Dcp::ListObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/alula/dcp/list_object.rb

Overview

Used to create a new list of DCP objects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list_type) ⇒ ListObject

Returns a new instance of ListObject.



12
13
14
15
# File 'lib/alula/dcp/list_object.rb', line 12

def initialize(list_type)
  @type = list_type
  @items = []
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



9
10
11
# File 'lib/alula/dcp/list_object.rb', line 9

def items
  @items
end

#rate_limitObject

Returns the value of attribute rate_limit.



10
11
12
# File 'lib/alula/dcp/list_object.rb', line 10

def rate_limit
  @rate_limit
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/alula/dcp/list_object.rb', line 9

def type
  @type
end

Class Method Details

.construct_from_response(device_id, klass, response, _opts) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/alula/dcp/list_object.rb', line 41

def self.construct_from_response(device_id, klass, response, _opts)
  # TODO: add pagination support if/when DCP supports it
  list = ListObject.new(klass)
  response.data.each do |sub_klass_string, items|
    # if it's a real class, create an instance of it
    # otherwise, log a warning and skip it
    ruby_sub_klass_string = Util.upper_camelcase_from_camelcase(sub_klass_string)
    sub_klass_name = "#{klass}::#{ruby_sub_klass_string}"
    sub_klass = klass.const_defined?(ruby_sub_klass_string, false) ? Object.const_get(sub_klass_name) : nil
    if sub_klass.nil?
      Alula.logger.warn("No class found for #{sub_klass_name}")
      next
    end

    sub_klass_list = []
    items.each do |item|
      sub_klass_list << sub_klass.new(device_id, item['index'], item)
    end
    list << sub_klass_list
    # define getter method for each sub_klass
    ruby_method = Util.underscore(sub_klass_string)
    list.define_singleton_method(ruby_method) do
      sub_klass_list
    end
  end
  list.rate_limit = response.rate_limit

  list
end

Instance Method Details

#<<(item) ⇒ Object



37
38
39
# File 'lib/alula/dcp/list_object.rb', line 37

def <<(item)
  @items << item
end

#[](index) ⇒ Object



25
26
27
# File 'lib/alula/dcp/list_object.rb', line 25

def[](index)
  @items[index]
end

#each(&block) ⇒ Object



21
22
23
# File 'lib/alula/dcp/list_object.rb', line 21

def each(&block)
  items.each { |item| block.call(item) }
end

#firstObject



29
30
31
# File 'lib/alula/dcp/list_object.rb', line 29

def first
  @items.first
end

#lastObject



33
34
35
# File 'lib/alula/dcp/list_object.rb', line 33

def last
  @items.last
end

#lengthObject



17
18
19
# File 'lib/alula/dcp/list_object.rb', line 17

def length
  items.length
end