Class: CraftingTable::ItemManager
- Inherits:
-
Object
- Object
- CraftingTable::ItemManager
- Defined in:
- lib/crafting_table/item_manager.rb
Overview
A class which contains items, and allows to search through them.
Instance Attribute Summary collapse
- #items ⇒ Object readonly
Instance Method Summary collapse
-
#add(item) ⇒ void
Add a new item to the internal collection.
-
#add_from_file(path) ⇒ void
Add new items by reading them from a YAML file.
-
#clear ⇒ void
Clear the internal collection of items.
-
#find {|builder| ... } ⇒ Array<Item>
Find items.
-
#find_by_damage_value(damage) ⇒ Array<Item>
deprecated
Deprecated.
Use #find instead
-
#find_by_identifier(identifier) ⇒ Array<Item>
Find items by their identifier.
-
#find_by_item_id(id) ⇒ Array<Item>
deprecated
Deprecated.
Use #find instead
-
#find_by_name(name, options = {}) ⇒ Array<Item>
deprecated
Deprecated.
Use #find instead
-
#initialize(items = []) ⇒ ItemManager
constructor
Create a new ItemManager.
Constructor Details
#initialize(items = []) ⇒ ItemManager
Create a new ItemManager
17 18 19 |
# File 'lib/crafting_table/item_manager.rb', line 17 def initialize(items = []) @items = items.to_ary end |
Instance Attribute Details
#items ⇒ Object (readonly)
12 13 14 |
# File 'lib/crafting_table/item_manager.rb', line 12 def items @items end |
Instance Method Details
#add(item) ⇒ void
This method returns an undefined value.
Add a new item to the internal collection.
25 26 27 |
# File 'lib/crafting_table/item_manager.rb', line 25 def add(item) @items << item end |
#add_from_file(path) ⇒ void
This method returns an undefined value.
Add new items by reading them from a YAML file.
33 34 35 36 37 |
# File 'lib/crafting_table/item_manager.rb', line 33 def add_from_file(path) YAML.load_file(path).each do |hash| add(Item.new(hash['name'], hash['id'], hash['damage'])) end end |
#clear ⇒ void
This method returns an undefined value.
Clear the internal collection of items.
41 42 43 |
# File 'lib/crafting_table/item_manager.rb', line 41 def clear @items.clear end |
#find {|builder| ... } ⇒ Array<Item>
Find items.
126 127 128 129 130 131 |
# File 'lib/crafting_table/item_manager.rb', line 126 def find(&block) builder = Search::SearchBuilder.new yield builder builder.searches.inject(items) { |items, search| search.apply_to(items) } end |
#find_by_damage_value(damage) ⇒ Array<Item>
Use #find instead
Find items by their damage value.
148 149 150 151 152 153 154 |
# File 'lib/crafting_table/item_manager.rb', line 148 def find_by_damage_value(damage) if damage.is_a?(Symbol) || !damage.respond_to?(:include?) items.select { |item| item.damage_value == damage } else items.select { |item| damage.include? item.damage_value } end end |
#find_by_identifier(identifier) ⇒ Array<Item>
Find items by their identifier.
search.
165 166 167 |
# File 'lib/crafting_table/item_manager.rb', line 165 def find_by_identifier(identifier) items.select { |item| item.identifier == identifier } end |
#find_by_item_id(id) ⇒ Array<Item>
Use #find instead
Find items by their ID.
94 95 96 97 98 99 100 |
# File 'lib/crafting_table/item_manager.rb', line 94 def find_by_item_id(id) if id.respond_to? :include? items.select { |item| id.include? item.item_id } else items.select { |item| item.item_id == id } end end |
#find_by_name(name, options = {}) ⇒ Array<Item>
Use #find instead
Find items by their name.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/crafting_table/item_manager.rb', line 60 def find_by_name(name, = {}) = { exact: true, case_sensitive: true } .update() if [:case_sensitive] if [:exact] items.select { |item| item.name == name } else items.select { |item| item.name.include? name } end else if [:exact] items.select { |item| item.name.downcase == name.downcase } else items.select { |item| item.name.downcase.include? name.downcase } end end end |