Class: OutputMode::Callable
- Inherits:
-
Object
- Object
- OutputMode::Callable
- Defined in:
- lib/output_mode/callable.rb
Instance Attribute Summary collapse
-
#callable ⇒ #call
readonly
Returns the underlining block.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#modes ⇒ Hash<Symbol => Boolean>
readonly
Returns the configured modes.
Instance Method Summary collapse
-
#call(*a) ⇒ Object
Calls the underlining block.
-
#generator(output) ⇒ Object
DEPRECATED: Use a Formatter class.
-
#initialize(modes: {}, **config, &block) ⇒ Callable
constructor
Wraps a block/ callable object with mode query methods.
-
#method_char(s) ⇒ Object
Determines the “type” associated with a dynamic method.
-
#method_missing(s, *args, &b) ⇒ Boolean
Handles the dynamic <query>? and <explicit-negation>! methods.
-
#mode! ⇒ Boolean
deprecated
Deprecated.
Please use the newer mode?(true) syntax
-
#mode?(ifnone = false) ⇒ Boolean
This is a dynamic method for check if an arbitrary
mode
has been set. -
#respond_to_missing?(s, *_) ⇒ Boolean
Responds
true
for valid dynamic methods.
Constructor Details
#initialize(modes: {}, **config) {|*a| ... } ⇒ Callable #initialize(modes: []) ⇒ Callable
Wraps a block/ callable object with mode query methods
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
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
120 |
# File 'lib/output_mode/callable.rb', line 120 attr_reader :modes, :callable, :config |
#config ⇒ Object (readonly)
Returns the value of attribute config.
120 |
# File 'lib/output_mode/callable.rb', line 120 attr_reader :modes, :callable, :config |
#modes ⇒ Hash<Symbol => Boolean> (readonly)
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
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
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
Please use the newer mode?(true) syntax
Older syntax that returns true
if the mode
has not been defined. Otherwise the same as #mode?
|
# 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
|
# File 'lib/output_mode/callable.rb', line 161
|
#respond_to_missing?(s, *_) ⇒ Boolean
Responds true
for valid dynamic methods
179 180 181 |
# File 'lib/output_mode/callable.rb', line 179 def respond_to_missing?(s, *_) method_char(s) ? true : false end |