Class: Boom::Storage
- Inherits:
-
Object
- Object
- Boom::Storage
- Defined in:
- lib/boom/storage.rb
Constant Summary collapse
- JSON_FILE =
"#{ENV['HOME']}/.boom"
Instance Attribute Summary collapse
-
#lists ⇒ Object
Public: the list of Lists in your JSON data, sorted by number of items descending.
Instance Method Summary collapse
-
#bootstrap ⇒ Object
Takes care of bootstrapping the Json file, both in terms of creating the file and in terms of creating a skeleton Json schema.
-
#initialize ⇒ Storage
constructor
Public: initializes a Storage instance by loading in your persisted data from adapter.
-
#item_exists?(name) ⇒ Boolean
Public: tests whether a named Item exists.
-
#items ⇒ Object
Public: all Items in storage.
-
#json_file ⇒ Object
Public: the path to the Json file used by boom.
-
#list_exists?(name) ⇒ Boolean
Public: tests whether a named List exists.
-
#populate ⇒ Object
Take a JSON representation of data and explode it out into the consituent Lists and Items for the given Storage instance.
-
#save ⇒ Object
Public: persists your in-memory objects to disk in Json format.
-
#to_hash ⇒ Object
Public: creates a Hash of the representation of the in-memory data structure.
-
#to_json ⇒ Object
Public: the Json representation of the current List and Item assortment attached to the Storage instance.
Constructor Details
#initialize ⇒ Storage
Public: initializes a Storage instance by loading in your persisted data from adapter.
Returns the Storage instance.
19 20 21 22 23 |
# File 'lib/boom/storage.rb', line 19 def initialize @lists = [] bootstrap populate end |
Instance Attribute Details
#lists ⇒ Object
Public: the list of Lists in your JSON data, sorted by number of items descending.
Returns an Array of List objects.
37 38 39 |
# File 'lib/boom/storage.rb', line 37 def lists @lists.sort_by { |list| -list.items.size } end |
Instance Method Details
#bootstrap ⇒ Object
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.
79 80 81 82 83 84 |
# File 'lib/boom/storage.rb', line 79 def bootstrap return if File.exist?(json_file) FileUtils.touch json_file File.open(json_file, 'w') {|f| f.write(to_json) } save end |
#item_exists?(name) ⇒ Boolean
Public: tests whether a named Item exists.
name - the String name of an Item
Returns true if found, false if not.
62 63 64 |
# File 'lib/boom/storage.rb', line 62 def item_exists?(name) items.detect { |item| item.name == name } end |
#items ⇒ Object
Public: all Items in storage.
Returns an Array of all Items.
53 54 55 |
# File 'lib/boom/storage.rb', line 53 def items @lists.collect(&:items).flatten end |
#json_file ⇒ Object
Public: the path to the Json file used by boom.
Returns the String path of boom’s Json representation.
12 13 14 |
# File 'lib/boom/storage.rb', line 12 def json_file ENV['BOOMFILE'] || JSON_FILE end |
#list_exists?(name) ⇒ Boolean
Public: tests whether a named List exists.
name - the String name of a List
Returns true if found, false if not.
46 47 48 |
# File 'lib/boom/storage.rb', line 46 def list_exists?(name) @lists.detect { |list| list.name == name } end |
#populate ⇒ Object
Take a JSON representation of data and explode it out into the consituent Lists and Items for the given Storage instance.
Returns nothing.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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 ⇒ Object
Public: persists your in-memory objects to disk in Json format.
lists_Json - list in Json format
Returns true if successful, false if unsuccessful.
112 113 114 |
# File 'lib/boom/storage.rb', line 112 def save File.open(json_file, 'w') {|f| f.write(to_json) } end |
#to_hash ⇒ Object
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 tern calls to_hash on individual Items.
Returns a Hash of the entire data set.
71 72 73 |
# File 'lib/boom/storage.rb', line 71 def to_hash { :lists => lists.collect(&:to_hash) } end |
#to_json ⇒ Object
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.
120 121 122 |
# File 'lib/boom/storage.rb', line 120 def to_json Yajl::Encoder.encode(to_hash, :pretty => true) end |