lib/synchronisable/synchronizer.rb in synchronisable-0.0.4 vs lib/synchronisable/synchronizer.rb in synchronisable-0.0.5
- old
+ new
@@ -36,22 +36,27 @@
# Logger that will be used during synchronization
# of this particular model.
attribute :logger, default: -> { Synchronisable.logging[:logger] }
+ # Gateway to be used to get the remote data
+ #
+ # @see Synchronizable::Gateway
+ attribute :gateway
+
# Lambda that returns array of hashes with remote attributes.
- method :fetch, default: -> { [] }
+ method :fetcher, default: -> { [] }
# Lambda that returns a hash with remote attributes by id.
#
# @example Common use case
# class FooSynchronizer < Synchronisable::Synchronizer
# find do |id|
# remote_source.find { |h| h[:foo_id] == id } }
# end
# end
- method :find
+ method :finder, default: -> (id) { nil }
# Lambda, that will be called before synchronization
# of each record and its assocations.
#
# @param source [Synchronisable::Source] synchronization source
@@ -93,9 +98,23 @@
# @param association [Synchronisable::DSL::Associations::Association]
# association builder
method :after_association_sync
class << self
+ def fetch
+ data = fetcher.()
+ data.present? ? data : gateway_instance.try(:fetch)
+ end
+
+ def find(id)
+ data = finder.(id)
+ data.present? ? data : gateway_instance.try(:find, id)
+ end
+
+ def gateway_instance
+ @gateway_instance ||= gateway.try(:new, self)
+ end
+
# Extracts remote id from given attribute hash.
#
# @param attrs [Hash] remote attributes
# @return remote id value
#