Class: Boom::List

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ List

Public: creates a new List instance in-memory.

name - The name of the List. Fails if already used.

Returns the unpersisted List instance.



15
16
17
18
# File 'lib/kaboom/list.rb', line 15

def initialize(name)
  @items = []
  @name  = name
end

Instance Attribute Details

#itemsObject

Public: lets you access the array of items contained within this List.

Returns an Array of Items.



30
31
32
# File 'lib/kaboom/list.rb', line 30

def items
  @items
end

#nameObject

Public: the name of the List.

Returns the String name.



35
36
37
# File 'lib/kaboom/list.rb', line 35

def name
  @name
end

Class Method Details

.delete(name) ⇒ Object

Public: deletes a List by name.

name - String name of the list to delete

Returns whether one or more lists were removed.



62
63
64
65
66
# File 'lib/kaboom/list.rb', line 62

def self.delete(name)
  previous = storage.lists.size
  storage.lists = storage.lists.reject { |list| list.name == name }
  previous != storage.lists.size
end

.find(name) ⇒ Object

Public: finds any given List by name.

name - String name of the list to search for

Returns the first instance of List that it finds.



53
54
55
# File 'lib/kaboom/list.rb', line 53

def self.find(name)
  storage.lists.find { |list| list.name == name }
end

.storageObject

Public: accesses the in-memory JSON representation.

Returns a Storage instance.



23
24
25
# File 'lib/kaboom/list.rb', line 23

def self.storage
  Boom.storage
end

Instance Method Details

#add_item(item) ⇒ Object

Public: associates an Item with this List. If the item name is already defined, then the value will be replaced

item - the Item object to associate with this List.

Returns the current set of items.



43
44
45
46
# File 'lib/kaboom/list.rb', line 43

def add_item(item)
  delete_item(item.name) if find_item(item.name)
  @items << item
end

#delete_item(name) ⇒ Object

Public: deletes an Item by name.

name - String name of the item to delete

Returns whether an item was removed.



73
74
75
76
77
# File 'lib/kaboom/list.rb', line 73

def delete_item(name)
  previous = items.size
  items.reject! { |item| item.name == name}
  previous != items.size
end

#find_item(name) ⇒ Object

Public: finds an Item by name. If the name is typically truncated, also allow a search based on that truncated name.

name - String name of the Item to find

Returns the found item



85
86
87
88
89
90
# File 'lib/kaboom/list.rb', line 85

def find_item(name)
  items.find do |item|
    item.name == name ||
    item.short_name.gsub('','') == name.gsub('','')
  end
end

#to_hashObject

Public: a Hash representation of this List.

Returns a Hash of its own data and its child Items.



95
96
97
# File 'lib/kaboom/list.rb', line 95

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