class JSONFilePersistedCollection

Collection of items that are automatically persisted to a JSON file

Public Class Methods

new( file ) click to toggle source

Creates a new JSONFilePersistedCollection

@param [String] file the JSON file used to persist this collection

# File lib/util/json_file_persisted_collection.rb, line 10
def initialize( file )
  @store = JSONStore.new(file, true)
end

Public Instance Methods

delete( item ) click to toggle source

Deletes the first element from this persisted collection that is equal to item

@param [Hash] item the object we want to delete @return [Hash] the object we just deleted

# File lib/util/json_file_persisted_collection.rb, line 32
def delete( item )
  r = nil
  @store.transaction do
    if @store['collection'].nil?
      r = {}
      @store.abort
    end
    r = @store['collection'].delete_at(@store['collection'].index(item) || @store['collection'].length)
    r = {} if r.nil?
  end
  r
end
length() click to toggle source

Returns the length of the collection

@return [Fixnum] the length of the collection

# File lib/util/json_file_persisted_collection.rb, line 67
def length
  @store.transaction { @store['collection'].nil? ? 0 : @store['collection'].length }
end
push( item ) click to toggle source

Pushes (appends) the given hash on to the end of this persisted collection

@param [Hash] item the object we want to append @return [JSONFilePersistedCollection] the persisted collection itself, so several appends may be chained together

# File lib/util/json_file_persisted_collection.rb, line 19
def push( item )
  @store.transaction do
    @store['collection'] = Array.new if @store['collection'].nil?
    @store['collection'].push(item)
  end
  self
end
replace( item, replacement ) click to toggle source

Replaces the first element from this persisted array that matches item, with replacement

@param [Hash] replacement for the current element @return [Hahs] the item that was replaced, {} if the element wasn't in the collection

# File lib/util/json_file_persisted_collection.rb, line 50
def replace( item, replacement )
  r = delete item
  push replacement
  r
end
to_a() click to toggle source

Returns an array representation of this collection

@return [Array<Hash>] array representation of this persisted collection

# File lib/util/json_file_persisted_collection.rb, line 60
def to_a
  @store.transaction { @store['collection'].nil? ? {} : @store['collection'] }
end