class OutputMode::Callable

Attributes

callable[R]

@!attribute [r] modes

@return [Hash<Symbol => Boolean>] Returns the configured modes

@!attribute [r] callable

@return [#call] Returns the underlining block

@!attribute [r] config

@return [Hash] An arbitrary hash of key-value pairs
config[R]

@!attribute [r] modes

@return [Hash<Symbol => Boolean>] Returns the configured modes

@!attribute [r] callable

@return [#call] Returns the underlining block

@!attribute [r] config

@return [Hash] An arbitrary hash of key-value pairs
modes[R]

@!attribute [r] modes

@return [Hash<Symbol => Boolean>] Returns the configured modes

@!attribute [r] callable

@return [#call] Returns the underlining block

@!attribute [r] config

@return [Hash] An arbitrary hash of key-value pairs

Public Class Methods

new(modes: {}, **config, &block) click to toggle source

Wraps a block/ callable object with mode query methods @overload initialize(modes: {}, **config)

@param [Hash<Symbol => Boolean>] modes: Provide the preconfigured modes
@param **config An arbitrary hash to be stored on the object
@yield Executed by the {#call} method
@yieldparam *a The arguments provided to {#call}

@overload initialize(modes: []) { |*a| … }

@param [Array] modes: The preconfigured modes as an array, this will be converted to a hash
# File lib/output_mode/callable.rb, line 127
def initialize(modes: {}, **config, &block)
  @callable = block
  @modes = if modes.is_a? Hash
             modes.reject { |_, v| v.nil? }
                  .map { |k, v| [k, v ? true : false] }
                  .to_h
           else
             modes.map { |k| [k, true] }.to_h
           end
  @config = config
end

Public Instance Methods

call(*a) click to toggle source

Calls the underlining block @param *a The arguments to be provided to {#callable} @return The results from the block

# File lib/output_mode/callable.rb, line 196
def call(*a)
  callable.call(*a)
end
generator(output) click to toggle source
# File lib/output_mode/callable.rb, line 200
def generator(output)
  ->(*a) do
    # Implicitly determine which parts of the context can be passed through
    ctx = if callable.parameters.any? { |type, _| type == :keyrest }
            output.context
          else
            keys = callable.parameters.select { |type, _| [:key, :keyreq].include?(type) }
                                  .map { |_, k| k }
            output.context.slice(*keys)
          end
    raw = call(*a, **ctx)
    if raw == true
      config[:yes] || output.yes
    elsif raw == false
      config[:no] ||  output.no
    elsif [nil, ''].include?(raw)
      config[:default] || output.default
    else
      raw
    end
  end
end
method_char(s) click to toggle source

Determines the “type” associated with a dynamic method @overload method_char(bang!)

@param bang! A symbol/string ending with !
@return ['!']

@overload method_char(question?)

@param question? A symbol/string ending with ?
@return ['?']

@overload method_char(other)

@param other Any other symbol/string
@return [Nil]
# File lib/output_mode/callable.rb, line 188
def method_char(s)
  char = s[-1]
  ['?', '!'].include?(char) ? char : nil
end
method_missing(s, *args, &b) click to toggle source

Handles the dynamic +<query>?+ and +<explicit-negation>!+ methods

@return [Boolean] The result of the query or explicit-negation @raise [NoMethodError] All other method calls

Calls superclass method
# File lib/output_mode/callable.rb, line 143
def method_missing(s, *args, &b)
  mode = s[0..-2].to_sym
  case method_char(s)
  when '?'
    ifnone = (args.length > 0 ? args.first : false)
    modes.fetch(mode, ifnone)
  when '!'
    send(:"#{mode}?", true)
  else
    super
  end
end
respond_to_missing?(s, *_) click to toggle source

Responds true for valid dynamic methods @param [Symbol] s The method to be tested @return [Boolean] The truthiness of the underlining call to {#method_char}

# File lib/output_mode/callable.rb, line 174
def respond_to_missing?(s, *_)
  method_char(s) ? true : false
end