Class: BettyResource::Model::Record
- Inherits:
-
Object
- Object
- BettyResource::Model::Record
- Includes:
- DirtyAttributes::InstanceMethods, MethodMap
- Defined in:
- lib/betty_resource/model/record.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #_class ⇒ Object
-
#as_json(options = {}) ⇒ Object
TODO: Test this update.
-
#attributes=(other) ⇒ Object
TODO: Test this.
- #class ⇒ Object
- #errors ⇒ Object
-
#initialize(model, attributes = {}) ⇒ Record
constructor
A new instance of Record.
- #inspect ⇒ Object (also: #to_s)
- #new_record? ⇒ Boolean
- #save ⇒ Object
- #typecast(key, value) ⇒ Object
Constructor Details
#initialize(model, attributes = {}) ⇒ Record
Returns a new instance of Record.
57 58 59 60 61 62 63 64 |
# File 'lib/betty_resource/model/record.rb', line 57 def initialize(model, attributes = {}) @model = model @id = attributes.delete(:id) || attributes.delete('id') @errors = {} super(self) self.attributes = Hash[model.attributes.map { |x| [x, nil] }].merge attributes self.attributes.instance_variable_set(:@model, model) end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
45 46 47 |
# File 'lib/betty_resource/model/record.rb', line 45 def id @id end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
45 46 47 |
# File 'lib/betty_resource/model/record.rb', line 45 def model @model end |
Instance Method Details
#_class ⇒ Object
47 |
# File 'lib/betty_resource/model/record.rb', line 47 alias :_class :class |
#as_json(options = {}) ⇒ Object
TODO: Test this update
108 109 110 |
# File 'lib/betty_resource/model/record.rb', line 108 def as_json( = {}) attributes_as_json().merge! 'id' => id end |
#attributes=(other) ⇒ Object
TODO: Test this
71 72 73 74 75 |
# File 'lib/betty_resource/model/record.rb', line 71 def attributes=(other) other.each do |key, value| send "#{key}=", value end end |
#class ⇒ Object
48 49 50 |
# File 'lib/betty_resource/model/record.rb', line 48 def class model end |
#errors ⇒ Object
77 78 79 |
# File 'lib/betty_resource/model/record.rb', line 77 def errors @errors.dup end |
#inspect ⇒ Object Also known as: to_s
101 102 103 104 |
# File 'lib/betty_resource/model/record.rb', line 101 def inspect inspection = "id: #{id.inspect}, #{attributes.map { |key, value| "#{key}: #{value.inspect}"}.join(", ")}" "#<#{model.name} #{inspection}>" end |
#new_record? ⇒ Boolean
66 67 68 |
# File 'lib/betty_resource/model/record.rb', line 66 def new_record? @id.nil? end |
#save ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/betty_resource/model/record.rb', line 81 def save @errors.clear result = begin if new_record? Api.post("/models/#{model.id}/records/new", to_params) else Api.put("/models/#{model.id}/records/#{id}", to_params) end end result.success?.tap do |success| if success model.send :load, result.parsed_response, self else @errors = result.parsed_response ? result.parsed_response['errors'] : { '' => ['Er is iets mis gegaan met het verwerken van het formulier. Probeer u het later nog eens. Onze excuses voor het ongemak'] } end end end |
#typecast(key, value) ⇒ Object
52 53 54 55 |
# File 'lib/betty_resource/model/record.rb', line 52 def typecast(key, value) property = self.class.property(key) property ? property.typecast(self, value) : value end |