Module: TrajectPlus::Macros

Extended by:
Deprecation
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

Instance Method Details

#accumulate(&block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/traject_plus/macros.rb', line 33

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



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/traject_plus/macros.rb', line 81

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, extract || aLambda, transform, Traject::Util.extract_caller_location(caller.first), indexer)
end

#conditional(condition, block) ⇒ Object

only accumulate values if a condition is met



42
43
44
45
46
47
48
# File 'lib/traject_plus/macros.rb', line 42

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



56
57
58
59
60
# File 'lib/traject_plus/macros.rb', line 56

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



25
26
27
28
29
30
31
# File 'lib/traject_plus/macros.rb', line 25

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



50
51
52
53
54
# File 'lib/traject_plus/macros.rb', line 50

def from_settings(field)
  accumulate do |record, context|
    context.settings.fetch(field)
  end
end

#to_field(field_name, *procs, extract: nil, transform: nil, **namedArgs, &block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/traject_plus/macros.rb', line 73

def to_field(field_name, *procs, extract: nil, transform: nil, **namedArgs, &block)
  if transform || extract
    Deprecation.warn(self, "Passing extract or transform arguments to to_field is deprecated. Use the Traject 3 pipeline instead.")
  end
  procs =  [extract, transform] if procs.empty?
  @index_steps << TrajectPlus::Indexer::ToFieldStep.new(field_name, procs, block, Traject::Util.extract_caller_location(caller.first), **namedArgs)
end

#to_fields(fields, mapping_method) ⇒ Object

apply the same mapping to multiple fields



69
70
71
# File 'lib/traject_plus/macros.rb', line 69

def to_fields(fields, mapping_method)
  fields.each { |field| to_field field, mapping_method }
end

#transform(options = {}) ⇒ Object



62
63
64
65
66
# File 'lib/traject_plus/macros.rb', line 62

def transform(options = {})
  return deprecated_transform(options) unless block_given?

  super() # These empty parens are meaningful, otherwise it'll pass options.
end

#transform_values(context, hash) ⇒ Object

construct a structured hash using values extracted using traject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/traject_plus/macros.rb', line 10

def transform_values(context, hash)
  hash.transform_values do |lambdas|
    accumulator = []
    Array(lambdas).each do |aProc|
      if aProc.arity == 2
        aProc.call(context.source_record, accumulator)
      else
        aProc.call(context.source_record, accumulator, context)
      end
    end
    accumulator
  end
end