Class: TsVectors::Attribute
- Inherits:
-
Object
- Object
- TsVectors::Attribute
- Defined in:
- lib/ts_vectors/attribute.rb
Constant Summary collapse
- FORMATS =
[:text, :tsvector]
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #format_operand ⇒ Object
- #format_query ⇒ Object
-
#initialize(name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #normalize(value) ⇒ Object
- #normalize_values(values) ⇒ Object
- #parse_values(string) ⇒ Object
- #serialize_values(values) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ts_vectors/attribute.rb', line 6 def initialize(name, = {}) .assert_valid_keys(:normalize, :configuration, :format) @name, @options = name, if (config = @options[:configuration]) @configuration = config else @configuration = 'simple' end if (format = @options[:format]) raise ArgumentError, "Unsupported format #{format.inspect}" unless FORMATS.include?(format) @format = format.to_sym else @format = :text end end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
82 83 84 |
# File 'lib/ts_vectors/attribute.rb', line 82 def configuration @configuration end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
81 82 83 |
# File 'lib/ts_vectors/attribute.rb', line 81 def format @format end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
79 80 81 |
# File 'lib/ts_vectors/attribute.rb', line 79 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
80 81 82 |
# File 'lib/ts_vectors/attribute.rb', line 80 def @options end |
Instance Method Details
#format_operand ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ts_vectors/attribute.rb', line 22 def format_operand @operand ||= begin config = @configuration if @format == :text "to_tsvector('#{config}', #{@name})" else @name end end end |
#format_query ⇒ Object
33 34 35 |
# File 'lib/ts_vectors/attribute.rb', line 33 def format_query @query ||= "to_tsquery('#{@configuration}', ?)" end |
#normalize(value) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ts_vectors/attribute.rb', line 62 def normalize(value) if value if (normalize = @options[:normalize]) value = normalize.call(value) else value = value.strip.downcase end if value.blank? nil elsif value =~ /\s/ %('#{value}') else value end end end |
#normalize_values(values) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/ts_vectors/attribute.rb', line 55 def normalize_values(values) values = [values] unless values.is_a?(Enumerable) values = values.map { |v| normalize(v) } values.compact! values end |
#parse_values(string) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/ts_vectors/attribute.rb', line 45 def parse_values(string) if string values = string.scan(/(?:([^'\s,]+(?::\d+)?)|'([^']+)(?::\d+)?')\s*/u).flatten values.reject! { |v| v.blank? } values else [] end end |
#serialize_values(values) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/ts_vectors/attribute.rb', line 37 def serialize_values(values) if values and values.length > 0 values.join(' ') else nil end end |