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) ⇒ Object
- #prefix(prefix) ⇒ Object
- #replace(regex, replace_with) ⇒ 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
33 34 35 36 37 |
# File 'lib/remi/transform.rb', line 33 def concatenate(delimiter="") memoize_as_lambda(__method__, delimiter) do |(mdelimiter), *largs| Array(largs).join(mdelimiter) end end |
#constant(const) ⇒ Object
118 119 120 121 122 |
# File 'lib/remi/transform.rb', line 118 def constant(const) memoize_as_lambda(__method__, const) do |(mconst), larg| mconst end end |
#date_diff(measure = :days) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/remi/transform.rb', line 104 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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/remi/transform.rb', line 65 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
59 60 61 62 63 |
# File 'lib/remi/transform.rb', line 59 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
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/remi/transform.rb', line 39 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
53 54 55 56 57 |
# File 'lib/remi/transform.rb', line 53 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/remi/transform.rb', line 83 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.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) ⇒ Object
27 28 29 30 31 |
# File 'lib/remi/transform.rb', line 27 def postfix(postfix) memoize_as_lambda(__method__, postfix) do |(mpostfix), larg| "#{larg}#{mpostfix}" end end |
#prefix(prefix) ⇒ Object
21 22 23 24 25 |
# File 'lib/remi/transform.rb', line 21 def prefix(prefix) memoize_as_lambda(__method__, prefix) do |(mprefix), larg| "#{mprefix}#{larg}" end end |
#replace(regex, replace_with) ⇒ Object
124 125 126 127 128 |
# File 'lib/remi/transform.rb', line 124 def replace(regex, replace_with) memoize_as_lambda(__method__, regex, replace_with) do |(mregex, mreplace_with), larg| larg.gsub(regex, replace_with) end end |
#validate_email(substitute = '') ⇒ Object
130 131 132 133 134 |
# File 'lib/remi/transform.rb', line 130 def validate_email(substitute='') memoize_as_lambda(__method__, substitute) do |(msubstitute), larg| larg.match(/^.+@[a-z0-9\-\.]+$/i) ? larg : msubstitute end end |