class RubyJsonApiClient::Base

Attributes

__origin__[RW]
meta[RW]

Public Class Methods

_identifier() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 55
def self._identifier
  @_identifier || superclass._identifier
end
all() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 122
def self.all
  where({})
end
attributes() click to toggle source
# 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) click to toggle source
# File lib/ruby_json_api_client/base.rb, line 130
def self.create(params)
  new(params).tap(&:save)
end
field(name, type = :string) click to toggle source
# File lib/ruby_json_api_client/base.rb, line 26
def self.field(name, type = :string)
  fields << name
  attr_accessor name
end
fields() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 31
def self.fields
  @_fields ||= Set.new [_identifier]
end
find(id) click to toggle source
# File lib/ruby_json_api_client/base.rb, line 118
def self.find(id)
  RubyJsonApiClient::Store.instance.find(self, id)
end
has_field?(name) click to toggle source
# File lib/ruby_json_api_client/base.rb, line 46
def self.has_field?(name)
  fields.include?(name)
end
has_many(name, options = {}) click to toggle source
# 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_relationships() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 82
def self.has_many_relationships
  @_has_many_relationships
end
has_one(name, options = {}) click to toggle source
# 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) click to toggle source
# File lib/ruby_json_api_client/base.rb, line 50
def self.identifier(name)
  @_identifier = name
  field(name, :number)
end
new(params={}) click to toggle source
Calls superclass method
# 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
where(params) click to toggle source
# File lib/ruby_json_api_client/base.rb, line 126
def self.where(params)
  RubyJsonApiClient::Store.instance.query(self, params)
end

Public Instance Methods

==(other) click to toggle source
# File lib/ruby_json_api_client/base.rb, line 169
def ==(other)
  klass_match = (self.class == other.class)
  ids_match = (send(self.class._identifier) == other.send(other.class._identifier))

  klass_match && ids_match
end
Also aliased as: eql?, equal?
_destroy() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 146
def _destroy
  marked_for_destruction?
end
attributes() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 42
def attributes
  @attrs ||= self.class.attributes
end
destroy() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 165
def destroy
  RubyJsonApiClient::Store.instance.delete(self)
end
eql?(other)
Alias for: ==
equal?(other)
Alias for: ==
hash() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 176
def hash
  self.send(self.class._identifier).hash
end
loaded_has_ones() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 114
def loaded_has_ones
  @_loaded_has_ones || {}
end
marked_for_destruction?() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 142
def marked_for_destruction?
  @marked_for_destruction
end
new_record?() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 138
def new_record?
  !persisted?
end
persisted?() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 134
def persisted?
  !!send(self.class._identifier)
end
reload() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 150
def reload
  RubyJsonApiClient::Store.instance.reload(self)
end
save() click to toggle source
# File lib/ruby_json_api_client/base.rb, line 154
def save
  RubyJsonApiClient::Store.instance.save(self)
end
update_attributes(data) click to toggle source
# File lib/ruby_json_api_client/base.rb, line 158
def update_attributes(data)
  data.each do |(key, value)|
    send("#{key}=", value)
  end
  save
end