class CraftingTable::Search::NameSearch

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

@author Michael Senn <morrolan@morrolan.ch> @since 0.3

Attributes

case_sensitive[R]
name[R]

Public Class Methods

new(name, options = {}) click to toggle source

Create a new NameSearch

@param [String] name Name for which to filter. @param [Hash] options Options hash which influences the filtering. @option options [Boolean] :case_sensitive (true)

Whether to filter case-sensitively.
# File lib/crafting_table/search/name_search.rb, line 20
def initialize(name, options = {})
  @name = name
  @case_sensitive = options.fetch(:case_sensitive, true)
end

Public Instance Methods

==(other) click to toggle source

Compare two searches for equality.

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

@param [NameSearch] other NameSearch which to compare for equality. @return [Boolean] Whether two searches are equal.

# File lib/crafting_table/search/name_search.rb, line 61
def ==(other)
  other.name == name && other.case_sensitive == case_sensitive
end
Also aliased as: eq?
apply_to(collection) click to toggle source

Apply this filter to a collection of items or recipes.

@param [Array<Item, Recipe>] collection

Collection of items and recipes which to filter.

@return [Array<Item, Recipe>]

Items and recipes which matched the search criteria.
# File lib/crafting_table/search/name_search.rb, line 39
def apply_to(collection)
  if case_sensitive?
    if exact?
      collection.select { |item| item.name == name }
    else
      collection.select { |item| item.name.include? name }
    end
  else
    if exact?
      collection.select { |item| item.name.downcase == name.downcase }
    else
      collection.select { |item| item.name.downcase.include? name.downcase }
    end
  end
end
case_sensitive?() click to toggle source
# File lib/crafting_table/search/name_search.rb, line 29
def case_sensitive?
  @case_sensitive
end
eq?(other)
Alias for: ==
exact?() click to toggle source
# File lib/crafting_table/search/name_search.rb, line 25
def exact?
  true
end