Class: GLCommand::Context
- Inherits:
-
Object
- Object
- GLCommand::Context
- Defined in:
- lib/gl_command/context.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#error ⇒ Object
Returns the value of attribute error.
- #full_error_message ⇒ Object
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #arguments ⇒ Object
-
#assign_callable(callable) ⇒ Object
This is what makes validation work.
- #assign_parameters(skip_unknown_parameters: false, **arguments_and_returns) ⇒ Object
- #chain? ⇒ Boolean
-
#errors ⇒ Object
If someone calls #errors, they expect to get the errors! Include the non-validation error, if it exists.
- #failure? ⇒ Boolean
- #in_chain? ⇒ Boolean
-
#initialize(klass, raise_errors: false, skip_unknown_parameters: false, in_chain: false, **arguments_and_returns) ⇒ Context
constructor
A new instance of Context.
-
#inspect ⇒ Object
Set up to make errors visible when specs fail.
-
#no_notifiable_error_to_raise ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#no_notify? ⇒ Boolean
If a command is no_notify within another command call!, inside_no_notify_error? is true.
-
#opts_hash ⇒ Object
TODO: figure out a different place to pass skip_unknown_parameters than initialize, it doesn’t make sense to include in the opts hash.
- #raise_errors? ⇒ Boolean
- #returns ⇒ Object
- #success? ⇒ Boolean (also: #successful?)
- #to_h ⇒ Object
Constructor Details
#initialize(klass, raise_errors: false, skip_unknown_parameters: false, in_chain: false, **arguments_and_returns) ⇒ Context
Returns a new instance of Context.
9 10 11 12 13 14 15 16 17 |
# File 'lib/gl_command/context.rb', line 9 def initialize(klass, raise_errors: false, skip_unknown_parameters: false, in_chain: false, **arguments_and_returns) @klass = klass @raise_errors = raise_errors.nil? ? false : raise_errors @in_chain = in_chain @klass.arguments_and_returns.each { |key| singleton_class.class_eval { attr_accessor key } } initialize_chain_context(**arguments_and_returns) if chain? assign_parameters(skip_unknown_parameters:, **arguments_and_returns) end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
25 26 27 |
# File 'lib/gl_command/context.rb', line 25 def error @error end |
#full_error_message ⇒ Object
68 69 70 71 72 |
# File 'lib/gl_command/context.rb', line 68 def return nil if @full_error_message.blank? && error.blank? ContextInspect.error(defined?(@full_error_message) ? @full_error_message : @error) end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
25 26 27 |
# File 'lib/gl_command/context.rb', line 25 def klass @klass end |
Instance Method Details
#arguments ⇒ Object
46 47 48 |
# File 'lib/gl_command/context.rb', line 46 def arguments @klass.arguments.index_with { |rattr| send(rattr) } end |
#assign_callable(callable) ⇒ Object
This is what makes validation work
92 93 94 |
# File 'lib/gl_command/context.rb', line 92 def assign_callable(callable) @callable = callable end |
#assign_parameters(skip_unknown_parameters: false, **arguments_and_returns) ⇒ Object
85 86 87 88 89 |
# File 'lib/gl_command/context.rb', line 85 def assign_parameters(skip_unknown_parameters: false, **arguments_and_returns) arguments_and_returns.each do |arg, val| assign_parameter_val(arg, val, skip_unknown_parameters:) end end |
#chain? ⇒ Boolean
34 35 36 |
# File 'lib/gl_command/context.rb', line 34 def chain? false end |
#errors ⇒ Object
If someone calls #errors, they expect to get the errors! Include the non-validation error, if it exists
29 30 31 32 |
# File 'lib/gl_command/context.rb', line 29 def errors current_errors&.add(:base, "Command Error: #{}") if add_command_error? current_errors end |
#failure? ⇒ Boolean
54 55 56 |
# File 'lib/gl_command/context.rb', line 54 def failure? @failure || current_errors.present? || @full_error_message.present? || false end |
#in_chain? ⇒ Boolean
38 39 40 |
# File 'lib/gl_command/context.rb', line 38 def in_chain? @in_chain end |
#inspect ⇒ Object
Set up to make errors visible when specs fail
81 82 83 |
# File 'lib/gl_command/context.rb', line 81 def inspect "<#{self.class.name} #{inspect_values.join(', ')}, class=#{klass}>" end |
#no_notifiable_error_to_raise ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
122 123 124 125 126 127 128 |
# File 'lib/gl_command/context.rb', line 122 def no_notifiable_error_to_raise if no_notify? && !inside_no_notify_error? GLCommand::CommandNoNotifyError.new() else error end end |
#no_notify? ⇒ Boolean
If a command is no_notify within another command call!, inside_no_notify_error? is true
64 65 66 |
# File 'lib/gl_command/context.rb', line 64 def no_notify? @no_notify.presence || inside_no_notify_error? end |
#opts_hash ⇒ Object
TODO: figure out a different place to pass skip_unknown_parameters than initialize, it doesn’t make sense to include in the opts hash
21 22 23 |
# File 'lib/gl_command/context.rb', line 21 def opts_hash { raise_errors: raise_errors? } end |
#raise_errors? ⇒ Boolean
50 51 52 |
# File 'lib/gl_command/context.rb', line 50 def raise_errors? @raise_errors end |
#returns ⇒ Object
42 43 44 |
# File 'lib/gl_command/context.rb', line 42 def returns @klass.returns.index_with { |rattr| send(rattr) } end |
#success? ⇒ Boolean Also known as: successful?
58 59 60 |
# File 'lib/gl_command/context.rb', line 58 def success? !failure? end |
#to_h ⇒ Object
76 77 78 |
# File 'lib/gl_command/context.rb', line 76 def to_h arguments.merge(returns) end |