Class: TrajectPlus::Extraction::TransformPipeline

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/traject_plus/extraction.rb

Overview

Pipeline for transforming extracted values into normalized values

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/traject_plus/extraction.rb', line 17

def options
  @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)
  options.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