Class: OutputMode::Callable

Inherits:
Object
  • Object
show all
Defined in:
lib/output_mode/callable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modes: {}, **config) {|*a| ... } ⇒ Callable #initialize(modes: []) ⇒ Callable

Wraps a block/ callable object with mode query methods

Overloads:

  • #initialize(modes: {}, **config) {|*a| ... } ⇒ Callable

    Parameters:

    • modes: (Hash<Symbol => Boolean>) (defaults to: {})

      Provide the preconfigured modes

    • **config

      An arbitrary hash to be stored on the object

    Yields:

    • Executed by the #call method

    Yield Parameters:

    • *a

      The arguments provided to #call

  • #initialize(modes: []) ⇒ Callable

    Parameters:

    • modes: (Array) (defaults to: [])

      The preconfigured modes as an array, this will be converted to a hash



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/output_mode/callable.rb', line 130

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *args, &b) ⇒ Boolean

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

DEPRECATED: The explicit! negation operator should not be used

Returns:

  • (Boolean)

    The result of the query or explicit-negation

Raises:

  • (NoMethodError)

    All other method calls



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/output_mode/callable.rb', line 148

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

Instance Attribute Details

#callable#call (readonly)

Returns the underlining block

Returns:

  • (#call)

    Returns the underlining block



120
# File 'lib/output_mode/callable.rb', line 120

attr_reader :modes, :callable, :config

#configObject (readonly)

Returns the value of attribute config.



120
# File 'lib/output_mode/callable.rb', line 120

attr_reader :modes, :callable, :config

#modesHash<Symbol => Boolean> (readonly)

Returns the configured modes

Returns:

  • (Hash<Symbol => Boolean>)

    Returns the configured modes



120
121
122
# File 'lib/output_mode/callable.rb', line 120

def modes
  @modes
end

Instance Method Details

#call(*a) ⇒ Object

Calls the underlining block

Parameters:

  • *a

    The arguments to be provided to #callable

Returns:

  • The results from the block



201
202
203
# File 'lib/output_mode/callable.rb', line 201

def call(*a)
  callable.call(*a)
end

#generator(output) ⇒ Object

DEPRECATED: Use a Formatter class



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/output_mode/callable.rb', line 206

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(bang!) ⇒ '!' #method_char(question?) ⇒ '?' #method_char(other) ⇒ Nil

Determines the “type” associated with a dynamic method

Overloads:

  • #method_char(bang!) ⇒ '!'

    Parameters:

    • bang!

      A symbol/string ending with !

    Returns:

    • ('!')
  • #method_char(question?) ⇒ '?'

    Parameters:

    • question?

      A symbol/string ending with ?

    Returns:

    • ('?')
  • #method_char(other) ⇒ Nil

    Parameters:

    • other

      Any other symbol/string

    Returns:

    • (Nil)


193
194
195
196
# File 'lib/output_mode/callable.rb', line 193

def method_char(s)
  char = s[-1]
  ['?', '!'].include?(char) ? char : nil
end

#mode!Boolean

Deprecated.

Please use the newer mode?(true) syntax

Older syntax that returns true if the mode has not been defined. Otherwise the same as #mode?

Returns:

  • (Boolean)


# File 'lib/output_mode/callable.rb', line 161

#mode?(ifnone = false) ⇒ Boolean

This is a dynamic method for check if an arbitrary mode has been set. It will return the associated value if the mode has been defined in #modes.

Otherwise it will return the ifnone value

Returns:

  • (Boolean)

    the associated value if mode has been defined

  • otherwise return the ifnone value



# File 'lib/output_mode/callable.rb', line 161

#respond_to_missing?(s, *_) ⇒ Boolean

Responds true for valid dynamic methods

Parameters:

  • s (Symbol)

    The method to be tested

Returns:

  • (Boolean)

    The truthiness of the underlining call to #method_char



179
180
181
# File 'lib/output_mode/callable.rb', line 179

def respond_to_missing?(s, *_)
  method_char(s) ? true : false
end