Class: BettyResource::Model::Record

Inherits:
Object
  • Object
show all
Includes:
DirtyAttributes::InstanceMethods, MethodMap
Defined in:
lib/betty_resource/model/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#idObject (readonly)

Returns the value of attribute id.



45
46
47
# File 'lib/betty_resource/model/record.rb', line 45

def id
  @id
end

#modelObject (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

#_classObject



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(options = {})
  attributes_as_json(options).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

#classObject



48
49
50
# File 'lib/betty_resource/model/record.rb', line 48

def class
  model
end

#errorsObject



77
78
79
# File 'lib/betty_resource/model/record.rb', line 77

def errors
  @errors.dup
end

#inspectObject 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

Returns:

  • (Boolean)


66
67
68
# File 'lib/betty_resource/model/record.rb', line 66

def new_record?
  @id.nil?
end

#saveObject



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