Class: Searchgasm::Condition::Base
- Inherits:
-
Object
- Object
- Searchgasm::Condition::Base
- Includes:
- Shared::Utilities
- Defined in:
- lib/searchgasm/condition/base.rb
Overview
Conditions condition
The base class for creating a condition. Your custom conditions should extend this class. See Searchgasm::Conditions::Base.register_condition on how to write your own condition.
Direct Known Subclasses
BeginsWith, Contains, DoesNotEqual, EndsWith, Equals, GreaterThan, GreaterThanOrEqualTo, IsBlank, IsNil, Keywords, LessThan, LessThanOrEqualTo, Tree
Instance Attribute Summary collapse
-
#column ⇒ Object
Returns the value of attribute column.
-
#klass ⇒ Object
Returns the value of attribute klass.
Class Method Summary collapse
-
.aliases_for_column(column) ⇒ Object
Alias methods for the column condition.
-
.aliases_for_klass(klass) ⇒ Object
Alias methods for the klass condition.
-
.comparable_column?(column) ⇒ Boolean
A utility method for using in name_for_column.
-
.condition_name ⇒ Object
Name of the condition inferred from the class name.
-
.ignore_blanks? ⇒ Boolean
:nodoc:.
-
.name_for_column(column) ⇒ Object
I pass you a column you tell me what to call the condition.
-
.name_for_klass(klass) ⇒ Object
Sane as name_for_column but for the class as a whole.
-
.string_column?(column) ⇒ Boolean
A utility method for using in name_for_column.
-
.type_cast_value? ⇒ Boolean
:nodoc:.
Instance Method Summary collapse
-
#condition_name ⇒ Object
A convenience method for the name of this condition.
-
#explicitly_set_value=(value) ⇒ Object
Allows nils to be meaninful values.
-
#explicitly_set_value? ⇒ Boolean
Need this if someone wants to actually use nil in a meaningful way.
-
#initialize(klass, column = nil) ⇒ Base
constructor
A new instance of Base.
-
#name ⇒ Object
A convenience method for the name of the method for that specific column or klass.
-
#quote_column_name(column_name) ⇒ Object
Quotes a column name properly for sql.
-
#quote_table_name(table_name) ⇒ Object
Quotes a table name properly for sql.
-
#quoted_column_name ⇒ Object
A convenience method for using when writing your sql in to_conditions.
-
#quoted_table_name ⇒ Object
A convenience method for using when writing your sql in to_conditions.
-
#sanitize(alt_value = nil) ⇒ Object
You should refrain from overwriting this method, it performs various tasks before callign your to_conditions method, allowing you to keep to_conditions simple.
-
#value ⇒ Object
The value for the condition.
-
#value=(v) ⇒ Object
Sets the value for the condition.
Constructor Details
#initialize(klass, column = nil) ⇒ Base
Returns a new instance of Base.
62 63 64 65 |
# File 'lib/searchgasm/condition/base.rb', line 62 def initialize(klass, column = nil) self.klass = klass self.column = column.is_a?(String) ? klass.columns_hash[column] : column end |
Instance Attribute Details
#column ⇒ Object
Returns the value of attribute column.
10 11 12 |
# File 'lib/searchgasm/condition/base.rb', line 10 def column @column end |
#klass ⇒ Object
Returns the value of attribute klass.
10 11 12 |
# File 'lib/searchgasm/condition/base.rb', line 10 def klass @klass end |
Class Method Details
.aliases_for_column(column) ⇒ Object
Alias methods for the column condition.
28 29 30 |
# File 'lib/searchgasm/condition/base.rb', line 28 def aliases_for_column(column) [] end |
.aliases_for_klass(klass) ⇒ Object
Alias methods for the klass condition
47 48 49 |
# File 'lib/searchgasm/condition/base.rb', line 47 def aliases_for_klass(klass) [] end |
.comparable_column?(column) ⇒ Boolean
A utility method for using in name_for_column. For example you wouldn’t want a string column to use the greater thann condition, but you would for an integer column.
57 58 59 |
# File 'lib/searchgasm/condition/base.rb', line 57 def comparable_column?(column) [:integer, :float, :decimal, :datetime, :timestamp, :time, :date].include?(column.type) end |
.condition_name ⇒ Object
Name of the condition inferred from the class name
17 18 19 |
# File 'lib/searchgasm/condition/base.rb', line 17 def condition_name name.split("::").last.gsub(/Condition$/, "").underscore end |
.ignore_blanks? ⇒ Boolean
:nodoc:
32 33 34 |
# File 'lib/searchgasm/condition/base.rb', line 32 def ignore_blanks? # :nodoc: ignore_blanks == true end |
.name_for_column(column) ⇒ Object
I pass you a column you tell me what to call the condition. If you don’t want to use this condition for the column just return nil
23 24 25 |
# File 'lib/searchgasm/condition/base.rb', line 23 def name_for_column(column) "#{column.name}_#{condition_name}" end |
.name_for_klass(klass) ⇒ Object
Sane as name_for_column but for the class as a whole. For example the tree methods apply to the class as a whole and not specific columns. Any condition that applies to columns should probably return nil here.
42 43 44 |
# File 'lib/searchgasm/condition/base.rb', line 42 def name_for_klass(klass) nil end |
.string_column?(column) ⇒ Boolean
A utility method for using in name_for_column. For example the keywords condition only applied to string columns, the great than condition doesnt.
52 53 54 |
# File 'lib/searchgasm/condition/base.rb', line 52 def string_column?(column) [:string, :text].include?(column.type) end |
.type_cast_value? ⇒ Boolean
:nodoc:
36 37 38 |
# File 'lib/searchgasm/condition/base.rb', line 36 def type_cast_value? # :nodoc: type_cast_value == true end |
Instance Method Details
#condition_name ⇒ Object
A convenience method for the name of this condition
83 84 85 |
# File 'lib/searchgasm/condition/base.rb', line 83 def condition_name self.class.condition_name end |
#explicitly_set_value=(value) ⇒ Object
Allows nils to be meaninful values
68 69 70 |
# File 'lib/searchgasm/condition/base.rb', line 68 def explicitly_set_value=(value) @explicitly_set_value = value end |
#explicitly_set_value? ⇒ Boolean
Need this if someone wants to actually use nil in a meaningful way
73 74 75 |
# File 'lib/searchgasm/condition/base.rb', line 73 def explicitly_set_value? @explicitly_set_value == true end |
#name ⇒ Object
A convenience method for the name of the method for that specific column or klass
78 79 80 |
# File 'lib/searchgasm/condition/base.rb', line 78 def name column ? self.class.name_for_column(column) : self.class.name_for_klass(klass) end |
#quote_column_name(column_name) ⇒ Object
Quotes a column name properly for sql.
88 89 90 |
# File 'lib/searchgasm/condition/base.rb', line 88 def quote_column_name(column_name) klass.connection.quote_column_name(column_name) end |
#quote_table_name(table_name) ⇒ Object
Quotes a table name properly for sql
98 99 100 |
# File 'lib/searchgasm/condition/base.rb', line 98 def quote_table_name(table_name) klass.connection.quote_table_name(table_name) end |
#quoted_column_name ⇒ Object
A convenience method for using when writing your sql in to_conditions. This is the proper way to use a column name in a query for most databases
93 94 95 |
# File 'lib/searchgasm/condition/base.rb', line 93 def quoted_column_name quote_column_name(column.name) end |
#quoted_table_name ⇒ Object
A convenience method for using when writing your sql in to_conditions. This is the proper way to use a table name in a query for most databases
103 104 105 |
# File 'lib/searchgasm/condition/base.rb', line 103 def quoted_table_name quote_table_name(klass.table_name) end |
#sanitize(alt_value = nil) ⇒ Object
You should refrain from overwriting this method, it performs various tasks before callign your to_conditions method, allowing you to keep to_conditions simple.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/searchgasm/condition/base.rb', line 108 def sanitize(alt_value = nil) # :nodoc: return unless explicitly_set_value? v = alt_value || value if v.is_a?(Array) && !["equals", "does_not_equal"].include?(condition_name) merge_conditions(*v.collect { |i| sanitize(i) }) else v = v.utc if column && [:time, :timestamp, :datetime].include?(column.type) && klass.time_zone_aware_attributes && !klass.skip_time_zone_conversion_for_attributes.include?(column.name.to_sym) to_conditions(v) end end |
#value ⇒ Object
The value for the condition
120 121 122 |
# File 'lib/searchgasm/condition/base.rb', line 120 def value self.class.type_cast_value? && @value.is_a?(String) ? column.type_cast(@value) : @value end |
#value=(v) ⇒ Object
Sets the value for the condition
125 126 127 128 |
# File 'lib/searchgasm/condition/base.rb', line 125 def value=(v) self.explicitly_set_value = true @value = v end |