Class: Specifind::AttributePhrase
- Inherits:
-
Object
- Object
- Specifind::AttributePhrase
- 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
-
#comparator ⇒ Object
Returns the value of attribute comparator.
-
#name ⇒ Object
Returns the value of attribute name.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #build_comparator_sql ⇒ Object
-
#initialize(args) ⇒ AttributePhrase
constructor
A new instance of AttributePhrase.
-
#to_assertion ⇒ Object
Converts this attribute to a dynamic assertion of type of whatever value is passed to the find_by_* method.
-
#to_params ⇒ Object
Converts this attribute to the parameters that the search_by method in the Searchable concern takes and passes to QueryBuilder.
-
#to_rearrangement ⇒ Object
rearrange any parameter thats passed to necessary values.
-
#to_signature ⇒ Object
Converts this attribute to the parameters it requires (based on it’s Comparator) for MethodBuilder.
-
#to_where ⇒ Object
Constructs a constructor call for this AttributePhrase object.
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
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
#comparator ⇒ Object
Returns the value of attribute comparator.
22 23 24 |
# File 'lib/specifind/attribute_phrase.rb', line 22 def comparator @comparator end |
#name ⇒ Object
Returns the value of attribute name.
22 23 24 |
# File 'lib/specifind/attribute_phrase.rb', line 22 def name @name end |
#operator ⇒ Object
Returns the value of attribute operator.
22 23 24 |
# File 'lib/specifind/attribute_phrase.rb', line 22 def operator @operator end |
#type ⇒ Object
Returns the value of attribute type.
22 23 24 |
# File 'lib/specifind/attribute_phrase.rb', line 22 def type @type end |
#value ⇒ Object
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
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_sql ⇒ Object
54 55 56 |
# File 'lib/specifind/attribute_phrase.rb', line 54 def build_comparator_sql comparator.build_sql end |
#to_assertion ⇒ Object
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_params ⇒ Object
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_rearrangement ⇒ Object
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_signature ⇒ Object
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_where ⇒ Object
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 |