Class: RubyJsonApiClient::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods, ActiveModel::Model, ActiveModel::Serialization, ActiveModel::SerializerSupport
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__.



49
50
51
# File 'lib/ruby_json_api_client/base.rb', line 49

def __origin__
  @__origin__
end

#metaObject

Returns the value of attribute meta.



48
49
50
# File 'lib/ruby_json_api_client/base.rb', line 48

def meta
  @meta
end

Class Method Details

._identifierObject



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

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

.allObject



110
111
112
# File 'lib/ruby_json_api_client/base.rb', line 110

def self.all
  where({})
end

.attributesObject



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

def self.attributes
  fields.reduce({}) do |attributes, field|
    attributes[field] = nil
    attributes
  end
end

.create(params) ⇒ Object



118
119
120
# File 'lib/ruby_json_api_client/base.rb', line 118

def self.create(params)
  new(params).tap(&:save)
end

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



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

def self.field(name, type = :string)
  fields << name
  attr_accessor name
end

.fieldsObject



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

def self.fields
  @_fields ||= Set.new [_identifier]
end

.find(id) ⇒ Object



106
107
108
# File 'lib/ruby_json_api_client/base.rb', line 106

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

.has_field?(name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ruby_json_api_client/base.rb', line 34

def self.has_field?(name)
  fields.include?(name)
end

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



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

def self.has_many(name, options = {})
  @_has_many_relationships ||= []
  @_has_many_relationships << name

  define_method(name) do
    @_loaded_has_manys ||= {}

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

      @_loaded_has_manys[name] = result
    end

    @_loaded_has_manys[name]
  end
end

.has_many_relationshipsObject



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

def self.has_many_relationships
  @_has_many_relationships
end

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_json_api_client/base.rb', line 74

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

  define_method("#{name}_id=".to_sym) do |related_id|
    klass_name = options[:class_name] || ActiveSupport::Inflector.classify(name)
    klass = ActiveSupport::Inflector.constantize(klass_name)
    @_loaded_has_ones ||= {}
    @_loaded_has_ones[name] = klass.new(id: related_id)
  end
end

.identifier(name) ⇒ Object



38
39
40
41
# File 'lib/ruby_json_api_client/base.rb', line 38

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

.where(params) ⇒ Object



114
115
116
# File 'lib/ruby_json_api_client/base.rb', line 114

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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?, equal?



145
146
147
148
149
150
# File 'lib/ruby_json_api_client/base.rb', line 145

def ==(other)
  klass_match = (self.class == other.class)
  ids_match = (send(self.class._identifier) == other.send(other.class._identifier))

  klass_match && ids_match
end

#attributesObject



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

def attributes
  @attrs ||= self.class.attributes
end

#destroyObject



141
142
143
# File 'lib/ruby_json_api_client/base.rb', line 141

def destroy
  RubyJsonApiClient::Store.instance.delete(self)
end

#loaded_has_onesObject



102
103
104
# File 'lib/ruby_json_api_client/base.rb', line 102

def loaded_has_ones
  @_loaded_has_ones || {}
end

#persisted?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/ruby_json_api_client/base.rb', line 122

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

#reloadObject



126
127
128
# File 'lib/ruby_json_api_client/base.rb', line 126

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

#saveObject



130
131
132
# File 'lib/ruby_json_api_client/base.rb', line 130

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

#update_attributes(data) ⇒ Object



134
135
136
137
138
139
# File 'lib/ruby_json_api_client/base.rb', line 134

def update_attributes(data)
  data.each do |(key, value)|
    send("#{key}=", value)
  end
  save
end