Class: FridgeApi::Model

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Model

Returns a new instance of Model.



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

def initialize(resource)
  @raw = resource.to_h
  @attrs = parse
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Retrieve field value



43
44
45
# File 'lib/fridge_api/model.rb', line 43

def method_missing(method, *args)
  @attrs.has_key?(method.to_sym) ? @attrs[method.to_sym] : super
end

Class Method Details

.new_from_part(part, data) ⇒ Object



4
5
6
# File 'lib/fridge_api/model.rb', line 4

def self.new_from_part(part, data)
  # TODO
end

Instance Method Details

#[](method) ⇒ Object

Allow fields to be retrieved via Hash notation



36
37
38
39
40
# File 'lib/fridge_api/model.rb', line 36

def [](method)
  send(method.to_sym)
rescue NoMethodError
  nil
end

#attrsObject



47
48
49
# File 'lib/fridge_api/model.rb', line 47

def attrs
  @attrs
end

#commitObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fridge_api/model.rb', line 17

def commit
  @raw.each do |key, val|
    if val.kind_of? Array
      if is_part? val
        val.each do |i, part|
          part_name = part_name(part)
          unless part_value(part) == @attrs[part_name]
            @raw[key][i][:value] = @attrs[part_name]
          end
        end
        return
      end
    end

    @raw[key] = @attrs[key] unless val == @attrs[key]
  end
end

#inspectObject



13
14
15
# File 'lib/fridge_api/model.rb', line 13

def inspect
  p @attrs
end