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=, #persisted?, #query_params, #respond_to?, #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



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

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



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

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

.resourceObject

base URL for this resource



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

def resource
  File.join(site, path)
end

.resource_nameObject



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

def resource_name
  name.demodulize.underscore
end

.run_request(query) ⇒ Object



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

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

.table_nameObject



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

def table_name
  resource_name.pluralize
end

Instance Method Details

#destroyObject



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

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

#saveObject



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

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

  run_request(query)
end