Class: SimpleSync::RecordSet

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_sync.rb

Overview

RecordSet is a class used internally to store collections of records, for new_records, changed_records, and deleted_records. Though not documented here, you can use several of the Array or Enumerable methods: [], <<, each, clear, length. RecordSets are created automatically, so the new method isn’t documented either. The biggest thing it does is extend all record objects with the SimpleSync::Record module, which gives them a few essential methods. RecordSet is also where all the snapshot! methods actually do their work.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, getter_proc = nil) ⇒ RecordSet

:nodoc:



262
263
264
265
# File 'lib/simple_sync.rb', line 262

def initialize(source,getter_proc=nil) # :nodoc:
  @source = source
  @getter = getter_proc if getter_proc
end

Instance Attribute Details

#getterObject

:nodoc:



260
261
262
# File 'lib/simple_sync.rb', line 260

def getter
  @getter
end

Instance Method Details

#<<(values) ⇒ Object

:nodoc:



287
288
289
290
291
292
293
# File 'lib/simple_sync.rb', line 287

def <<(values) # :nodoc:
  [values].flatten.compact.each do |rec|
    rec.extend Record
    rec.source = @source
    @set << rec
  end
end

#[](i) ⇒ Object

:nodoc:



283
284
285
# File 'lib/simple_sync.rb', line 283

def [](i) # :nodoc:
  set[i]
end

#clearObject

Simply clears the collection.



313
314
315
# File 'lib/simple_sync.rb', line 313

def clear
  set.clear
end

#delete(record_or_identifier) ⇒ Object

Same as RecordSet#find (below), but removes the found record from this RecordSet.



296
297
298
299
# File 'lib/simple_sync.rb', line 296

def delete(record_or_identifier)
  identifier = get_mapped_identifier(record_or_identifier)
  !!(set.reject! {|e| e.identifier == identifier})
end

#each(&block) ⇒ Object

:nodoc:



308
309
310
# File 'lib/simple_sync.rb', line 308

def each(&block) # :nodoc:
  set.each(&block)
end

#find(record) ⇒ Object

Finds a in this RecordSet given a record from another RecordSet on another source, or returns nil on not found.



302
303
304
305
306
# File 'lib/simple_sync.rb', line 302

def find(record)
  # ident_key = source.mapping_to(record.source)[source.identifier.to_s]
  # ident = {source.identifier.to_s => record.send(ident_key)}
  set.reject {|e| e.identifier != record.identifier}[0]
end

#lengthObject

:nodoc:



317
318
319
# File 'lib/simple_sync.rb', line 317

def length # :nodoc:
  set.length
end

#setObject

Returns a simple Array of the collection - provided public mostly for debugging purposes. Typically you won’t use this method publicly in production code.



279
280
281
# File 'lib/simple_sync.rb', line 279

def set
  @set ||= []
end

#snapshot!Object

Takes a snapshot of whatever type of records this RecordSet holds, by using the block assigned to the RecordSet.



268
269
270
271
272
273
274
275
# File 'lib/simple_sync.rb', line 268

def snapshot!
  if @getter.is_a?(Proc)
    clear
    self << @getter.call
  else
    nil
  end
end