Class: OutputMode::Callables
- Inherits:
-
Object
- Object
- OutputMode::Callables
- Includes:
- Enumerable
- Defined in:
- lib/output_mode/callable.rb
Overview
Internal array like object that will convert procs to Callable
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #config_select(key, *values) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(callables = nil) ⇒ Callables
constructor
private
A new instance of Callables.
- #pad_each(*ctx, **input_opts) ⇒ Object
Constructor Details
#initialize(callables = nil) ⇒ Callables
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Callables.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/output_mode/callable.rb', line 33 def initialize(callables = nil) @callables = [] case callables when Array, Callables callables.each { |c| @callables << c } when nil # NOOP else raise "Can not convert #{callables.class} into a #{self.class}" end end |
Instance Method Details
#<<(item) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/output_mode/callable.rb', line 45 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) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/output_mode/callable.rb', line 91 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) ⇒ Object
55 56 57 |
# File 'lib/output_mode/callable.rb', line 55 def each(&block) @callables.each(&block) end |
#pad_each(*ctx, **input_opts) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/output_mode/callable.rb', line 59 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.to_s end end max_length = fields.map(&:length).max pads = self.each_with_index.map do |callable, idx| field = fields[idx] length = max_length - field.length [callable, { padding: ' ' * length, field: field }] end if block_given? pads.each { |c, opts| yield(c, **opts) } else pads.each end end |