Class: FidorApi::Connectivity::Resource

Inherits:
Object
  • Object
show all
Extended by:
ModelAttribute
Includes:
ActiveModel::Model
Defined in:
lib/fidor_api/connectivity/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



25
26
27
# File 'lib/fidor_api/connectivity/resource.rb', line 25

def initialize(attributes = {})
  set_attributes attributes
end

Instance Attribute Details

#error_keysObject

Returns the value of attribute error_keys.



9
10
11
# File 'lib/fidor_api/connectivity/resource.rb', line 9

def error_keys
  @error_keys
end

Class Method Details

.all(options = {}) ⇒ Object



16
17
18
# File 'lib/fidor_api/connectivity/resource.rb', line 16

def all(options = {})
  FidorApi::Collection.build self, endpoint.for(self).get(target: :collection, query_params: options).body
end

.find(id) ⇒ Object



12
13
14
# File 'lib/fidor_api/connectivity/resource.rb', line 12

def find(id)
  new endpoint.for(id).get.body.reverse_merge(id: id)
end

.model_nameObject



20
21
22
# File 'lib/fidor_api/connectivity/resource.rb', line 20

def model_name
  ActiveModel::Name.new(self, nil, self.name.sub("FidorApi::", ""))
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/fidor_api/connectivity/resource.rb', line 33

def persisted?
  id.present?
end

#reloadObject



29
30
31
# File 'lib/fidor_api/connectivity/resource.rb', line 29

def reload
  set_attributes endpoint.for(self).get.body
end

#saveObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fidor_api/connectivity/resource.rb', line 37

def save
  if valid?
    set_attributes(persisted? ? remote_update.body : remote_create.body)
    true
  else
    false
  end
rescue ValidationError => e
  self.error_keys = e.error_keys
  map_errors(e.fields)
  false
end

#update_attributes(attributes = {}) ⇒ Object



50
51
52
53
# File 'lib/fidor_api/connectivity/resource.rb', line 50

def update_attributes(attributes={})
  set_attributes attributes
  valid? and remote_update attributes.keys
end