Module: Remotable

Extended by:
Nosync, ValidateModels
Defined in:
lib/remotable.rb,
lib/remotable/nosync.rb,
lib/remotable/version.rb,
lib/remotable/core_ext/uri.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, Nosync, ValidateModels Classes: InvalidRemoteModel, WithRemoteModelProxy

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Nosync

nosync, nosync!, nosync=, nosync?

Methods included from ValidateModels

validate_models=, validate_models?, without_validation

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:

* +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.
* +new_resource+

Resources must respond to:

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


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/remotable.rb', line 75

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

#with_remote_model(model) ⇒ Object



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

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