Class: Torque::PostgreSQL::Attributes::Builder::FullTextSearch
- Inherits:
-
Object
- Object
- Torque::PostgreSQL::Attributes::Builder::FullTextSearch
- Defined in:
- lib/torque/postgresql/attributes/builder/full_text_search.rb,
sig/torque/postgresql/attributes/builder/full_text_search.rbs
Instance Attribute Summary collapse
-
#attribute ⇒ column_ref
Returns the value of attribute attribute.
-
#default_language ⇒ String, Symbol
Returns the value of attribute default_language.
-
#default_mode ⇒ Symbol
Returns the value of attribute default_mode.
-
#default_order ⇒ :asc, ...
Returns the value of attribute default_order.
-
#default_rank ⇒ String?
Returns the value of attribute default_rank.
-
#klass ⇒ model
Returns the value of attribute klass.
-
#klass_module ⇒ Module?
Returns the value of attribute klass_module.
-
#options ⇒ Hash[Symbol, untyped]
Returns the value of attribute options.
Instance Method Summary collapse
-
#add_scope_to_module ⇒ Object
Creates a class method as the scope that builds the full text search.
-
#build ⇒ Object
Create the proper scope.
-
#conflicting? ⇒ Boolean
Just check if the scope name is already defined.
-
#initialize(klass, attribute, options = {}) ⇒ FullTextSearch
constructor
A new instance of FullTextSearch.
-
#scope_args ⇒ String
Returns the arguments to be used on the scope.
-
#scope_name ⇒ String
What is the name of the scope to be added to the model.
Constructor Details
#initialize(klass, attribute, options = {}) ⇒ FullTextSearch
Returns a new instance of FullTextSearch.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 11 def initialize(klass, attribute, = {}) @klass = klass @attribute = attribute @options = @default_rank = [:with_rank] == true ? 'rank' : [:with_rank]&.to_s @default_mode = [:mode] || PostgreSQL.config.full_text_search.default_mode @default_order = case [:order] when :asc, true then :asc when :desc then :desc else false end @default_language = [:language] if [:language].is_a?(String) || [:language].is_a?(Symbol) @default_language ||= PostgreSQL.config.full_text_search.default_language.to_s end |
Instance Attribute Details
#attribute ⇒ column_ref
Returns the value of attribute attribute.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def attribute @attribute end |
#default_language ⇒ String, Symbol
Returns the value of attribute default_language.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def default_language @default_language end |
#default_mode ⇒ Symbol
Returns the value of attribute default_mode.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def default_mode @default_mode end |
#default_order ⇒ :asc, ...
Returns the value of attribute default_order.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def default_order @default_order end |
#default_rank ⇒ String?
Returns the value of attribute default_rank.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def default_rank @default_rank end |
#klass ⇒ model
Returns the value of attribute klass.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def klass @klass end |
#klass_module ⇒ Module?
Returns the value of attribute klass_module.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def klass_module @klass_module end |
#options ⇒ Hash[Symbol, untyped]
Returns the value of attribute options.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def @options end |
Instance Method Details
#add_scope_to_module ⇒ Object
Creates a class method as the scope that builds the full text search
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 57 def add_scope_to_module klass_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{scope_name}(value#{scope_args}) attr = arel_table['#{attribute}'] fn = ::Torque::PostgreSQL::FN lang = language.to_s if !language.is_a?(::Symbol) lang ||= arel_table[language.to_s] if has_attribute?(language) lang ||= public_send(language) if respond_to?(language) function = { default: :to_tsquery, phrase: :phraseto_tsquery, plain: :plainto_tsquery, web: :websearch_to_tsquery, }[mode.to_sym] raise ::ArgumentError, <<~MSG.squish if lang.blank? Unable to determine language from \#{language.inspect}. MSG raise ::ArgumentError, <<~MSG.squish if function.nil? Invalid mode \#{mode.inspect} for full text search. MSG value = fn.bind(:value, value.to_s, attr.type_caster) lang = fn.bind(:lang, lang, attr.type_caster) if lang.is_a?(::String) query = fn.public_send(function, lang, value) ranker = fn.ts_rank(attr, query) if rank || order result = where(fn.infix(:"@@", attr, query)) result = result.order(ranker.public_send(order == :desc ? :desc : :asc)) if order result.select_extra_values += [ranker.as(rank == true ? 'rank' : rank.to_s)] if rank result end RUBY end |
#build ⇒ Object
Create the proper scope
50 51 52 53 54 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 50 def build @klass_module = Module.new add_scope_to_module klass.extend klass_module end |
#conflicting? ⇒ Boolean
Just check if the scope name is already defined
41 42 43 44 45 46 47 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 41 def conflicting? return if [:force] == true if klass.dangerous_class_method?(scope_name) raise Interrupt, scope_name.to_s end end |
#scope_args ⇒ String
Returns the arguments to be used on the scope
97 98 99 100 101 102 103 104 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 97 def scope_args args = +'' args << ", order: #{default_order.inspect}" args << ", rank: #{default_rank.inspect}" args << ", language: #{default_language.inspect}" args << ", mode: :#{default_mode}" args end |
#scope_name ⇒ String
What is the name of the scope to be added to the model
32 33 34 35 36 37 38 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 32 def scope_name @scope_name ||= [ [:prefix], :full_text_search, [:suffix], ].compact.join('_') end |