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.
- #length ⇒ Object
- #pad_each(*ctx, **input_opts, &block) ⇒ 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 44 45 |
# 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 |
Instance Method Details
#<<(item) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# 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) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/output_mode/callable.rb', line 96 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
57 58 59 |
# File 'lib/output_mode/callable.rb', line 57 def each(&block) @callables.each(&block) end |
#length ⇒ Object
108 109 110 |
# File 'lib/output_mode/callable.rb', line 108 def length @callables.length end |
#pad_each(*ctx, **input_opts, &block) ⇒ Object
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 90 91 92 93 94 |
# File 'lib/output_mode/callable.rb', line 61 def pad_each(*ctx, **input_opts, &block) 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 # Generate an enum # NOTE: This fixes the double splate deprecation warning enum = Enumerator.new do |yielder| pads.each do |callable, opts| yielder.yield(callable, **opts) end end enum.each(&block) end |