Class: Cure::Transformation::Candidate

Inherits:
Object
  • Object
show all
Includes:
Helpers::ObjectHelpers, Log
Defined in:
lib/cure/transformation/candidate.rb

Overview

Per row, we will have a candidate for each transformation that needs to be made

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#log_debug, #log_error, #log_info, #log_trace, #log_warn

Methods included from Helpers::ObjectHelpers

#class_exists?

Constructor Details

#initialize(column, named_range: Cure::Extraction.default_named_range, options: {}) ⇒ Candidate

Returns a new instance of Candidate.



33
34
35
36
37
38
39
40
# File 'lib/cure/transformation/candidate.rb', line 33

def initialize(column, named_range: Cure::Extraction.default_named_range, options: {})
  @column = column
  @named_range = named_range
  @ignore_empty = options.fetch(:ignore_empty, false)

  @translations = []
  @no_match_translation = nil
end

Instance Attribute Details

#columnString (readonly)

Lookup column name for CSV.

Returns:



22
23
24
# File 'lib/cure/transformation/candidate.rb', line 22

def column
  @column
end

#ignore_emptyObject (readonly)

Returns the value of attribute ignore_empty.



31
32
33
# File 'lib/cure/transformation/candidate.rb', line 31

def ignore_empty
  @ignore_empty
end

#named_rangeString (readonly)

Named range that column exists in

Returns:



18
19
20
# File 'lib/cure/transformation/candidate.rb', line 18

def named_range
  @named_range
end

#no_match_translationTranslation (readonly)

Returns:



29
30
31
# File 'lib/cure/transformation/candidate.rb', line 29

def no_match_translation
  @no_match_translation
end

#translationsList<Translation> (readonly)

What sort of data needs to be generated.

Returns:



26
27
28
# File 'lib/cure/transformation/candidate.rb', line 26

def translations
  @translations
end

Instance Method Details

#perform(source_value, row_ctx) ⇒ String?

Transforms the existing value

Parameters:

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cure/transformation/candidate.rb', line 46

def perform(source_value, row_ctx)
  value = source_value

  @translations.each do |translation|
    temp = translation.extract(value, row_ctx)
    value = temp if temp
  end

  if value == source_value && @no_match_translation
    log_trace("No translation made for #{value} [#{source_value}]")
    value = @no_match_translation.extract(source_value, row_ctx)
    log_trace("Translated to #{value} from [#{source_value}]")
  end

  value
end

#with_no_match_translation(no_match_translation) ⇒ Object



68
69
70
71
# File 'lib/cure/transformation/candidate.rb', line 68

def with_no_match_translation(no_match_translation)
  @no_match_translation = no_match_translation
  self
end

#with_translations(translations) ⇒ Object



63
64
65
66
# File 'lib/cure/transformation/candidate.rb', line 63

def with_translations(translations)
  @translations = translations
  self
end