Build Status Code Climate Inline docs Coverage Status Gem Version Dependency Status

Synchronisable

Provides base fuctionality (models, DSL) for AR synchronization with external resources (apis, services etc).

Overview

Installation

Add this line to your application's Gemfile:

gem 'synchronisable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install synchronisable

Usage

For examples we'll be using a well-known domain with posts & comments

class Post < ActiveRecord::Base
  has_many :comments

  synchronisable
end

class Comment < ActiveRecord::Base
  belongs_to :post

  synchronisable MyCommentSynchronizer
end

As you can see above the first step is to declare your models to be synchronisable. You can do so by using corresponding dsl instruction, that optionally takes a synchonizer class to be used. Actually, the only reason to specify it its when it has a name, that can't be figured out by the following convention: ModelSynchronizer.

After that you should define your model synchronizers

class PostSynchronizer < Synchronisable::Synchronizer
  remote_id :p_id

  mappings(
    :t => :title,
    :c => :content
  )

  except :ignored_attr1, :ignored_attr42

  has_many :comments

  fetch do
    # return array of hashes with
    # remote entity attributes
  end

  find do |id|
    # return a hash with
    # with remote entity attributes
  end

  # Hooks/callbacks

  before_record_sync do |source|
    # ...
  end

  after_record_sync do |source|
    # ...
  end

  before_association_sync do |source, remote_id, association|
    # ...
  end

  after_association_sync do |source, remote_id, association|
    # ...
  end

  before_sync do |source|
    # ...
  end

  after_sync do |source|
    # ...
  end
end

class MyCommentSynchronizer < Synchronisable::Synchronizer
  remote_id :c_id

  mappings(
    :a => :author,
    :t => :body
  )

  only :author, :body

  fetch do
    # ...
  end

  find do |id|
    # ...
  end

end

To start synchronization

Post.sync

P.S.: Better readme & wiki is coming! ^__^

Support

expert-button