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.4.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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/actionizer.rb', line 44 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.
6 7 8 |
# File 'lib/actionizer.rb', line 6 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
6 7 8 |
# File 'lib/actionizer.rb', line 6 def output @output end |
Class Method Details
.included(base) ⇒ Object
8 9 10 |
# File 'lib/actionizer.rb', line 8 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#fail!(params = {}) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/actionizer.rb', line 35 def fail!(params = {}) params.each_pair { |key, value| output[key] = value } output.fail raise Actionizer::Failure.new('Failed!', output) end |
#initialize(initial_input = {}) ⇒ Object
30 31 32 33 |
# File 'lib/actionizer.rb', line 30 def initialize(initial_input = {}) @input = Hashie::Mash.new(initial_input) @output = Actionizer::Result.new end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
59 60 61 |
# File 'lib/actionizer.rb', line 59 def respond_to_missing?(method_name, _include_private = false) method_name.to_s.end_with?('_or_fail') end |