Class: Ultrasphinx::Fields
- Includes:
- Singleton
- Defined in:
- lib/ultrasphinx/fields.rb
Instance Method Summary collapse
- #cast(source_string, field) ⇒ Object
- #check_type_match(field, new_type) ⇒ Object
- #configure(configuration) ⇒ Object
-
#initialize ⇒ Fields
constructor
A new instance of Fields.
- #null(field) ⇒ Object
Methods inherited from Hash
Constructor Details
#initialize ⇒ Fields
Returns a new instance of Fields.
9 10 11 12 |
# File 'lib/ultrasphinx/fields.rb', line 9 def initialize self["class_id"] = "numeric" self["class"] = "text" end |
Instance Method Details
#cast(source_string, field) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/ultrasphinx/fields.rb', line 24 def cast(source_string, field) if self[field] == "date" "UNIX_TIMESTAMP(#{source_string})" elsif source_string =~ /GROUP_CONCAT/ "CAST(#{source_string} AS CHAR)" else source_string end + " AS #{field}" end |
#check_type_match(field, new_type) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/ultrasphinx/fields.rb', line 14 def check_type_match(field, new_type) # tries to smoosh fields together by name in the sphinx query schema; raises if their types don't match field, new_type = field.to_s, COLUMN_TYPES[new_type.to_sym] if self[field] raise ConfigurationError, "Column type mismatch for #{field.inspect}; was already #{self[field].inspect}, but is now #{new_type.inspect}." unless self[field] == new_type else self[field] = new_type end end |
#configure(configuration) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ultrasphinx/fields.rb', line 45 def configure(configuration) configuration.each do |model, | klass = model.constantize begin # fields are from the model [:fields].to_a.each do |entry| entry = {:field => entry, :as => entry} unless entry.is_a? Hash unless klass.columns_hash[entry[:field]] ActiveRecord::Base.logger.warn "ultrasphinx: WARNING: field #{entry[:field]} is not present in #{model}" else check_type_match(entry[:as], klass.columns_hash[entry[:field]].type) end end # joins are whatever they are in the target [:includes].to_a.each do |join| check_type_match(join[:as] || join[:field], join[:model].constantize.columns_hash[join[:field]].type) end # regular concats are CHAR (I think), group_concats are BLOB and need to be cast to CHAR, e.g. :text [:concats].to_a.each do |concats| check_type_match(concats[:as], :text) end rescue ActiveRecord::StatementInvalid ActiveRecord::Base.logger.warn "ultrasphinx: WARNING: model #{model} does not exist in the database yet" end end self end |
#null(field) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ultrasphinx/fields.rb', line 34 def null(field) case self[field] when 'text' "''" when 'numeric' "0" when 'date' "UNIX_TIMESTAMP('1970-01-01 00:00:00')" end + " AS #{field}" end |