Module: Remotable

Extended by:
Nosync, ValidateModels
Includes:
ThreadsafeAttributes
Defined in:
lib/remotable.rb,
lib/remotable/errors.rb,
lib/remotable/nosync.rb,
lib/remotable/version.rb,
lib/remotable/null_remote.rb,
lib/remotable/core_ext/uri.rb,
lib/remotable/logger_wrapper.rb,
lib/remotable/core_ext/object.rb,
lib/remotable/validate_models.rb,
lib/remotable/active_record_extender.rb,
lib/remotable/with_remote_model_proxy.rb,
lib/remotable/adapters/active_resource.rb

Overview

Remotable keeps a locally-stored ActiveRecord synchronized with a remote resource.

Requirements ==

Remotable expects there to be an expires_at field on the record.

New resources ==

When a resource isn’t found locally, Remotable fetches it from the remote API and creates a local copy of the resource.

Expired resources ==

When a resource is found locally, Remotable checks the value of expires_at and re-fetches the remote resource if need be.

Deleted resources ==

When a remote resources has been deleted, the local resource should be removed when it is expired.

Creating, Updating, and Destroying local resources ==

Before a local record is saved, Remotable tries to apply the changes remotely.

Defined Under Namespace

Modules: ActiveRecordExtender, Adapters, CoreExt, Error, NetworkError, Nosync, NotFound, SSLError, ServiceUnavailableError, TimeoutError, ValidateModels Classes: FakeLogger, InvalidRemoteModel, LoggerWrapper, NullRemote, WithRemoteModelProxy

Constant Summary collapse

REQUIRED_CLASS_METHODS =
[:find_by, :new_resource]
REQUIRED_INSTANCE_METHODS =
[:save, :errors, :destroy]
VERSION =
"0.6.4"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Nosync

extended, included

Methods included from ValidateModels

extended, validate_models=, validate_models?, without_validation

Class Attribute Details

.log_levelObject

Returns the value of attribute log_level.



53
54
55
# File 'lib/remotable.rb', line 53

def log_level
  @log_level
end

Class Method Details

.http_format_time(time) ⇒ Object



141
142
143
144
# File 'lib/remotable.rb', line 141

def self.http_format_time(time)
  return "" unless time
  time.utc.strftime("%a, %e %b %Y %H:%M:%S %Z")
end

.loggerObject

Logger



49
# File 'lib/remotable.rb', line 49

def self.logger; @logger ||= LoggerWrapper.new(FakeLogger.new); end

.logger=(logger) ⇒ Object



50
# File 'lib/remotable.rb', line 50

def self.logger=(logger); @logger = LoggerWrapper.new(logger); end

Instance Method Details

#remote_model(*args) ⇒ Object

remote_model( model [optional] )

When called without arguments, this method returns the remote model connected to this local ActiveRecord model.

When called with an argument, it extends the ActiveRecord model on which it is called.

model can be a class that inherits from any of these API consumers:

* ActiveResource

model can be any object that responds to these two methods for getting a resource:

* +new_resource+
* +find_by(path)+ or +find_by(remote_attr, value)+
    +find_by+ can be defined to take either one argument or two.
    If it takes one argument, it will be passed path.
    If it takes two, it will be passed remote_attr and value.
* (Optional) +find_by_for_local(local_record, remote_key, fetch_value)+

Resources must respond to:

* +save+ (return true on success and false on failure)
* +destroy+
* +errors+ (returns a hash of error messages by attribute)
* getters and setters for each attribute


91
92
93
94
95
96
97
98
99
100
# File 'lib/remotable.rb', line 91

def remote_model(*args)
  if args.length >= 1
    @remote_model = args.first

    ensure_remotable_included!

    extend_remote_model(@remote_model) if @remote_model
  end
  override_remote_model? ? _remote_model_override : @remote_model
end

#with_remote_model(model) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/remotable.rb', line 104

def with_remote_model(model)
  if block_given?
    begin
      extend_remote_model(model) if model
      ensure_remotable_included!
      self._remote_model_override = model
      self._use_remote_model_override = true
      yield
    ensure
      self._use_remote_model_override = false
    end
  else
    WithRemoteModelProxy.new(self, model)
  end
end