class Boom::Storage
Constants
- JSON_FILE
Attributes
Public Class Methods
Public Instance Methods
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
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
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
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
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
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
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