Class: TsVectors::Model::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



9
10
11
12
# File 'lib/ts_vectors/model.rb', line 9

def initialize(name, options = {})
  options.assert_valid_keys(:normalize)
  @name, @options = name, options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



48
49
50
# File 'lib/ts_vectors/model.rb', line 48

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



49
50
51
# File 'lib/ts_vectors/model.rb', line 49

def options
  @options
end

Instance Method Details

#normalize(value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ts_vectors/model.rb', line 31

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



24
25
26
27
28
29
# File 'lib/ts_vectors/model.rb', line 24

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



14
15
16
17
18
19
20
21
22
# File 'lib/ts_vectors/model.rb', line 14

def parse_values(string)
  if string
    values = string.scan(/(?:([^'\s,]+)|'([^']+)')\s*/u).flatten
    values.reject! { |v| v.blank? }
    values
  else
    []
  end
end