Class: Specifind::AttributePhrase

Inherits:
Object
  • Object
show all
Defined in:
lib/specifind/attribute_phrase.rb

Overview

The AttributePhrase class is a component of the MethodBuilder -> QueryBuilder -> SQL flow. See MethodBuilder for more detail on how that works. AttributePhrase objects have search parameters specific to the attribute (ie. name of attribute and value(s) associated with it), sometimes a Comparator (specifying how to query those attributes in relation to the provided values), and sometimes an Operator (specifying how to link) this attribute in the query to the next attribute (ie. ‘and’, ‘or’).

Example AttributePhrase:

  • name: :first_name

  • type: :varchar

  • value: ‘Steve’

  • comparator: Comparator matching ‘equals’

  • operator: Operator matching ‘and’

will be used by the QueryBuilder, as delegated by MethodBuilder to construct a sql fragment that will find object ids that have a first_name property equal to ‘Steve’ and link it with whatever AttributePhrase follows with an and Operator.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ AttributePhrase

A new instance of AttributePhrase

set the type of the object to any of (:boolean, :varchar, :datetime, :num). Can take [:value], which sets the value(s) to be queried (per the terms of the comparator). Can take [:comparator] and [:operator]. These values can also be set after creation time but before query building in case the values are not known by the instantiating object

Parameters:

  • args (Hash)

    requires [:name], specifying the name of the class’s attribute that is being searched on. Can also take [:type], which will



46
47
48
49
50
51
52
# File 'lib/specifind/attribute_phrase.rb', line 46

def initialize(args)
  @name = args[:name]
  @type = args[:type] || nil
  @value = args[:value] || nil
  @comparator = Specifind.comparator.find(args[:comparator]) || nil
  @operator = Operator.find(args[:operator]) || nil
end

Instance Attribute Details

#comparatorObject

Returns the value of attribute comparator.



22
23
24
# File 'lib/specifind/attribute_phrase.rb', line 22

def comparator
  @comparator
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/specifind/attribute_phrase.rb', line 22

def name
  @name
end

#operatorObject

Returns the value of attribute operator.



22
23
24
# File 'lib/specifind/attribute_phrase.rb', line 22

def operator
  @operator
end

#typeObject

Returns the value of attribute type.



22
23
24
# File 'lib/specifind/attribute_phrase.rb', line 22

def type
  @type
end

#valueObject

Returns the value of attribute value.



22
23
24
# File 'lib/specifind/attribute_phrase.rb', line 22

def value
  @value
end

Class Method Details

.from_string_and_operator(args) ⇒ Object

Class factory method to build AttributePhrase objects from a String (which contains an attribute name and sometimes a comparator) and an Operator object. If no comparator is included in the string, it is assumed to mean equals. If no operator is included in the args, then the AttributePhrase is assumed to be the terminal part of the query

Parameters:

  • args (Hash)

    requires [:string] and [:operator]. String is of form ‘attribute(_comparator)’ and operator is of type Operator or nil



30
31
32
33
34
35
36
37
# File 'lib/specifind/attribute_phrase.rb', line 30

def self.from_string_and_operator(args)
  raise 'AttributePhrase.from_string_and_operator requires a hash with [:string]' unless args[:string]
  comparator_patterns = Regexp.new Specifind.comparator.patterns.map{|c| c = "(#{c})"}.join '|'
  attribute_with_comparator = args[:string].split comparator_patterns
  name = attribute_with_comparator[0]
  comparator = attribute_with_comparator[1] || '_equals'
  AttributePhrase.new :name => name, :comparator => comparator, :operator => args[:operator]
end

Instance Method Details

#build_comparator_sqlObject



54
55
56
# File 'lib/specifind/attribute_phrase.rb', line 54

def build_comparator_sql
  comparator.build_sql
end

#to_assertionObject

Converts this attribute to a dynamic assertion of type of whatever value is passed to the find_by_* method



90
91
92
# File 'lib/specifind/attribute_phrase.rb', line 90

def to_assertion
  @comparator.to_type_test @name, @type
end

#to_paramsObject

Converts this attribute to the parameters that the search_by method in the Searchable concern takes and passes to QueryBuilder



84
85
86
# File 'lib/specifind/attribute_phrase.rb', line 84

def to_params
  @comparator ? @comparator.to_params(@name) : nil
end

#to_rearrangementObject

rearrange any parameter thats passed to necessary values

example: sql lists are formatted as (val1, val2, val3), while ruby will export (strings) as [‘val1’, ‘val2’, ‘val3’]



98
99
100
# File 'lib/specifind/attribute_phrase.rb', line 98

def to_rearrangement
  @comparator.to_rearrangement @name, @type
end

#to_signatureObject

Converts this attribute to the parameters it requires (based on it’s Comparator) for MethodBuilder.

Example: An AttributePhrase object first_name equals ‘Steve’ will generate it’s portion of the magic method signature search_by_first_name_equals(first_name_val), where first_name_val is the signature

An AttributePhrase object with age between 15 and 25 will generate it’s portion of the magic method signature search_by_age_between(age_bound_one, age_bound_two), where age_bound_one, age_bound_two is it’s signature



77
78
79
# File 'lib/specifind/attribute_phrase.rb', line 77

def to_signature
  @comparator ? @comparator.to_signature(@name) : nil
end

#to_whereObject

Constructs a constructor call for this AttributePhrase object.

Used in a special case by MethodBuilder, which needs to create a block of code (dynamically) to match the method passed. As such, we cannot pass a pointer to the object, so this method dehydrates it with directions for recreating itself.



63
64
65
66
# File 'lib/specifind/attribute_phrase.rb', line 63

def to_where

  ".where(\"#{@name} #{@comparator.to_where(@name, @type == :boolean)}\")"
end