class Boom::Storage

Constants

JSON_FILE

Attributes

lists[W]

Public: the in-memory collection of all Lists attached to this Storage instance.

lists - an Array of individual List items

Returns nothing.

Public Class Methods

new() click to toggle source

Public: initializes a Storage instance by loading in your persisted data from adapter.

Returns the Storage instance.

# File lib/boom/storage.rb, line 19
def initialize
  @lists = []
  bootstrap
  populate
end

Public Instance Methods

bootstrap() click to toggle source

Takes care of bootstrapping the Json file, both in terms of creating the file and in terms of creating a skeleton Json schema.

Return true if successfully saved.

# File lib/boom/storage.rb, line 79
def bootstrap
  return if File.exist?(json_file) and !File.zero?(json_file)
  FileUtils.touch json_file
  File.open(json_file, 'w') {|f| f.write(to_json) }
  save
end
item_exists?(name) click to toggle source

Public: tests whether a named Item exists.

name - the String name of an Item

Returns true if found, false if not.

# File lib/boom/storage.rb, line 62
def item_exists?(name)
  items.detect { |item| item.name == name }
end
items() click to toggle source

Public: all Items in storage.

Returns an Array of all Items.

# File lib/boom/storage.rb, line 53
def items
  @lists.collect(&:items).flatten
end
json_file() click to toggle source

Public: the path to the Json file used by boom.

Returns the String path of boom's Json representation.

# File lib/boom/storage.rb, line 12
def json_file
  ENV['BOOMFILE'] || JSON_FILE
end
list_exists?(name) click to toggle source

Public: tests whether a named List exists.

name - the String name of a List

Returns true if found, false if not.

# File lib/boom/storage.rb, line 46
def list_exists?(name)
  @lists.detect { |list| list.name == name }
end
lists() click to toggle source

Public: the list of Lists in your JSON data, sorted by number of items descending.

Returns an Array of List objects.

# File lib/boom/storage.rb, line 37
def lists
  @lists.sort_by { |list| -list.items.size }
end
populate() click to toggle source

Take a JSON representation of data and explode it out into the constituent Lists and Items for the given Storage instance.

Returns nothing.

# File lib/boom/storage.rb, line 90
def populate
  file = File.new(json_file, 'r')
  storage = Yajl::Parser.parse(file)

  storage['lists'].each do |lists|
    lists.each do |list_name, items|
      @lists << list = List.new(list_name)

      items.each do |item|
        item.each do |name,value|
          list.add_item(Item.new(name,value))
        end
      end
    end
  end
end
save() click to toggle source

Public: persists your in-memory objects to disk in Json format.

lists_Json - list in Json format

Returns true if successful, false if unsuccessful.

# File lib/boom/storage.rb, line 112
def save
  File.open(json_file, 'w') {|f| f.write(to_json) }
end
to_hash() click to toggle source

Public: creates a Hash of the representation of the in-memory data structure. This percolates down to Items by calling to_hash on the List, which in turn calls to_hash on individual Items.

Returns a Hash of the entire data set.

# File lib/boom/storage.rb, line 71
def to_hash
  { :lists => lists.collect(&:to_hash) }
end
to_json() click to toggle source

Public: the Json representation of the current List and Item assortment attached to the Storage instance.

Returns a String Json representation of its Lists and their Items.

# File lib/boom/storage.rb, line 120
def to_json
  Yajl::Encoder.encode(to_hash, :pretty => true)
end