Module: Actionizer
- Defined in:
- lib/actionizer.rb,
lib/actionizer/result.rb,
lib/actionizer/failure.rb,
lib/actionizer/version.rb
Defined Under Namespace
Modules: ClassMethods Classes: Failure, Result
Constant Summary collapse
- VERSION =
'0.5.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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/actionizer.rb', line 45 def method_missing(method_name, *args, &block) return super unless method_name.to_s.end_with?('_or_fail') action_class, params = *args unless action_class.include? Actionizer raise ArgumentError, "#{action_class.name} must include Actionizer" end result = action_class.send(method_name.to_s.chomp('_or_fail'), params) fail!(error: result.error) if result.failure? result end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
7 8 9 |
# File 'lib/actionizer.rb', line 7 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
7 8 9 |
# File 'lib/actionizer.rb', line 7 def output @output end |
Class Method Details
.included(base) ⇒ Object
9 10 11 |
# File 'lib/actionizer.rb', line 9 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#fail!(params = {}) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/actionizer.rb', line 36 def fail!(params = {}) params.each_pair { |key, value| output[key] = value } output.fail raise Actionizer::Failure.new('Failed!', output) end |
#initialize(initial_input = {}) ⇒ Object
31 32 33 34 |
# File 'lib/actionizer.rb', line 31 def initialize(initial_input = {}) @input = RecursiveOpenStruct.new(initial_input) @output = Actionizer::Result.new end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
60 61 62 |
# File 'lib/actionizer.rb', line 60 def respond_to_missing?(method_name, _include_private = false) method_name.to_s.end_with?('_or_fail') end |