Class: RubyJsonApiClient::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods, ActiveModel::Model
Defined in:
lib/ruby_json_api_client/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__origin__Object

Returns the value of attribute __origin__.



31
32
33
# File 'lib/ruby_json_api_client/base.rb', line 31

def __origin__
  @__origin__
end

#metaObject

Returns the value of attribute meta.



30
31
32
# File 'lib/ruby_json_api_client/base.rb', line 30

def meta
  @meta
end

Class Method Details

._identifierObject



25
26
27
# File 'lib/ruby_json_api_client/base.rb', line 25

def self._identifier
  @_identifier || superclass._identifier
end

.allObject



77
78
79
# File 'lib/ruby_json_api_client/base.rb', line 77

def self.all
  where({})
end

.field(name, type = :string) ⇒ Object



6
7
8
9
10
# File 'lib/ruby_json_api_client/base.rb', line 6

def self.field(name, type = :string)
  @_fields ||= []
  @_fields << name
  attr_accessor name
end

.fieldsObject



12
13
14
# File 'lib/ruby_json_api_client/base.rb', line 12

def self.fields
  @_fields || []
end

.find(id) ⇒ Object



73
74
75
# File 'lib/ruby_json_api_client/base.rb', line 73

def self.find(id)
  RubyJsonApiClient::Store.instance.find(self, id)
end

.has_field?(name) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ruby_json_api_client/base.rb', line 16

def self.has_field?(name)
  (@_fields | [_identifier]).include?(name)
end

.has_many(name, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_json_api_client/base.rb', line 33

def self.has_many(name, options = {})
  @_has_many_relationships ||= []
  @_has_many_relationships << name
  define_method(name) do
    # make cachable
    RubyJsonApiClient::Store
      .instance
      .find_many_relationship(self, name, options)
  end
end

.has_many_relationshipsObject



44
45
46
# File 'lib/ruby_json_api_client/base.rb', line 44

def self.has_many_relationships
  @_has_many_relationships
end

.has_one(name, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_json_api_client/base.rb', line 48

def self.has_one(name, options = {})
  define_method(name) do
    @_loaded_has_ones ||= {}

    if @_loaded_has_ones[name].nil?
      result = RubyJsonApiClient::Store
        .instance
        .find_single_relationship(self, name, options)

      @_loaded_has_ones[name] = result
    end

    @_loaded_has_ones[name]
  end

  define_method("#{name}=".to_sym) do |related|
    @_loaded_has_ones ||= {}
    @_loaded_has_ones[name] = related
  end
end

.identifier(name) ⇒ Object



20
21
22
23
# File 'lib/ruby_json_api_client/base.rb', line 20

def self.identifier(name)
  @_identifier = name
  field(name, :number)
end

.where(params) ⇒ Object



81
82
83
# File 'lib/ruby_json_api_client/base.rb', line 81

def self.where(params)
  RubyJsonApiClient::Store.instance.query(self, params)
end

Instance Method Details



93
94
95
# File 'lib/ruby_json_api_client/base.rb', line 93

def links
  store.find(self.class._identifier).__data__
end

#loaded_has_onesObject



69
70
71
# File 'lib/ruby_json_api_client/base.rb', line 69

def loaded_has_ones
  @_loaded_has_ones || {}
end

#persisted?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/ruby_json_api_client/base.rb', line 85

def persisted?
  !!send(self.class._identifier)
end

#reloadObject



89
90
91
# File 'lib/ruby_json_api_client/base.rb', line 89

def reload
  RubyJsonApiClient::Store.instance.reload(self)
end