Class: Boom::Storage::Json

Inherits:
Base
  • Object
show all
Defined in:
lib/boom/storage/json.rb

Constant Summary collapse

JSON_FILE =
"#{ENV['HOME']}/.boom"

Instance Attribute Summary

Attributes inherited from Base

#lists

Instance Method Summary collapse

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

#bootstrapObject

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_fileObject

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

#populateObject

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

#saveObject

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_jsonObject

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