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
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#default_language ⇒ Object
Returns the value of attribute default_language.
-
#default_mode ⇒ Object
Returns the value of attribute default_mode.
-
#default_order ⇒ Object
Returns the value of attribute default_order.
-
#default_rank ⇒ Object
Returns the value of attribute default_rank.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#klass_module ⇒ Object
Returns the value of attribute klass_module.
-
#options ⇒ Object
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 ⇒ Object
Returns the arguments to be used on the scope.
-
#scope_name ⇒ Object
What is the name of the scope to be added to the model.
Constructor Details
#initialize(klass, attribute, options = {}) ⇒ 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 = @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 ⇒ Object
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 ⇒ Object
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 ⇒ Object
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 ⇒ Object
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 ⇒ Object
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 ⇒ Object
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 ⇒ Object
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 ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8 def 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 " def \#{scope_name}(value\#{scope_args})\n attr = arel_table['\#{attribute}']\n fn = ::Torque::PostgreSQL::FN\n\n lang = language.to_s if !language.is_a?(::Symbol)\n lang ||= arel_table[language.to_s] if has_attribute?(language)\n lang ||= public_send(language) if respond_to?(language)\n\n function = {\n default: :to_tsquery,\n phrase: :phraseto_tsquery,\n plain: :plainto_tsquery,\n web: :websearch_to_tsquery,\n }[mode.to_sym]\n\n raise ::ArgumentError, <<~MSG.squish if lang.blank?\n Unable to determine language from \\\#{language.inspect}.\n MSG\n\n raise ::ArgumentError, <<~MSG.squish if function.nil?\n Invalid mode \\\#{mode.inspect} for full text search.\n MSG\n\n value = fn.bind(:value, value.to_s, attr.type_caster)\n lang = fn.bind(:lang, lang, attr.type_caster) if lang.is_a?(::String)\n\n query = fn.public_send(function, lang, value)\n ranker = fn.ts_rank(attr, query) if rank || order\n\n result = where(fn.infix(:\"@@\", attr, query))\n result = result.order(ranker.public_send(order == :desc ? :desc : :asc)) if order\n result.select_extra_values += [ranker.as(rank == true ? 'rank' : rank.to_s)] if rank\n result\n end\n RUBY\nend\n", __FILE__, __LINE__ + 1 |
#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 ⇒ Object
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 ⇒ Object
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 |