Class: Minisphinx::Source
- Inherits:
-
Object
- Object
- Minisphinx::Source
- Defined in:
- lib/minisphinx.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#delta_field ⇒ Object
readonly
Returns the value of attribute delta_field.
-
#fetch_key ⇒ Object
readonly
Returns the value of attribute fetch_key.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #configuration(opts) ⇒ Object
-
#initialize(name, opts) ⇒ Source
constructor
A new instance of Source.
- #sql_query ⇒ Object
- #sql_query_delta(delta_min) ⇒ Object
- #sql_query_info ⇒ Object
- #sql_query_range ⇒ Object
- #table_name ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(name, opts) ⇒ Source
Returns a new instance of Source.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/minisphinx.rb', line 40 def initialize(name, opts) @name = name @delta_field = opts.delete(:delta_field) @delta_min = opts.delete(:delta_min) @fetch_key = opts.delete(:fetch_key) || 'id' @fields = initialize_fields(opts) @joins = Array(opts.delete(:joins)) + Array(opts.delete(:join)) (opts.delete(:include) || []).each do |include_opts| @fields.concat initialize_fields(include_opts) @joins.concat Array(include_opts.delete(:joins)) + Array(include_opts.delete(:join)) end raise 'at least one field required' if @fields.empty? @fields.sort! @model_class = opts.delete(:model_class) @table_name = opts.delete(:table_name) @db = opts.delete(:db) || model_class.connection.config @db = ActiveRecord::Base.configurations["#{db}_#{RAILS_ENV}"] unless db.kind_of?(Hash) @config = self.class.config.merge(opts) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
38 39 40 |
# File 'lib/minisphinx.rb', line 38 def config @config end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
38 39 40 |
# File 'lib/minisphinx.rb', line 38 def db @db end |
#delta_field ⇒ Object (readonly)
Returns the value of attribute delta_field.
38 39 40 |
# File 'lib/minisphinx.rb', line 38 def delta_field @delta_field end |
#fetch_key ⇒ Object (readonly)
Returns the value of attribute fetch_key.
38 39 40 |
# File 'lib/minisphinx.rb', line 38 def fetch_key @fetch_key end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
38 39 40 |
# File 'lib/minisphinx.rb', line 38 def fields @fields end |
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
38 39 40 |
# File 'lib/minisphinx.rb', line 38 def joins @joins end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
38 39 40 |
# File 'lib/minisphinx.rb', line 38 def model_class @model_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
38 39 40 |
# File 'lib/minisphinx.rb', line 38 def name @name end |
Class Method Details
.config ⇒ Object
117 118 119 120 121 122 |
# File 'lib/minisphinx.rb', line 117 def self.config @config ||= { :range_step => 5000, :ranged_throttle => 0, } end |
Instance Method Details
#configuration(opts) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/minisphinx.rb', line 71 def configuration(opts) str = Minisphinx.config_block("source #{name}_base", [ "type = #{type}", config.collect do |key, value| "sql_#{key} = #{value}" end, "sql_db = #{db[:database]}", "sql_host = #{db[:host]}", "sql_pass = #{db[:password]}", "sql_user = #{db[:username]}", "sql_query_info = #{sql_query_info}", fields.collect do |field| "sql_attr_#{field.type} = #{field.name}" if field.type != :text end, ]) str << Minisphinx.config_block("source #{name} : #{name}_base", [ "sql_query_range = #{sql_query_range}", "sql_query = #{sql_query}", ]) if delta_field str << Minisphinx.config_block("source #{name}_delta : #{name}_base", [ "sql_query = #{sql_query_delta(opts[:delta_min])}", ]) end str end |
#sql_query ⇒ Object
102 103 104 105 |
# File 'lib/minisphinx.rb', line 102 def sql_query "SELECT #{table_name}.id AS doc_id, #{fields.join(', ')} " << "FROM #{table_name} #{joins.join(' ')} WHERE #{fetch_key} >= $start AND #{fetch_key} <= $end" end |
#sql_query_delta(delta_min) ⇒ Object
107 108 109 110 111 |
# File 'lib/minisphinx.rb', line 107 def sql_query_delta(delta_min) delta_min = "'#{delta_min.to_s(:db)}'" if delta_min.kind_of?(Time) or delta_min.kind_of?(Date) "SELECT #{table_name}.id AS doc_id, #{fields.join(', ')} " << "FROM #{table_name} #{joins.join(' ')} WHERE #{delta_field} >= #{delta_min}" end |
#sql_query_info ⇒ Object
113 114 115 |
# File 'lib/minisphinx.rb', line 113 def sql_query_info "SELECT * FROM #{table_name} WHERE id = $id" end |
#sql_query_range ⇒ Object
98 99 100 |
# File 'lib/minisphinx.rb', line 98 def sql_query_range "SELECT coalesce(MIN(#{fetch_key}),1)::bigint, coalesce(MAX(#{fetch_key}),1)::bigint FROM #{table_name}" end |
#table_name ⇒ Object
63 64 65 |
# File 'lib/minisphinx.rb', line 63 def table_name @table_name ||= model_class.table_name end |
#type ⇒ Object
67 68 69 |
# File 'lib/minisphinx.rb', line 67 def type db[:adapter] == 'postgresql' ? 'pgsql' : db[:adapter] end |