Class: Remi::Transform::Lookup

Inherits:
Remi::Transform show all
Defined in:
lib/remi/transform.rb

Overview

Public: Transform used to do key-value lookup on hash-like objects

lookup - The lookup object that takes keys and returns values. missing - What to use if a key is not found in the lookup (default: nil). If this

is a proc, it is sent the key as an argument.

Examples:

my_lookup = { 1 => 'one', 2 => 'two }
Lookup.new().to_proc.call(1) # => "1"
Lookup.new().to_proc.call(3) # => nil
Lookup.new().to_proc.call(3, missing: 'UNK') # => "UNK"
Lookup.new().to_proc.call(3, missing: ->(v) { "I don't know #{v}" }) # => "I don't know 3"

Instance Attribute Summary

Attributes inherited from Remi::Transform

#multi_args, #source_metadata, #target_metadata

Instance Method Summary collapse

Methods inherited from Remi::Transform

#call, #to_proc

Constructor Details

#initialize(lookup, *args, missing: nil, **kargs, &block) ⇒ Lookup

Returns a new instance of Lookup.



159
160
161
162
163
# File 'lib/remi/transform.rb', line 159

def initialize(lookup, *args, missing: nil, **kargs, &block)
  super
  @lookup  = lookup
  @missing = missing
end

Instance Method Details

#transform(value) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/remi/transform.rb', line 165

def transform(value)
  result = @lookup[value]

  if !result.nil?
    result
  elsif @missing.respond_to? :call
    @missing.call(value)
  else
    @missing
  end
end