Class: JsonApiClient::Resource

Inherits:
Object
  • Object
show all
Includes:
Helpers::Associable, Helpers::Attributable, Helpers::CustomEndpoints, Helpers::Initializable, Helpers::Linkable, Helpers::Parsable, Helpers::Queryable, Helpers::Serializable
Defined in:
lib/json_api_client/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Serializable

#as_json, #read_attribute_for_serialization

Methods included from Helpers::Attributable

#attributes=, #method_missing, #persisted?, #query_params, #to_param, #update_attributes

Methods included from Helpers::Initializable

#initialize

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class JsonApiClient::Helpers::Attributable

Class Method Details

.create(conditions = {}) ⇒ Object



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

def create(conditions = {})
  result = run_request(Query::Create.new(self, conditions))
  if result.has_errors?
    yield(result) if block_given?
    return nil
  end
  result.first
end

.find(conditions) ⇒ Object



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

def find(conditions)
  run_request(Query::Find.new(self, conditions))
end

.resourceObject

base URL for this resource



17
18
19
# File 'lib/json_api_client/resource.rb', line 17

def resource
  File.join(site, path)
end

.resource_nameObject



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

def resource_name
  name.demodulize.underscore
end

.run_request(query) ⇒ Object



42
43
44
# File 'lib/json_api_client/resource.rb', line 42

def run_request(query)
  parse(connection.execute(query))
end

.table_nameObject



21
22
23
# File 'lib/json_api_client/resource.rb', line 21

def table_name
  resource_name.pluralize
end

Instance Method Details

#destroyObject



64
65
66
67
68
69
70
71
# File 'lib/json_api_client/resource.rb', line 64

def destroy
  if run_request(Query::Destroy.new(self.class, attributes))
    self.attributes.clear
    true
  else
    false
  end
end

#saveObject



56
57
58
59
60
61
62
# File 'lib/json_api_client/resource.rb', line 56

def save
  query = persisted? ? 
    Query::Update.new(self.class, attributes) :
    Query::Create.new(self.class, attributes)

  run_request(query)
end