Class: CraftingTable::Search::NameSearch

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

Overview

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

Author:

Since:

  • 0.3

Direct Known Subclasses

FuzzyNameSearch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ NameSearch

Create a new NameSearch

Parameters:

  • name (String)

    Name for which to filter.

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

    Options hash which influences the filtering.

Options Hash (options):

  • :case_sensitive (Boolean) — default: true

    Whether to filter case-sensitively.

Since:

  • 0.3



20
21
22
23
# File 'lib/crafting_table/search/name_search.rb', line 20

def initialize(name, options = {})
  @name = name
  @case_sensitive = options.fetch(:case_sensitive, true)
end

Instance Attribute Details

#case_sensitiveObject (readonly)

Since:

  • 0.3



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

def case_sensitive
  @case_sensitive
end

#nameObject (readonly)

Since:

  • 0.3



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

def name
  @name
end

Instance Method Details

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

Compare two searches for equality.

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

Parameters:

  • other (NameSearch)

    NameSearch which to compare for equality.

Returns:

  • (Boolean)

    Whether two searches are equal.

Since:

  • 0.3



61
62
63
# File 'lib/crafting_table/search/name_search.rb', line 61

def ==(other)
  other.name == name && other.case_sensitive == case_sensitive
end

#apply_to(collection) ⇒ Array<Item, Recipe>

Apply this filter to a collection of items or recipes.

Parameters:

  • collection (Array<Item, Recipe>)

    Collection of items and recipes which to filter.

Returns:

  • (Array<Item, Recipe>)

    Items and recipes which matched the search criteria.

Since:

  • 0.3



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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?Boolean

Returns:

  • (Boolean)

Since:

  • 0.3



29
30
31
# File 'lib/crafting_table/search/name_search.rb', line 29

def case_sensitive?
  @case_sensitive
end

#exact?Boolean

Returns:

  • (Boolean)

Since:

  • 0.3



25
26
27
# File 'lib/crafting_table/search/name_search.rb', line 25

def exact?
  true
end