Module: Crossbeam::Output::ClassMethods
- Defined in:
- lib/crossbeam/output.rb
Overview
Methods to load in the service object as class methods
Constant Summary collapse
- CB_ALLOWED_OUTPUTS =
[NilClass, String, Symbol].freeze
Instance Method Summary collapse
-
#allowed_output?(output_type) ⇒ Boolean
Determine if the output attribute type is allowed.
-
#build_error_list ⇒ Void
Add errors to @result.errors.
-
#output(param) ⇒ String, Symbol
Used to specify an attribute/instance variable that should be used too return instead of @result.
-
#output? ⇒ Boolean
Used to determine if a output parameter has been set or not.
-
#output_param ⇒ String, ...
Used hold the parameter which can/will be used instead of @result.
-
#reassign_results ⇒ Void
Reassign result, unless errors.
-
#set_results_output ⇒ Hash
Determine the output to return if the instance_variable exists in the klass.
-
#specified_output? ⇒ Boolean
Does the klass have an assigned output.
Instance Method Details
#allowed_output?(output_type) ⇒ Boolean
Determine if the output attribute type is allowed
38 39 40 |
# File 'lib/crossbeam/output.rb', line 38 def allowed_output?(output_type) CB_ALLOWED_OUTPUTS.include?(output_type.class) end |
#build_error_list ⇒ Void
Add errors to @result.errors
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/crossbeam/output.rb', line 62 def build_error_list return if @klass.nil? return unless @klass&.errors&.any? @klass.errors.each do |error| # options is usually passed with an ActiveRecord validation error # @example: # <attribute=age, type=greater_than_or_equal_to, options={:value=>15, :count=>18}> @result.errors.add(error.attribute, error.) end @klass.errors.clear reassign_results end |
#output(param) ⇒ String, Symbol
Used to specify an attribute/instance variable that should be used too return instead of @result
28 29 30 31 32 |
# File 'lib/crossbeam/output.rb', line 28 def output(param) raise(ArguementError, 'A string or symbol is require for output') unless allowed_output?(param) @output_param = param end |
#output? ⇒ Boolean
Used to determine if a output parameter has been set or not
45 46 47 |
# File 'lib/crossbeam/output.rb', line 45 def output? !output_param.nil? end |
#output_param ⇒ String, ...
Used hold the parameter which can/will be used instead of @result
94 95 96 |
# File 'lib/crossbeam/output.rb', line 94 def output_param @output_param ||= nil end |
#reassign_results ⇒ Void
Reassign result, unless errors
79 80 81 82 |
# File 'lib/crossbeam/output.rb', line 79 def reassign_results @result.results = nil @result.results = @klass.instance_variable_get(:"@#{output_param}") if specified_output? end |
#set_results_output ⇒ Hash
Determine the output to return if the instance_variable exists in the klass
52 53 54 55 56 57 |
# File 'lib/crossbeam/output.rb', line 52 def set_results_output return unless @klass return unless output? && @klass.instance_variable_defined?(:"@#{output_param}") @result.results = @klass.instance_variable_get(:"@#{output_param}") end |
#specified_output? ⇒ Boolean
Does the klass have an assigned output
87 88 89 |
# File 'lib/crossbeam/output.rb', line 87 def specified_output? output? && @klass.instance_variable_defined?(:"@#{output_param}") end |