Class: Pursuit::TermSearch

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(default_table: nil, &block) ⇒ TermSearch

Creates a new term search instance.

Parameters:

  • default_table (Arel::Table) (defaults to: nil)

    The default table to retrieve attributes from.

  • block (Proc)

    The proc to invoke in the search instance (optional).


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

#attributesSet<Arel::Attributes::Attribute>

Returns The attributes to match against.

Returns:

  • (Set<Arel::Attributes::Attribute>)

    The attributes to match against.


13
14
15
# File 'lib/pursuit/term_search.rb', line 13

def attributes
  @attributes
end

#default_tableArel::Table

Returns The default table to retrieve attributes from.

Returns:

  • (Arel::Table)

    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`.

Parameters:

  • query (String)

    The term query.

  • relation (ActiveRecord::Relation)

    The base relation to apply the term clauses to.

Returns:

  • (ActiveRecord::Relation)

    The base relation with the term clauses applied.


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.

Parameters:

  • query (String)

    The term query.

Returns:

  • (Arel::Nodes::Node)

    The ARel node representing the term query.


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

#parserPursuit::TermParser

Returns The parser which converts queries into trees.

Returns:


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.

Parameters:

  • attribute (Arel::Attributes::Attribute, Symbol)

    The underlying attribute to query.

Returns:

  • (Arel::Attributes::Attribute)

    The underlying attribute to query.


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

#transformPursuit::TermTransform

Returns The transform which converts trees into ARel nodes.

Returns:


39
40
41
# File 'lib/pursuit/term_search.rb', line 39

def transform
  @transform ||= TermTransform.new
end