Module: Remotable

Extended by:
Nosync, ValidateModels
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, Nosync, 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.3.0"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Nosync

nosync, nosync!, nosync=, nosync?, nosync_value?

Methods included from ValidateModels

validate_models=, validate_models?, without_validation

Class Attribute Details

.log_levelObject

Returns the value of attribute log_level.



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

def log_level
  @log_level
end

Class Method Details

.http_format_time(time) ⇒ Object



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

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

.loggerObject

Logger



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

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

.logger=(logger) ⇒ Object



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

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


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

def remote_model(*args)
  if args.length >= 1
    @remote_model = args.first
    
    @__remotable_included ||= begin
      require "remotable/active_record_extender"
      include Remotable::ActiveRecordExtender
      true
    end
    
    extend_remote_model(@remote_model) if @remote_model
  end
  @remote_model
end

#with_remote_model(model) ⇒ Object



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

def with_remote_model(model)
  if block_given?
    begin
      original = self.remote_model
      self.remote_model(model)
      yield
    ensure
      self.remote_model(original)
    end
  else
    WithRemoteModelProxy.new(self, model)
  end
end