Class: CraftingTable::Search::ItemIDSearch

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

Overview

A class which allows to filter recipes and items by their item ID.

Author:

Since:

  • 0.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_id) ⇒ ItemIDSearch

Create a new ItemIDSearch

Parameters:

  • item_id (Fixnum, Range)

    Item ID for which to search.

Since:

  • 0.3



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

def initialize(item_id)
  @item_id = item_id
end

Instance Attribute Details

#item_idObject (readonly)

Since:

  • 0.3



12
13
14
# File 'lib/crafting_table/search/item_id_search.rb', line 12

def item_id
  @item_id
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eq?

Compare two searches for equality.

They are considered equal if the item ID for which they filter is equal.

Parameters:

  • other (ItemIDSearch)

    ItemIDSearch which to compare for equality.

Returns:

  • (Boolean)

    Whether two searches are equal.

Since:

  • 0.3



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

def ==(other)
  other.item_id == item_id
end

#apply_to(items) ⇒ Array<Item>

Apply this filter to a collection of items.

Parameters:

  • items (Array<Item>)

    Collection of items which to filter.

Returns:

  • (Array<Item>)

    Items which matched the search criteria

Since:

  • 0.3



27
28
29
30
31
32
33
# File 'lib/crafting_table/search/item_id_search.rb', line 27

def apply_to(items)
  if item_id.respond_to?(:include?)
    items.select { |item| item_id.include? item.item_id }
  else
    items.select { |item| item_id == item.item_id }
  end
end