Class: CraftingTable::Search::NameSearch
- Inherits:
-
Object
- Object
- CraftingTable::Search::NameSearch
- Defined in:
- lib/crafting_table/search/name_search.rb
Overview
A class which allows to filter recipes and items by their name.
Direct Known Subclasses
Instance Attribute Summary collapse
- #case_sensitive ⇒ Object readonly
- #name ⇒ Object readonly
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eq?)
Compare two searches for equality.
-
#apply_to(collection) ⇒ Array<Item, Recipe>
Apply this filter to a collection of items or recipes.
- #case_sensitive? ⇒ Boolean
- #exact? ⇒ Boolean
-
#initialize(name, options = {}) ⇒ NameSearch
constructor
Create a new NameSearch.
Constructor Details
#initialize(name, options = {}) ⇒ NameSearch
Create a new NameSearch
20 21 22 23 |
# File 'lib/crafting_table/search/name_search.rb', line 20 def initialize(name, = {}) @name = name @case_sensitive = .fetch(:case_sensitive, true) end |
Instance Attribute Details
#case_sensitive ⇒ Object (readonly)
12 13 14 |
# File 'lib/crafting_table/search/name_search.rb', line 12 def case_sensitive @case_sensitive end |
#name ⇒ Object (readonly)
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.
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.
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
29 30 31 |
# File 'lib/crafting_table/search/name_search.rb', line 29 def case_sensitive? @case_sensitive end |
#exact? ⇒ Boolean
25 26 27 |
# File 'lib/crafting_table/search/name_search.rb', line 25 def exact? true end |