class OutputMode::Callables

Internal array like object that will convert procs to Callable

Public Class Methods

new(callables = nil) click to toggle source

@api private

# File lib/output_mode/callable.rb, line 33
def initialize(callables = nil)
  @callables = []
  case callables
  when Array, Callables
    callables.each do |c|
      @callables << (c.is_a?(Callable) ? c : Callable.new(&c))
    end
  when nil
    # NOOP
  else
    raise "Can not convert #{callables.class} into a #{self.class}"
  end
end

Public Instance Methods

<<(item) click to toggle source
# File lib/output_mode/callable.rb, line 47
def <<(item)
  if item.is_a? Callable
    @callables << item
  elsif item.respond_to?(:call)
    @callables << Callable.new(&item)
  else
    raise Error, "#{item.class} is not callable"
  end
end
config_select(key, *values) click to toggle source
# File lib/output_mode/callable.rb, line 93
def config_select(key, *values)
  selected = self.select do |callable|
    conf = callable.config[key]
    if conf.is_a? Array
      !(conf & values).empty?
    else
      values.include?(conf)
    end
  end
  Callables.new(selected)
end
each(&block) click to toggle source
# File lib/output_mode/callable.rb, line 57
def each(&block)
  @callables.each(&block)
end
length() click to toggle source
# File lib/output_mode/callable.rb, line 105
def length
  @callables.length
end
pad_each(*ctx, **input_opts) { |c, **opts| ... } click to toggle source
# File lib/output_mode/callable.rb, line 61
def pad_each(*ctx, **input_opts)
  fields = self.map do |callables|
    field = callables.config[:header]
    if field.respond_to?(:call)
      opts =  if field.parameters.include?(:keyrest)
                input_opts.dup
              else
                keys = field.parameters
                             .select { |type, _| [:key, :keyreq].include?(type) }
                             .map { |_, k| k }
                input_opts.slice(*keys)
              end
      opts.empty? ? field.call(*ctx) : field.call(*ctx, **opts)
    else
      field
    end
  end

  max_length = fields.map { |f| f.to_s.length }.max
  pads = self.each_with_index.map do |callable, idx|
    field = fields[idx]
    length = max_length - field.to_s.length
    [callable, { padding: ' ' * length, field: field }]
  end

  if block_given?
    pads.each { |c, opts|  yield(c, **opts) }
  else
    pads.each
  end
end