Module: TrajectPlus::Macros
- Defined in:
- lib/traject_plus/macros.rb,
lib/traject_plus/macros/csv.rb,
lib/traject_plus/macros/tei.rb,
lib/traject_plus/macros/xml.rb,
lib/traject_plus/macros/fgdc.rb,
lib/traject_plus/macros/json.rb,
lib/traject_plus/macros/mods.rb
Defined Under Namespace
Modules: Csv, FGDC, JSON, Mods, Tei, Xml
Instance Method Summary
collapse
-
#accumulate(&block) ⇒ Object
-
#compose(fieldname = nil, aLambda = nil, extract: nil, transform: nil, &block) ⇒ Object
-
#conditional(condition, block) ⇒ Object
only accumulate values if a condition is met.
-
#copy(field) ⇒ Object
-
#first(*macros) ⇒ Object
try a bunch of macros and short-circuit after one returns values.
-
#from_settings(field) ⇒ Object
-
#to_field(field_name, aLambda = nil, extract: nil, transform: nil, **namedArgs, &block) ⇒ Object
-
#to_fields(fields, mapping_method) ⇒ Object
apply the same mapping to multiple fields.
-
#transform(options = {}) ⇒ Object
-
#transform_values(context, hash) ⇒ Object
construct a structured hash using values extracted using traject.
Instance Method Details
#accumulate(&block) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/traject_plus/macros.rb', line 24
def accumulate(&block)
lambda do |record, accumulator, context|
Array(block.call(record, context)).each do |v|
accumulator << v if v.present?
end
end
end
|
#compose(fieldname = nil, aLambda = nil, extract: nil, transform: nil, &block) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/traject_plus/macros.rb', line 69
def compose(fieldname = nil, aLambda = nil, extract: nil, transform: nil, &block)
if fieldname.is_a? Proc
aLambda ||= fieldname
fieldname = nil
end
indexer = self.class.new(settings)
indexer.instance_eval(&block)
@index_steps << TrajectPlus::Indexer::ComposeStep.new(fieldname, || aLambda, transform, Traject::Util.(caller.first), indexer)
end
|
#conditional(condition, block) ⇒ Object
only accumulate values if a condition is met
33
34
35
36
37
38
39
|
# File 'lib/traject_plus/macros.rb', line 33
def conditional(condition, block)
lambda do |record, accumulator, context|
if condition.call(record, context)
block.call(record, accumulator, context)
end
end
end
|
#copy(field) ⇒ Object
47
48
49
50
51
|
# File 'lib/traject_plus/macros.rb', line 47
def copy(field)
accumulate do |_record, context|
Array(context.output_hash[field])
end
end
|
#first(*macros) ⇒ Object
try a bunch of macros and short-circuit after one returns values
16
17
18
19
20
21
22
|
# File 'lib/traject_plus/macros.rb', line 16
def first(*macros)
lambda do |record, accumulator, context|
macros.lazy.map do |block|
block.call(record, accumulator, context)
end.reject(&:blank?).first
end
end
|
#from_settings(field) ⇒ Object
41
42
43
44
45
|
# File 'lib/traject_plus/macros.rb', line 41
def from_settings(field)
accumulate do |record, context|
context.settings.fetch(field)
end
end
|
#to_field(field_name, aLambda = nil, extract: nil, transform: nil, **namedArgs, &block) ⇒ Object
65
66
67
|
# File 'lib/traject_plus/macros.rb', line 65
def to_field(field_name, aLambda = nil, extract: nil, transform: nil, **namedArgs, &block)
@index_steps << TrajectPlus::Indexer::ToFieldStep.new(field_name, || aLambda, transform || block, Traject::Util.(caller.first), **namedArgs)
end
|
#to_fields(fields, mapping_method) ⇒ Object
apply the same mapping to multiple fields
61
62
63
|
# File 'lib/traject_plus/macros.rb', line 61
def to_fields(fields, mapping_method)
fields.each { |field| to_field field, mapping_method }
end
|
53
54
55
56
57
58
|
# File 'lib/traject_plus/macros.rb', line 53
def transform(options = {})
lambda do |record, accumulator, context|
results = TrajectPlus::Extraction.(accumulator, options)
accumulator.replace(results)
end
end
|
construct a structured hash using values extracted using traject
5
6
7
8
9
10
11
12
13
|
# File 'lib/traject_plus/macros.rb', line 5
def transform_values(context, hash)
hash.transform_values do |lambdas|
accumulator = []
Array(lambdas).each do |lambda|
lambda.call(context.source_record, accumulator, context)
end
accumulator
end
end
|