class RemoteRecord::Base

Remote record classes should inherit from this class and define get.

Attributes

remote_record_config[RW]
remote_resource_id[R]

Public Class Methods

inherited(subclass) click to toggle source

When you inherit from `Base`, it'll set up an Active Record Type for you available on its Type constant. It'll also have a Collection.

Calls superclass method
# File lib/remote_record/base.rb, line 10
def self.inherited(subclass)
  subclass.const_set :Type, RemoteRecord::Type.for(subclass)
  subclass.const_set :Collection, Class.new(RemoteRecord::Collection) unless subclass.const_defined? :Collection
  super
end
new(remote_resource_id, remote_record_config = Config.defaults, initial_attrs = {}) click to toggle source
# File lib/remote_record/base.rb, line 18
def initialize(remote_resource_id,
               remote_record_config = Config.defaults,
               initial_attrs = {})
  @remote_resource_id = remote_resource_id
  @remote_record_config = remote_record_config
  @attrs = HashWithIndifferentAccess.new(initial_attrs)
  @fetched = initial_attrs.present?
end

Public Instance Methods

attrs=(new_attrs) click to toggle source
# File lib/remote_record/base.rb, line 47
def attrs=(new_attrs)
  @attrs.update(new_attrs)
  @fetched = true
end
fetch() click to toggle source
# File lib/remote_record/base.rb, line 42
def fetch
  @attrs.update(get)
  @fetched = true
end
fresh() click to toggle source
# File lib/remote_record/base.rb, line 52
def fresh
  fetch
  self
end
get() click to toggle source
# File lib/remote_record/base.rb, line 38
def get
  raise NotImplementedError.new, '#get should return a hash of data that represents the remote record.'
end
method_missing(method_name, *_args, &_block) click to toggle source
Calls superclass method
# File lib/remote_record/base.rb, line 27
def method_missing(method_name, *_args, &_block)
  fetch unless @remote_record_config.memoize && @fetched
  transform(@attrs).fetch(method_name)
rescue KeyError
  super
end
respond_to_missing?(method_name, _include_private = false) click to toggle source
# File lib/remote_record/base.rb, line 34
def respond_to_missing?(method_name, _include_private = false)
  @attrs.key?(method_name)
end

Private Instance Methods

authorization() click to toggle source
# File lib/remote_record/base.rb, line 74
def authorization
  authz = @remote_record_config.authorization
  authz.respond_to?(:call) ? authz.call(@remote_record_config.authorization_source) : authz
end
transform(data) click to toggle source
# File lib/remote_record/base.rb, line 59
def transform(data)
  return data unless transformers.any?

  transformers.reduce(data) do |transformed_data, transformer|
    transformer.new(transformed_data).transform
  end
end
transformers() click to toggle source

Robots in disguise.

# File lib/remote_record/base.rb, line 68
def transformers
  @remote_record_config.transform.map do |transformer_name|
    "RemoteRecord::Transformers::#{transformer_name.to_s.camelize}".constantize
  end
end