Class: FridgeApi::Model
- Inherits:
-
Object
- Object
- FridgeApi::Model
- Defined in:
- lib/fridge_api/model.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#[](method) ⇒ Object
Allow fields to be retrieved via Hash notation.
- #attrs ⇒ Object
- #commit ⇒ Object
-
#initialize(resource) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
-
#method_missing(method, *args) ⇒ Object
Retrieve field value.
Constructor Details
#initialize(resource) ⇒ Model
Returns a new instance of Model.
10 11 12 13 |
# File 'lib/fridge_api/model.rb', line 10 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
45 46 47 |
# File 'lib/fridge_api/model.rb', line 45 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
6 7 8 |
# File 'lib/fridge_api/model.rb', line 6 def self.new_from_part(part, data) # TODO end |
Instance Method Details
#[](method) ⇒ Object
Allow fields to be retrieved via Hash notation
38 39 40 41 42 |
# File 'lib/fridge_api/model.rb', line 38 def [](method) send(method.to_sym) rescue NoMethodError nil end |
#attrs ⇒ Object
49 50 51 |
# File 'lib/fridge_api/model.rb', line 49 def attrs @attrs end |
#commit ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fridge_api/model.rb', line 19 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 |
#inspect ⇒ Object
15 16 17 |
# File 'lib/fridge_api/model.rb', line 15 def inspect p @attrs end |