Class: CraftingTable::ItemManager

Inherits:
Object
  • Object
show all
Defined in:
lib/crafting_table/item_manager.rb

Overview

A class which contains items, and allows to search through them.

Author:

Since:

  • 0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = []) ⇒ ItemManager

Create a new ItemManager

Parameters:

  • items (Array) (defaults to: [])

    Array of items with which to initialize the item manager.

Since:

  • 0.1



17
18
19
# File 'lib/crafting_table/item_manager.rb', line 17

def initialize(items = [])
  @items = items.to_ary
end

Instance Attribute Details

#itemsObject (readonly)

Since:

  • 0.1



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.

Parameters:

  • item (Item)

    Item which to add to the collection.

Since:

  • 0.1



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.

Parameters:

  • path (String)

    The path to the file from which to read items from.

Since:

  • 0.1



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

#clearvoid

This method returns an undefined value.

Clear the internal collection of items.

Since:

  • 0.1



41
42
43
# File 'lib/crafting_table/item_manager.rb', line 41

def clear
  @items.clear
end

#find {|builder| ... } ⇒ Array<Item>

Find items.

Examples:

Searching for name and damage value

results = manager.find do |search|
  search.name = 'Wood'
  search.exact = false
  search.damage_value = 3
 end
 results.first.name #=> 'Jungle Wood'

Searching for range of item IDs and two specific damage values

results = manager.find do |search|
  search.item_id = 1..20
  search.damage_value = [1, 3]
end
results.map(&:name) #=> ['Spruce Sapling', 'Jungle Sapling', 'Spruce Wood', 'Jungle Wood', 'Spruce Leaves', 'Jungle Leaves']

Yield Parameters:

  • builder (SearchBuilder)

    An instance of the SearchBuilder class which allows to easily specify multiple search conditions.

Returns:

  • (Array<Item>)

    Items which matched the search conditions.

Since:

  • 0.3



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>

Deprecated.

Use #find instead

Find items by their damage value.

Examples:

Searching for single damage value.

manager.find_by_damage_value(2).map(&:name) #=> ['Birch Wood', '...']

Searching for range of damage values.

manager.find_by_damage_value(1..2).map(&:name) #=> ['Spruce Wood', 'Birch Wood', '...']

Searching for collection of damage values.

manager.find_by_damage_value([1, 3]).map(&:name) #=> ['Spruce Wood', 'Jungle Wood', '...']

Parameters:

  • id (#include?, Integer)

    A collection of damage values, or a single damage value, for which to search.

Returns:

  • (Array<Item>)

    Collection of items which matched the search condition.

Since:

  • 0.1



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.

Parameters:

  • identifier (Array<Integer>)

    Identifier for which to

Returns:

  • (Array<Item>)

    Collection of items which matched the search condition.

See Also:

Since:

  • 0.2



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>

Deprecated.

Use #find instead

Find items by their ID.

Examples:

Searching for single ID.

manager.find_by_item_id(17).first.name #=> 'Wood'

Searching for range of IDs.

manager.find_by_item_id(14..16).map(&:name) #=> ['Gold Ore', 'Iron Ore', 'Coal Ore']

Searching for collection of IDs.

manager.find_by_item_id([1, 3, 24]).map(&:name) #=> ['Stone', 'Dirt', 'Sandstone']

Parameters:

  • id (#include?, Integer)

    A collection of IDs, or a single ID, for which to search.

Returns:

  • (Array<Item>)

    Collection of items which matched the search condition.

Since:

  • 0.1



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>

Deprecated.

Use #find instead

Find items by their name.

Examples:

Search using default parameters.

manager.find_by_name('Stone').map(&:name) #=> ['Stone']

Case-insensitive search, non-exact matching.

manager.find_by_name('stone', exact: false, case_sensitive: false).map(&:name) #=> ['Stone', 'Sandstone', '...']

Parameters:

  • name (String)

    The name for which to search.

  • options (Hash) (defaults to: {})

    Options which influence the search.

Options Hash (options):

  • :exact (Boolean) — default: true

    Whether to match names exactly.

  • :case_sensitive (Boolean) — default: true

    Whether to search case-sensitively.

Returns:

  • (Array<Item>)

    Collection of items which matched the search condition.

Since:

  • 0.1



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, options = {})
  default_options = { exact: true, case_sensitive: true }
  default_options.update(options)

  if default_options[:case_sensitive]
    if default_options[:exact]
      items.select { |item| item.name == name }
    else
      items.select { |item| item.name.include? name }
    end
  else
    if default_options[:exact]
      items.select { |item| item.name.downcase == name.downcase }
    else
      items.select { |item| item.name.downcase.include? name.downcase }
    end
  end
end