Module: Actionizer
- Defined in:
- lib/actionizer.rb,
lib/actionizer/inputs.rb,
lib/actionizer/result.rb,
lib/actionizer/failure.rb,
lib/actionizer/version.rb
Defined Under Namespace
Modules: ClassMethods Classes: Failure, Inputs, Result
Constant Summary collapse
- VERSION =
'0.8.0'
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Class Method Summary collapse
Instance Method Summary collapse
- #fail!(params = {}) ⇒ Object
- #initialize(initial_input = {}) ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
Allows you to call *_or_fail.
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Allows you to call *_or_fail
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/actionizer.rb', line 75 def method_missing(method_name, *args, &block) return super if !method_name.to_s.end_with?('_or_fail') action_class, *params = args = method_name.to_s.chomp('_or_fail') if !action_class.respond_to?() raise ArgumentError, "#{action_class.name} must define ##{}" end result = action_class.send(, *params) verify_result_is_conforming!(result, "#{action_class.name}##{}") errors = result.to_h.select { |key, _value| key.to_s.start_with?('error') } fail!(errors) if result.failure? result end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
8 9 10 |
# File 'lib/actionizer.rb', line 8 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
8 9 10 |
# File 'lib/actionizer.rb', line 8 def output @output end |
Class Method Details
.included(base) ⇒ Object
10 11 12 |
# File 'lib/actionizer.rb', line 10 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#fail!(params = {}) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/actionizer.rb', line 66 def fail!(params = {}) params.each_pair { |key, value| output[key] = value } output.fail raise Actionizer::Failure.new('Failed!', output) end |
#initialize(initial_input = {}) ⇒ Object
61 62 63 64 |
# File 'lib/actionizer.rb', line 61 def initialize(initial_input = {}) @input = OpenStruct.new(initial_input) @output = Actionizer::Result.new end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
95 96 97 |
# File 'lib/actionizer.rb', line 95 def respond_to_missing?(method_name, _include_private = false) method_name.to_s.end_with?('_or_fail') end |