Class: JsonApiResource::Resource

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Model, ActiveModel::Validations, Cacheable, Conversions, Queryable, Schemable
Defined in:
lib/json_api_resource/resource.rb

Constant Summary

Constants included from Queryable

Queryable::MAX_PAGES_FOR_ALL

Instance Attribute Summary collapse

Attributes included from Queryable

#errors, #linked_data, #meta

Instance Method Summary collapse

Methods included from Cacheable

#cache_key

Methods included from Conversions

#Address, #ApiErrors, #ApiResource, #Boolean, #Date, #DateTime, #Symbolize

Constructor Details

#initialize(opts = {}) ⇒ Resource

Returns a new instance of Resource.



20
21
22
23
24
# File 'lib/json_api_resource/resource.rb', line 20

def initialize(opts={})
  self.client = self.client_klass.new(self.schema)
  self.errors = ActiveModel::Errors.new(self)
  self.attributes = opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/json_api_resource/resource.rb', line 63

def method_missing(method, *args, &block)
  if match = method.to_s.match(/^(.*=)$/)
    self.client.send(match[1], args.first)
  elsif self.client.respond_to?(method.to_sym)
    is_method = self.client.methods.include?(method.to_sym)
    argument_count = (is_method ? self.client.method(method.to_sym).arity : 0)
    argument_count = args.length if argument_count == -1
    if (argument_count == 0) || args.blank?
      self.client.send(method)
    else
      self.client.send(method, *args.take(argument_count))
    end
  else
    super
  end

rescue JsonApiClient::Errors::ServerError => e
  pretty_error e
end

Instance Attribute Details

#cache_expires_inObject

Returns the value of attribute cache_expires_in.



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

def cache_expires_in
  @cache_expires_in
end

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#attributes=(attr = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/json_api_resource/resource.rb', line 50

def attributes=(attr = {})
  client_params = attr.delete(:client)
  if attr.is_a? self.client_klass
    self.client = attr
  elsif client_params
    self.client = client_params
  else
    self.client.attributes = attr
  end
end

#new_record?Boolean

Returns:



26
27
28
# File 'lib/json_api_resource/resource.rb', line 26

def new_record?
  self.id.nil?
end

#persisted?Boolean

Returns:



30
31
32
# File 'lib/json_api_resource/resource.rb', line 30

def persisted?
  !new_record?
end

#saveObject



34
35
36
37
38
39
40
# File 'lib/json_api_resource/resource.rb', line 34

def save
  run_callbacks :save do
    self.client.save
  end
rescue JsonApiClient::Errors::ServerError => e
  pretty_error e
end

#update_attributes(attrs = {}) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/json_api_resource/resource.rb', line 42

def update_attributes(attrs = {})
  run_callbacks :update_attributes do
    self.client.update_attributes(attrs)
  end
rescue JsonApiClient::Errors::ServerError => e
  pretty_error e
end