Class: Pursuit::TermSearch
- Inherits:
-
Object
- Object
- Pursuit::TermSearch
- Defined in:
- lib/pursuit/term_search.rb
Overview
Provides an interface for declaring which attributes should be searched in a term query, and a method for applying a term query to an ‘ActiveRecord::Relation` instance.
Instance Attribute Summary collapse
-
#attributes ⇒ Set<Arel::Attributes::Attribute>
The attributes to match against.
-
#default_table ⇒ Arel::Table
The default table to retrieve attributes from.
Instance Method Summary collapse
-
#apply(query, relation) ⇒ ActiveRecord::Relation
Applies the term clauses derived from ‘query` to `relation`.
-
#initialize(default_table: nil, &block) ⇒ TermSearch
constructor
Creates a new term search instance.
-
#parse(query) ⇒ Arel::Nodes::Node
Parse a term query into an ARel node.
-
#parser ⇒ Pursuit::TermParser
The parser which converts queries into trees.
-
#search_attribute(attribute) ⇒ Arel::Attributes::Attribute
Adds an attribute to match against in term queries.
-
#transform ⇒ Pursuit::TermTransform
The transform which converts trees into ARel nodes.
Constructor Details
#initialize(default_table: nil, &block) ⇒ TermSearch
Creates a new term search instance.
24 25 26 27 28 29 |
# File 'lib/pursuit/term_search.rb', line 24 def initialize(default_table: nil, &block) @attributes = Set.new @default_table = default_table instance_eval(&block) if block end |
Instance Attribute Details
#attributes ⇒ Set<Arel::Attributes::Attribute>
Returns The attributes to match against.
13 14 15 |
# File 'lib/pursuit/term_search.rb', line 13 def attributes @attributes end |
#default_table ⇒ Arel::Table
Returns The default table to retrieve attributes from.
17 18 19 |
# File 'lib/pursuit/term_search.rb', line 17 def default_table @default_table end |
Instance Method Details
#apply(query, relation) ⇒ ActiveRecord::Relation
Applies the term clauses derived from ‘query` to `relation`.
69 70 71 72 |
# File 'lib/pursuit/term_search.rb', line 69 def apply(query, relation) node = parse(query) node ? relation.where(node) : relation.none end |
#parse(query) ⇒ Arel::Nodes::Node
Parse a term query into an ARel node.
58 59 60 61 |
# File 'lib/pursuit/term_search.rb', line 58 def parse(query) tree = parser.parse(query) transform.apply(tree, attributes: attributes) end |
#parser ⇒ Pursuit::TermParser
Returns The parser which converts queries into trees.
33 34 35 |
# File 'lib/pursuit/term_search.rb', line 33 def parser @parser ||= TermParser.new end |
#search_attribute(attribute) ⇒ Arel::Attributes::Attribute
Adds an attribute to match against in term queries.
48 49 50 51 |
# File 'lib/pursuit/term_search.rb', line 48 def search_attribute(attribute) attribute = default_table[attribute] if attribute.is_a?(Symbol) attributes.add(attribute) end |
#transform ⇒ Pursuit::TermTransform
Returns The transform which converts trees into ARel nodes.
39 40 41 |
# File 'lib/pursuit/term_search.rb', line 39 def transform @transform ||= TermTransform.new end |