Class: Boom::Storage

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStorage

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

#listsObject

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

#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.



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.

Returns:

  • (Boolean)


62
63
64
# File 'lib/boom/storage.rb', line 62

def item_exists?(name)
  items.detect { |item| item.name == name }
end

#itemsObject

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_fileObject

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.

Returns:

  • (Boolean)


46
47
48
# File 'lib/boom/storage.rb', line 46

def list_exists?(name)
  @lists.detect { |list| list.name == name }
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.



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

#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.



112
113
114
# File 'lib/boom/storage.rb', line 112

def save
  File.open(json_file, 'w') {|f| f.write(to_json) }
end

#to_hashObject

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_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.



120
121
122
# File 'lib/boom/storage.rb', line 120

def to_json
  Yajl::Encoder.encode(to_hash, :pretty => true)
end