Class: RubyJsonApiClient::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::SerializerSupport, ActiveModel::Validations
Defined in:
lib/ruby_json_api_client/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(params={})
  params.each do |attr, value|
    self.public_send("#{attr}=", value)
  end if params

  super()
end

Instance Attribute Details

#__origin__Object

Returns the value of attribute __origin__.



61
62
63
# File 'lib/ruby_json_api_client/base.rb', line 61

def __origin__
  @__origin__
end

#metaObject

Returns the value of attribute meta.



60
61
62
# File 'lib/ruby_json_api_client/base.rb', line 60

def meta
  @meta
end

Class Method Details

._identifierObject



55
56
57
# File 'lib/ruby_json_api_client/base.rb', line 55

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

.allObject



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

def self.all
  where({})
end

.attributesObject



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

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

.create(params) ⇒ Object



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

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

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



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

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

.fieldsObject



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

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

.find(id) ⇒ Object



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

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

.has_field?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ruby_json_api_client/base.rb', line 46

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

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



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

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



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

def self.has_many_relationships
  @_has_many_relationships
end

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ruby_json_api_client/base.rb', line 86

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



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

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

.where(params) ⇒ Object



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

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

Instance Method Details

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



157
158
159
160
161
162
# File 'lib/ruby_json_api_client/base.rb', line 157

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



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

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

#destroyObject



153
154
155
# File 'lib/ruby_json_api_client/base.rb', line 153

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

#loaded_has_onesObject



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

def loaded_has_ones
  @_loaded_has_ones || {}
end

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#reloadObject



138
139
140
# File 'lib/ruby_json_api_client/base.rb', line 138

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

#saveObject



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

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

#update_attributes(data) ⇒ Object



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

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