Class: TsVectors::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/ts_vectors/attribute.rb

Constant Summary collapse

FORMATS =
[:text, :tsvector]

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  options.assert_valid_keys(:normalize, :configuration, :format)
  @name, @options = name, options
  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

#configurationObject (readonly)

Returns the value of attribute configuration.



82
83
84
# File 'lib/ts_vectors/attribute.rb', line 82

def configuration
  @configuration
end

#formatObject (readonly)

Returns the value of attribute format.



81
82
83
# File 'lib/ts_vectors/attribute.rb', line 81

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



79
80
81
# File 'lib/ts_vectors/attribute.rb', line 79

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



80
81
82
# File 'lib/ts_vectors/attribute.rb', line 80

def options
  @options
end

Instance Method Details

#format_operandObject



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_queryObject



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