Class: TsVectors::Model::Attribute
- Inherits:
-
Object
- Object
- TsVectors::Model::Attribute
- Defined in:
- lib/ts_vectors/model.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #normalize(value) ⇒ Object
- #normalize_values(values) ⇒ Object
- #parse_values(string) ⇒ Object
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, = {}) .assert_valid_keys(:normalize) @name, @options = name, end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
48 49 50 |
# File 'lib/ts_vectors/model.rb', line 48 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
49 50 51 |
# File 'lib/ts_vectors/model.rb', line 49 def @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 |