Class: SimpleSync::Source

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_scopeObject

Returns the value of attribute finder_scope.



182
183
184
# File 'lib/simple_sync.rb', line 182

def finder_scope
  @finder_scope
end

#identifierObject

Returns the value of attribute identifier.



182
183
184
# File 'lib/simple_sync.rb', line 182

def identifier
  @identifier
end

#klassObject

Returns the value of attribute klass.



182
183
184
# File 'lib/simple_sync.rb', line 182

def klass
  @klass
end

#mappingsObject

Returns the value of attribute mappings.



182
183
184
# File 'lib/simple_sync.rb', line 182

def mappings
  @mappings
end

#targetsObject

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].

Returns:

  • (Boolean)


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(finder_options={})
  klass.get(finder_options.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

#inspectObject

: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

#nameObject

: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).

Raises:

  • (RuntimeError)


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