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
-
#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
modehas been set. -
#respond_to_missing?(s, *_) ⇒ Boolean
Responds
truefor valid dynamic methods.
Constructor Details
#initialize(modes: {}, **config) {|*a| ... } ⇒ Callable #initialize(modes: []) ⇒ Callable
Wraps a block/ callable object with mode query methods
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/output_mode/callable.rb', line 121 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
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/output_mode/callable.rb', line 137 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
111 |
# File 'lib/output_mode/callable.rb', line 111 attr_reader :modes, :callable, :config |
#config ⇒ Object (readonly)
Returns the value of attribute config.
111 |
# File 'lib/output_mode/callable.rb', line 111 attr_reader :modes, :callable, :config |
#modes ⇒ Hash<Symbol => Boolean> (readonly)
Returns the configured modes
111 112 113 |
# File 'lib/output_mode/callable.rb', line 111 def modes @modes end |
Instance Method Details
#call(*a) ⇒ Object
Calls the underlining block
190 191 192 |
# File 'lib/output_mode/callable.rb', line 190 def call(*a) callable.call(*a) end |
#generator(output) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/output_mode/callable.rb', line 194 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
182 183 184 185 |
# File 'lib/output_mode/callable.rb', line 182 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 150
|
#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 150
|
#respond_to_missing?(s, *_) ⇒ Boolean
Responds true for valid dynamic methods
168 169 170 |
# File 'lib/output_mode/callable.rb', line 168 def respond_to_missing?(s, *_) method_char(s) ? true : false end |