Class: Alula::DcpResource

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/dcp_resource.rb

Overview

Parent class for all DCP objects

Direct Known Subclasses

Alula::Dcp::BaseResource

Defined Under Namespace

Classes: ModelErrors

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_id, index = nil, attributes = {}) ⇒ DcpResource

Returns a new instance of DcpResource.



12
13
14
15
16
17
# File 'lib/alula/dcp_resource.rb', line 12

def initialize(device_id, index = nil, attributes = {})
  @raw_data = {}
  @dirty_attributes = {}
  @errors = ModelErrors.new(self.class)
  construct_from(device_id, index, attributes)
end

Instance Attribute Details

#device_idObject

Returns the value of attribute device_id.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def device_id
  @device_id
end

#dirty_attributesObject

Returns the value of attribute dirty_attributes.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def dirty_attributes
  @dirty_attributes
end

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def errors
  @errors
end

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def index
  @index
end

#metaObject

Returns the value of attribute meta.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def meta
  @meta
end

#rate_limitObject

Returns the value of attribute rate_limit.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def rate_limit
  @rate_limit
end

#raw_dataObject

Returns the value of attribute raw_data.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def raw_data
  @raw_data
end

#stagedObject

Returns the value of attribute staged.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def staged
  @staged
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/alula/dcp_resource.rb', line 6

def value
  @value
end

Class Method Details

.build(device_id) ⇒ Object

Construct a new resource, ready to receive attributes, with empty values for all attrs. Useful for making New resources



27
28
29
30
31
# File 'lib/alula/dcp_resource.rb', line 27

def self.build(device_id)
  fields = get_fields.keys.map { |k| Util.camelize(k) }
  empty_shell = fields.each_with_object({}) { |f, obj| obj[f] = nil }
  new(device_id, nil, empty_shell)
end

.class_nameObject



8
9
10
# File 'lib/alula/dcp_resource.rb', line 8

def self.class_name
  name.split('::')[-1]
end

Instance Method Details

#annotate_errors(model_errors) ⇒ Object



115
116
117
118
119
120
# File 'lib/alula/dcp_resource.rb', line 115

def annotate_errors(model_errors)
  @errors = ModelErrors.new(self.class)
  model_errors.each_pair do |field_name, error|
    errors.add(field_name, error)
  end
end

#apply_attributes(attributes) ⇒ Object

Take a hash of attributes and apply them to the model



48
49
50
51
52
53
54
# File 'lib/alula/dcp_resource.rb', line 48

def apply_attributes(attributes)
  attributes.each do |key, value|
    next unless fields.include?(key)

    @dirty_attributes[key] = value
  end
end

#as_jsonObject

Fetch known attributes out of the object, collected into a Hash in camelCase format Intended for eventually making its way back up to the API



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/alula/dcp_resource.rb', line 79

def as_json
  field_names.each_with_object({}) do |ruby_key, obj|
    key = Util.camelize(ruby_key)
    val = find_value(ruby_key)

    next if val.nil?

    obj[key] = if val.is_a? Array
                 val.map { |v| parse_value(v, ruby_key) }
               elsif val.is_a? Alula::Dcp::ObjectField
                 val.as_json
               else
                 parse_value(val, ruby_key)
               end
  end
end

#as_patchable_jsonObject

Reduce as_json to a set that can be updated



107
108
109
110
111
112
113
# File 'lib/alula/dcp_resource.rb', line 107

def as_patchable_json
  values = as_json.each_pair.each_with_object({}) do |(key, val), collector|
    collector[key] = val
  end

  values.reject { |k, _v| %w[index version].include? k } # blacklist index and version
end

#cloneObject



19
20
21
# File 'lib/alula/dcp_resource.rb', line 19

def clone
  self.class.new(@device_id, nil, @raw_data)
end

#construct_from(device_id, index, json_object) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/alula/dcp_resource.rb', line 33

def construct_from(device_id, index, json_object)
  raise ArgumentError, 'device_id is required' if device_id.empty?

  @raw_data = json_object.dup
  assign_meta_value_staged(json_object)
  self.index = index
  self.device_id = device_id

  @errors = ModelErrors.new(self.class)

  self
end

#errors?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/alula/dcp_resource.rb', line 60

def errors?
  @errors.any?
end

#filter_builderObject Also known as: fb

Return an instance of QueryEngine annotated with the correct model attributes



124
125
126
# File 'lib/alula/dcp_resource.rb', line 124

def filter_builder
  Alula::FilterBuilder.new(self.class)
end

#find_value(key) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/alula/dcp_resource.rb', line 96

def find_value(key)
  # prioritize dirty attributes, then staged, then value
  # ideally we should use only dirty attributes, done by executing .apply_attributes
  return @dirty_attributes[key] if @dirty_attributes.include?(key)
  return @staged.send(key) if @staged.respond_to?(key)

  @value.send(key) if @value.respond_to?(key)
end

#model_nameObject



129
130
131
# File 'lib/alula/dcp_resource.rb', line 129

def model_name
  self.class
end

#reconstruct_from(device_id, index, json_object) ⇒ Object



56
57
58
# File 'lib/alula/dcp_resource.rb', line 56

def reconstruct_from(device_id, index, json_object)
  construct_from(device_id, index, json_object)
end

#refreshObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/alula/dcp_resource.rb', line 64

def refresh
  response = Alula::Client.request(:get, resource_url, {}, {})
  if response.ok?
    model = construct_from(device_id, index, response.data)
    model.rate_limit = response.rate_limit
    model
  else
    error_class = AlulaError.for_response(response)
    raise error_class
  end
end