module Verbalize::Action::ClassMethods
Public Instance Methods
call()
click to toggle source
Because call/call! are defined when Action.input is called, they would not be defined when there is no input. So we pre-define them here, and if there is any input, they are overwritten
# File lib/verbalize/action.rb, line 77 def call __proxied_call end
call!()
click to toggle source
# File lib/verbalize/action.rb, line 81 def call! __proxied_call! end
Also aliased as: !
default_inputs()
click to toggle source
# File lib/verbalize/action.rb, line 42 def default_inputs (@defaults || {}).keys end
defaults()
click to toggle source
# File lib/verbalize/action.rb, line 50 def defaults @defaults end
input_is_valid?(input, value)
click to toggle source
# File lib/verbalize/action.rb, line 58 def input_is_valid?(input, value) return true unless input_validations.include?(input.to_sym) input_validations[input].call(value) == true rescue => e @local_error = e false end
input_validations()
click to toggle source
# File lib/verbalize/action.rb, line 54 def input_validations @input_validations ||= {} end
inputs()
click to toggle source
# File lib/verbalize/action.rb, line 46 def inputs required_inputs + optional_inputs + default_inputs end
optional_inputs()
click to toggle source
# File lib/verbalize/action.rb, line 38 def optional_inputs @optional_inputs || [] end
pop_local_error()
click to toggle source
# File lib/verbalize/action.rb, line 67 def pop_local_error return nil unless @local_error @local_error ensure @local_error = nil end
required_inputs()
click to toggle source
# File lib/verbalize/action.rb, line 34 def required_inputs @required_inputs || [] end
Private Instance Methods
__proxied_call(*args)
click to toggle source
# File lib/verbalize/action.rb, line 114 def __proxied_call(*args) error = catch(:verbalize_error) do value = perform(*args) return Success.new(value) end Failure.new(error) end
__proxied_call!(*args)
click to toggle source
# File lib/verbalize/action.rb, line 123 def __proxied_call!(*args) perform(*args) rescue UncaughtThrowError => uncaught_throw_error fail_value = uncaught_throw_error.value error = Verbalize::Error.new("Unhandled fail! called with: #{fail_value.inspect}.") error.set_backtrace(uncaught_throw_error.backtrace[2..-1]) raise error end
assign_defaults(optional)
click to toggle source
# File lib/verbalize/action.rb, line 102 def assign_defaults(optional) @defaults = optional.select { |kw| kw.is_a?(Hash) }.reduce(&:merge) @defaults = (@defaults || {}) .map { |k, v| [k, v.respond_to?(:call) ? v : -> { v }] } .to_h end
input(*required_keywords, optional: [])
click to toggle source
# File lib/verbalize/action.rb, line 88 def input(*required_keywords, optional: []) @required_inputs = required_keywords optional = Array(optional) @optional_inputs = optional.reject { |kw| kw.is_a?(Hash) } assign_defaults(optional) class_eval Build.call(required_inputs, optional_inputs, default_inputs) end
perform(*args)
click to toggle source
# File lib/verbalize/action.rb, line 110 def perform(*args) new(*args).send(:call) end
validate(keyword, &block)
click to toggle source
# File lib/verbalize/action.rb, line 97 def validate(keyword, &block) raise Verbalize::Error, 'Missing block to validate against!' unless block_given? input_validations[keyword.to_sym] = block end