Class: TrajectPlus::Extraction::TransformPipeline
- Inherits:
-
Object
- Object
- TrajectPlus::Extraction::TransformPipeline
- Extended by:
- Deprecation
- Defined in:
- lib/traject_plus/extraction.rb
Overview
Pipeline for transforming extracted values into normalized values
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#append(values, append_string) ⇒ Object
to_field ‘x’, append: ‘321’ # ‘abc’ to ‘abc321’.
-
#default(values, default_value) ⇒ Object
to_field ‘x’, default: ‘y’ # nil => ‘y’.
-
#format(values, insert_string) ⇒ Object
to_field ‘x’, format: ‘-> %s <-’ # ‘abc’ to ‘-> abc <-’.
-
#initialize(options) ⇒ TransformPipeline
constructor
A new instance of TransformPipeline.
-
#match(values, match, index) ⇒ Object
to_field ‘x’, match: [/([aeiou])/, 1] # ‘abc’ => ‘a’.
-
#max(values, count, block = nil) ⇒ Object
to_field ‘x’, max: 1 # [‘a’, ‘b’] => [‘b’].
-
#min(values, count, block = nil) ⇒ Object
to_field ‘x’, min: 1 # [‘a’, ‘b’] => [‘a’].
-
#reject(values, block) ⇒ Object
to_field ‘x’, reject: lambda { |x| x =~ /a/} # [‘a’, ‘b’] => [‘b’].
-
#select(values, block) ⇒ Object
to_field ‘x’, select: lambda { |x| x =~ /a/} # [‘a’, ‘b’] => [‘a’].
- #transform(values) ⇒ Object
-
#translation_map(values, maps) ⇒ Object
Using a named Traject translation map: to_field ‘x’, translation_map: ‘types’ # ‘x’ => ‘mapped x’,.
Constructor Details
#initialize(options) ⇒ TransformPipeline
Returns a new instance of TransformPipeline.
19 20 21 |
# File 'lib/traject_plus/extraction.rb', line 19 def initialize() @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/traject_plus/extraction.rb', line 17 def @options end |
Instance Method Details
#append(values, append_string) ⇒ Object
to_field ‘x’, append: ‘321’ # ‘abc’ to ‘abc321’
62 63 64 65 66 |
# File 'lib/traject_plus/extraction.rb', line 62 def append(values, append_string) values.flat_map do |v| "#{v}#{append_string}" end end |
#default(values, default_value) ⇒ Object
to_field ‘x’, default: ‘y’ # nil => ‘y’
120 121 122 123 124 125 126 |
# File 'lib/traject_plus/extraction.rb', line 120 def default(values, default_value) if values.present? values else default_value end end |
#format(values, insert_string) ⇒ Object
to_field ‘x’, format: ‘-> %s <-’ # ‘abc’ to ‘-> abc <-’
78 79 80 81 82 |
# File 'lib/traject_plus/extraction.rb', line 78 def format(values, insert_string) values.flat_map do |v| insert_string % v end end |
#match(values, match, index) ⇒ Object
to_field ‘x’, match: [/([aeiou])/, 1] # ‘abc’ => ‘a’
69 70 71 72 73 74 75 |
# File 'lib/traject_plus/extraction.rb', line 69 def match(values, match, index) values.flat_map do |v| v.match(match) do |m| m[index] end end end |
#max(values, count, block = nil) ⇒ Object
to_field ‘x’, max: 1 # [‘a’, ‘b’] => [‘b’]
104 105 106 107 108 109 110 |
# File 'lib/traject_plus/extraction.rb', line 104 def max(values, count, block = nil) if block.present? values.max(count) else values.max(count, &block) end end |
#min(values, count, block = nil) ⇒ Object
to_field ‘x’, min: 1 # [‘a’, ‘b’] => [‘a’]
95 96 97 98 99 100 101 |
# File 'lib/traject_plus/extraction.rb', line 95 def min(values, count, block = nil) if block.present? values.min(count) else values.min(count, &block) end end |
#reject(values, block) ⇒ Object
to_field ‘x’, reject: lambda { |x| x =~ /a/} # [‘a’, ‘b’] => [‘b’]
90 91 92 |
# File 'lib/traject_plus/extraction.rb', line 90 def reject(values, block) values.reject(&block) end |
#select(values, block) ⇒ Object
to_field ‘x’, select: lambda { |x| x =~ /a/} # [‘a’, ‘b’] => [‘a’]
85 86 87 |
# File 'lib/traject_plus/extraction.rb', line 85 def select(values, block) values.select(&block) end |
#transform(values) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/traject_plus/extraction.rb', line 23 def transform(values) .inject(values) do |memo, (step, params)| if step.respond_to? :call memo.flat_map { |v| step.call(v, params) } else public_send(step, memo, params) end end end |
#translation_map(values, maps) ⇒ Object
Using a named Traject translation map: to_field ‘x’, translation_map: ‘types’ # ‘x’ => ‘mapped x’,
114 115 116 117 |
# File 'lib/traject_plus/extraction.rb', line 114 def translation_map(values, maps) translation_map = Traject::TranslationMap.new(*Array(maps)) translation_map.translate_array Array(values) end |