Class: Boom::Storage::Json
Constant Summary collapse
- JSON_FILE =
"#{ENV['HOME']}/.boom"
Instance Attribute Summary
Attributes inherited from Base
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.
-
#json_file ⇒ Object
Public: the path to the Json file used by boom.
-
#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_json ⇒ Object
Public: the Json representation of the current List and Item assortment attached to the Storage instance.
Methods inherited from Base
#initialize, #item_exists?, #items, #list_exists?, #to_hash
Constructor Details
This class inherits a constructor from Boom::Storage::Base
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.
23 24 25 26 27 28 |
# File 'lib/boom/storage/json.rb', line 23 def bootstrap return if File.exist?(json_file) FileUtils.touch json_file File.open(json_file, 'w') {|f| f.write(to_json) } save end |
#json_file ⇒ Object
Public: the path to the Json file used by boom.
Returns the String path of boom’s Json representation.
15 16 17 |
# File 'lib/boom/storage/json.rb', line 15 def json_file JSON_FILE 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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/boom/storage/json.rb', line 34 def populate storage = Yajl::Parser.new.parse(File.new(json_file, 'r')) 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.
55 56 57 |
# File 'lib/boom/storage/json.rb', line 55 def save File.open(json_file, 'w') {|f| f.write(to_json) } 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.
63 64 65 |
# File 'lib/boom/storage/json.rb', line 63 def to_json Yajl::Encoder.encode(to_hash, :pretty => true) end |