Class: Boom::Storage::Base

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

Direct Known Subclasses

Gist, Json, Keychain, Mongodb, Redis

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Public: initializes a Storage instance by loading in your persisted data from adapter.

Returns the Storage instance.



14
15
16
17
18
19
# File 'lib/kaboom/storage/base.rb', line 14

def initialize
  @lists = []
  bootstrap
  Boom::Remote.allowed? self
  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.



43
44
45
# File 'lib/kaboom/storage/base.rb', line 43

def lists
  @lists.sort_by { |list| -list.items.size }
end

Instance Method Details

#bootstrapObject

run bootstrap tasks for the storage



22
# File 'lib/kaboom/storage/base.rb', line 22

def bootstrap ; end

#handle(error, message) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/kaboom/storage/base.rb', line 81

def handle error, message
  case error
  when NoMethodError
    output cyan config_text
  when NameError
    output message
  end
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)


68
69
70
# File 'lib/kaboom/storage/base.rb', line 68

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

#itemsObject

Public: all Items in storage.

Returns an Array of all Items.



59
60
61
# File 'lib/kaboom/storage/base.rb', line 59

def items
  @lists.collect(&:items).flatten
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)


52
53
54
# File 'lib/kaboom/storage/base.rb', line 52

def list_exists?(name)
  @lists.detect { |list| list.name == name }
end

#populateObject

populate the in-memory store with all the lists and items



25
# File 'lib/kaboom/storage/base.rb', line 25

def populate ; end

#saveObject

save the data



28
# File 'lib/kaboom/storage/base.rb', line 28

def save ; 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.



77
78
79
# File 'lib/kaboom/storage/base.rb', line 77

def to_hash
  { :lists => lists.collect(&:to_hash) }
end