Module: Remi::Transform
Instance Method Summary collapse
- #[](meth) ⇒ Object
- #concatenate(delimiter = "") ⇒ Object
- #constant(const) ⇒ Object
- #date_diff(measure = :days) ⇒ Object
- #format_date(from_fmt: '%m/%d/%Y', to_fmt: '%Y-%m-%d') ⇒ Object
- #ifblank(replace_with) ⇒ Object
- #lookup(h_lookup, missing: nil) ⇒ Object
-
#memoize_as_lambda(func, *args, &block) ⇒ Object
We need to memoize each lambda with its static arguments so it’s not recreated each row.
- #nvl(default = '') ⇒ Object
- #parse_date(format: '%Y-%m-%d', if_blank: nil) ⇒ Object
- #postfix(postfix, if_blank: '') ⇒ Object
- #prefix(prefix, if_blank: '') ⇒ Object
- #replace(regex, replace_with) ⇒ Object
- #truncate(len) ⇒ Object
- #validate_email(substitute = '') ⇒ Object
Instance Method Details
#[](meth) ⇒ Object
5 6 7 |
# File 'lib/remi/transform.rb', line 5 def [](meth) method(meth) end |
#concatenate(delimiter = "") ⇒ Object
47 48 49 50 51 |
# File 'lib/remi/transform.rb', line 47 def concatenate(delimiter="") memoize_as_lambda(__method__, delimiter) do |(mdelimiter), *largs| Array(largs).join(mdelimiter) end end |
#constant(const) ⇒ Object
134 135 136 137 138 |
# File 'lib/remi/transform.rb', line 134 def constant(const) memoize_as_lambda(__method__, const) do |(mconst), larg| mconst end end |
#date_diff(measure = :days) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/remi/transform.rb', line 120 def date_diff(measure = :days) memoize_as_lambda(__method__, measure.to_sym) do |(mmeasure), *larg| if mmeasure == :days (larg.last - larg.first).to_i elsif mmeasure == :months (larg.last.year * 12 + larg.last.month) - (larg.first.year * 12 + larg.first.month) elsif mmeasure == :years larg.last.year - larg.first.year else raise "I don't know how to handle #{mmeasure} yet" end end end |
#format_date(from_fmt: '%m/%d/%Y', to_fmt: '%Y-%m-%d') ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/remi/transform.rb', line 79 def format_date(from_fmt: '%m/%d/%Y', to_fmt: '%Y-%m-%d') memoize_as_lambda(__method__, from_fmt, to_fmt) do |(mfrom_fmt, mto_fmt), larg| begin if larg.blank? then '' elsif larg.respond_to? :strftime larg.strftime(mto_fmt) else Date.strptime(larg, mfrom_fmt).strftime(mto_fmt) end rescue ArgumentError => err puts "Error parsing date (#{larg.class}): '#{larg}'" raise err end end end |
#ifblank(replace_with) ⇒ Object
73 74 75 76 77 |
# File 'lib/remi/transform.rb', line 73 def ifblank(replace_with) memoize_as_lambda(__method__, replace_with) do |(mreplace_with), larg| larg.blank? ? mreplace_with : larg end end |
#lookup(h_lookup, missing: nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/remi/transform.rb', line 53 def lookup(h_lookup, missing: nil) memoize_as_lambda(__method__, h_lookup, missing) do |(mh_lookup, mmissing), larg| result = mh_lookup[larg] if !result.nil? result elsif mmissing.class == Proc mmissing.call(larg) else mmissing end end end |
#memoize_as_lambda(func, *args, &block) ⇒ Object
We need to memoize each lambda with its static arguments so it’s not recreated each row. Inspired by parameter memoization in www.justinweiss.com/articles/4-simple-memoization-patterns-in-ruby-and-one-gem/
11 12 13 14 15 16 17 18 19 |
# File 'lib/remi/transform.rb', line 11 def memoize_as_lambda(func, *args, &block) iv = instance_variable_get("@#{func}") return iv[args] if iv hash_memo = Hash.new do |h, margs| h[margs] = lambda { |*largs| block.call(margs, *largs) } end instance_variable_set("@#{func}", hash_memo)[args] end |
#nvl(default = '') ⇒ Object
67 68 69 70 71 |
# File 'lib/remi/transform.rb', line 67 def nvl(default='') memoize_as_lambda(__method__, default) do |(mdefault), *largs| Array(largs).find(->() { mdefault }) { |arg| !arg.blank? } end end |
#parse_date(format: '%Y-%m-%d', if_blank: nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/remi/transform.rb', line 97 def parse_date(format: '%Y-%m-%d', if_blank: nil) memoize_as_lambda(__method__, format, if_blank.try(:to_sym)) do |(mformat, mif_blank), larg| begin if larg.respond_to?(:strftime) larg elsif larg.blank? then if mif_blank == :low Date.new(1900,01,01) elsif mif_blank == :high Date.new(2999,12,31) else mif_blank end else Date.strptime(larg, mformat) end rescue ArgumentError => err puts "Error parsing date (#{larg.class}): '#{larg}')" raise err end end end |
#postfix(postfix, if_blank: '') ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/remi/transform.rb', line 31 def postfix(postfix, if_blank: '') memoize_as_lambda(__method__, postfix, if_blank) do |(mpostfix, mif_blank), larg| if larg.blank? mif_blank else "#{larg}#{mpostfix}" end end end |
#prefix(prefix, if_blank: '') ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/remi/transform.rb', line 21 def prefix(prefix, if_blank: '') memoize_as_lambda(__method__, prefix, if_blank) do |(mprefix, mif_blank), larg| if larg.blank? mif_blank else "#{mprefix}#{larg}" end end end |
#replace(regex, replace_with) ⇒ Object
140 141 142 143 144 |
# File 'lib/remi/transform.rb', line 140 def replace(regex, replace_with) memoize_as_lambda(__method__, regex, replace_with) do |(mregex, mreplace_with), larg| larg.gsub(regex, replace_with) end end |
#truncate(len) ⇒ Object
41 42 43 44 45 |
# File 'lib/remi/transform.rb', line 41 def truncate(len) memoize_as_lambda(__method__, len) do |(mlen), larg| larg.slice(0,mlen) end end |
#validate_email(substitute = '') ⇒ Object
146 147 148 149 150 151 |
# File 'lib/remi/transform.rb', line 146 def validate_email(substitute='') memoize_as_lambda(__method__, substitute) do |(msubstitute), larg| larg = larg || '' larg.match(/^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,}$/i) ? larg : msubstitute end end |