class ObjectJSONMapper::Base

Attributes

associations[RW]
relation[RW]
root_url[RW]
attributes[RW]
persisted[RW]
persisted?[RW]

Public Class Methods

all(conditions = {})
Alias for: where
attribute(name, type: nil, default: nil) click to toggle source

@param name [Symbol] @param type [Dry::Types::Constructor] @param default [Proc]

# File lib/object_json_mapper/base.rb, line 93
def attribute(name, type: nil, default: nil)
  define_method(name) do
    return default.call if attributes.exclude?(name) && default
    return type.call(attributes[name]) if type

    attributes[name]
  end

  define_method("#{name}=") do |value|
    attributes[name] = value
  end
end
client() click to toggle source
# File lib/object_json_mapper/base.rb, line 71
def client
  RestClient::Resource.new(
    URI.join(ObjectJSONMapper.base_url, root_url).to_s,
    headers: ObjectJSONMapper.headers
  )
end
configure() { |self| ... } click to toggle source
# File lib/object_json_mapper/base.rb, line 78
def configure
  yield self
end
find(id) click to toggle source

@param id [Integer] @return [ObjectJSONMapper::Base] current model instance

# File lib/object_json_mapper/base.rb, line 143
def find(id)
  raise ActiveRecord::RecordNotFound if id.nil?

  result = HTTP.parse_json(client[id].get.body)

  persist(result)
rescue RestClient::ExceptionWithResponse
  raise ActiveRecord::RecordNotFound
end
find_by(conditions = {}) click to toggle source

rubocop:disable Rails/FindBy

@param conditions [Hash] @return [ObjectJSONMapper::Base] current model instance

# File lib/object_json_mapper/base.rb, line 157
def find_by(conditions = {})
  where(conditions).first
end
inherited(base) click to toggle source
# File lib/object_json_mapper/base.rb, line 65
def inherited(base)
  base.root_url     = base.name.underscore.pluralize
  base.associations = Associations::Registry.new
  base.relation     = Relation.new(klass: base)
end
name() click to toggle source
Calls superclass method
# File lib/object_json_mapper/base.rb, line 86
def name
  @name || super
end
name=(value) click to toggle source
# File lib/object_json_mapper/base.rb, line 82
def name=(value)
  @name = value.to_s
end
new(attributes = {}) click to toggle source
# File lib/object_json_mapper/base.rb, line 21
def initialize(attributes = {})
  self.attributes = attributes
  @persisted = false
end
none() click to toggle source
# File lib/object_json_mapper/base.rb, line 161
def none
  NullRelation.new(klass: self)
end
persist(attributes = {}) click to toggle source

Same as `new` but for persisted records @param attributes [Hash] @return [ObjectJSONMapper::Base]

# File lib/object_json_mapper/base.rb, line 128
def persist(attributes = {})
  new(attributes).tap do |base|
    base.persisted = true
  end
end
root(value) click to toggle source
# File lib/object_json_mapper/base.rb, line 118
def root(value)
  clone.tap do |base|
    base.name     = name
    base.root_url = value.to_s
  end
end
scope(name, block) click to toggle source

@param name [Symbol] @param block [Proc]

# File lib/object_json_mapper/base.rb, line 108
def scope(name, block)
  define_singleton_method(name) do
    relation.deep_clone.instance_exec(&block)
  end

  relation.define_singleton_method(name) do
    instance_exec(&block)
  end
end
where(conditions = {}) click to toggle source

@param conditions [Hash] @return [ObjectJSONMapper::Relation<ObjectJSONMapper::Base>] collection of model instances

# File lib/object_json_mapper/base.rb, line 136
def where(conditions = {})
  relation.tap { |relation| relation.klass = self }.where(conditions)
end
Also aliased as: all

Public Instance Methods

==(other) click to toggle source
# File lib/object_json_mapper/base.rb, line 54
def ==(other)
  attributes == other.attributes && persisted == other.persisted
end
attributes=(value) click to toggle source

@param value [Hash]

# File lib/object_json_mapper/base.rb, line 50
def attributes=(value)
  @attributes = HashWithIndifferentAccess.new(value)
end
client() click to toggle source
# File lib/object_json_mapper/base.rb, line 58
def client
  self.class.client[id]
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/object_json_mapper/base.rb, line 40
def method_missing(method_name, *args, &block)
  return attributes.fetch(method_name) if respond_to_missing?(method_name)
  super
end
new_record?() click to toggle source
# File lib/object_json_mapper/base.rb, line 28
def new_record?
  !persisted?
end
persist() click to toggle source
# File lib/object_json_mapper/base.rb, line 32
def persist
  @persisted = true
end
reloadable?() click to toggle source
# File lib/object_json_mapper/base.rb, line 36
def reloadable?
  to_key.any?
end
respond_to_missing?(method_name, *) click to toggle source
# File lib/object_json_mapper/base.rb, line 45
def respond_to_missing?(method_name, *)
  attributes.key?(method_name)
end