Module: UnicodePlot::ValueTransformer

Included in:
Barplot
Defined in:
lib/unicode_plot/value_transformer.rb

Constant Summary collapse

PREDEFINED_TRANSFORM_FUNCTIONS =
{
  log: Math.method(:log),
  ln: Math.method(:log),
  log10: Math.method(:log10),
  lg: Math.method(:log10),
  log2: Math.method(:log2),
  lb: Math.method(:log2),
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transform_name(func, basename = "") ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/unicode_plot/value_transformer.rb', line 30

module_function def transform_name(func, basename="")
  return basename unless func
  case func
  when String, Symbol
    name = func
  when ->(f) { f.respond_to?(:name) }
    name = func.name
  else
    name = "custom"
  end
  "#{basename} [#{name}]"
end

Instance Method Details

#transform_values(func, values) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/unicode_plot/value_transformer.rb', line 12

def transform_values(func, values)
  return values unless func

  unless func.respond_to?(:call)
    func = PREDEFINED_TRANSFORM_FUNCTIONS[func]
    unless func.respond_to?(:call)
      raise ArgumentError, "func must be callable"
    end
  end

  case values
  when Numeric
    func.(values)
  else
    values.map(&func)
  end
end