Class: SimpleSync::Source
- Inherits:
-
Object
- Object
- SimpleSync::Source
- Defined in:
- lib/simple_sync.rb
Overview
Source represents each sync source. There may be more than two. A source must be tied to a Syncer (use Syncer#add_source to create sources), and any two sources must have mappings defined between them.
Instance Attribute Summary collapse
-
#finder_scope ⇒ Object
Returns the value of attribute finder_scope.
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#mappings ⇒ Object
Returns the value of attribute mappings.
-
#targets ⇒ Object
Returns the value of attribute targets.
Instance Method Summary collapse
-
#can_map?(target) ⇒ Boolean
Returns true or false after checking to see if mappings are defined in source.mappings[source => target].
-
#changed_records(&block) ⇒ Object
When called with a block, that block is stored for use when snapshot! is called, in order to retrieve the changed_records from the source.
-
#deleted_records(&block) ⇒ Object
When called with a block, that block is stored for use when snapshot! is called, in order to retrieve the deleted_records from the source.
-
#get(finder_options = {}) ⇒ Object
Calls klass.get with the :finder_scope option(s) merged into the finder_options.
-
#initialize(klass, identifier, mappings, finder_scope, &block) ⇒ Source
constructor
Creates a new Source object.
-
#initialize_record(record) ⇒ Object
Initializes a record retrieved by the SimpleSync gem.
-
#inspect ⇒ Object
:nodoc:.
-
#mapping_to(target) ⇒ Object
source.mapping_to(target) is synonymous to source.mappings[source => target].
-
#name ⇒ Object
:nodoc:.
-
#new_records(&block) ⇒ Object
When called with a block, that block is stored for use when snapshot! is called, in order to retrieve the new_records from the source.
-
#snapshot! ⇒ Object
Takes a snapshot of new_records, changed_records and deleted_records.
Constructor Details
#initialize(klass, identifier, mappings, finder_scope, &block) ⇒ Source
Creates a new Source object. Use Syncer#add_source unless you know why you want to call this directly. If a block is supplied, it is understood to be a block to be run on a newly-created record object of this source type.
211 212 213 214 215 216 217 |
# File 'lib/simple_sync.rb', line 211 def initialize(klass, identifier, mappings, finder_scope, &block) @klass = klass @identifier = identifier @mappings = mappings @finder_scope = finder_scope @initialize = block if block_given? end |
Instance Attribute Details
#finder_scope ⇒ Object
Returns the value of attribute finder_scope.
182 183 184 |
# File 'lib/simple_sync.rb', line 182 def finder_scope @finder_scope end |
#identifier ⇒ Object
Returns the value of attribute identifier.
182 183 184 |
# File 'lib/simple_sync.rb', line 182 def identifier @identifier end |
#klass ⇒ Object
Returns the value of attribute klass.
182 183 184 |
# File 'lib/simple_sync.rb', line 182 def klass @klass end |
#mappings ⇒ Object
Returns the value of attribute mappings.
182 183 184 |
# File 'lib/simple_sync.rb', line 182 def mappings @mappings end |
#targets ⇒ Object
Returns the value of attribute targets.
182 183 184 |
# File 'lib/simple_sync.rb', line 182 def targets @targets end |
Instance Method Details
#can_map?(target) ⇒ Boolean
Returns true or false after checking to see if mappings are defined in source.mappings[source => target].
249 250 251 |
# File 'lib/simple_sync.rb', line 249 def can_map?(target) !mappings[self => target].empty? end |
#changed_records(&block) ⇒ Object
When called with a block, that block is stored for use when snapshot! is called, in order to retrieve the changed_records from the source. Whether a block is given or not, a RecordSet object is returned that holds the current known list of changed_records for this source.
197 198 199 200 |
# File 'lib/simple_sync.rb', line 197 def changed_records(&block) changed_records.getter = block if block_given? @changed_records ||= RecordSet.new(self) end |
#deleted_records(&block) ⇒ Object
When called with a block, that block is stored for use when snapshot! is called, in order to retrieve the deleted_records from the source. Whether a block is given or not, a RecordSet object is returned that holds the current known list of deleted_records for this source.
204 205 206 207 |
# File 'lib/simple_sync.rb', line 204 def deleted_records(&block) deleted_records.getter = block if block_given? @deleted_records ||= RecordSet.new(self) end |
#get(finder_options = {}) ⇒ Object
Calls klass.get with the :finder_scope option(s) merged into the finder_options.
220 221 222 |
# File 'lib/simple_sync.rb', line 220 def get(={}) klass.get(.merge(@finder_scope || {})) end |
#initialize_record(record) ⇒ Object
Initializes a record retrieved by the SimpleSync gem. Simply runs the block supplied to the Source object creation, if one was given.
225 226 227 228 229 |
# File 'lib/simple_sync.rb', line 225 def initialize_record(record) return record if @initialize.nil? record.send(:eval, @initialize.to_ruby).call record end |
#inspect ⇒ Object
:nodoc:
231 232 233 |
# File 'lib/simple_sync.rb', line 231 def inspect # :nodoc: "#<SimpleSync::Source #{name}: #{new_records.length} new, #{changed_records.length} changed, #{deleted_records.length} deleted >" end |
#mapping_to(target) ⇒ Object
source.mapping_to(target) is synonymous to source.mappings[source => target].
245 246 247 |
# File 'lib/simple_sync.rb', line 245 def mapping_to(target) mappings[self => target].merge(self.identifier.to_s => target.identifier.to_s) end |
#name ⇒ Object
:nodoc:
183 184 185 |
# File 'lib/simple_sync.rb', line 183 def name # :nodoc: "#{klass.name}#{finder_scope ? '('+finder_scope.inspect+')' : ''}" end |
#new_records(&block) ⇒ Object
When called with a block, that block is stored for use when snapshot! is called, in order to retrieve the new_records from the source. Whether a block is given or not, a RecordSet object is returned that holds the current known list of new_records for this source.
190 191 192 193 |
# File 'lib/simple_sync.rb', line 190 def new_records(&block) new_records.getter = block if block_given? @new_records ||= RecordSet.new(self) end |
#snapshot! ⇒ Object
Takes a snapshot of new_records, changed_records and deleted_records. Returns self (the Source object).
236 237 238 239 240 241 242 |
# File 'lib/simple_sync.rb', line 236 def snapshot! raise RuntimeError, "Must send new_records, changed_records, and deleted_records each a block before this function can be run" unless @new_records_getter.is_a?(Proc) && @changed_records_getter.is_a?(Proc) && @deleted_records_getter.is_a?(Proc) new_records.snapshot! changed_records.snapshot! deleted_records.snapshot! self end |