Class: GLCommand::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/gl_command/context.rb

Direct Known Subclasses

ChainableContext

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#errorObject

Returns the value of attribute error.



25
26
27
# File 'lib/gl_command/context.rb', line 25

def error
  @error
end

#full_error_messageObject



68
69
70
71
72
# File 'lib/gl_command/context.rb', line 68

def full_error_message
  return nil if @full_error_message.blank? && error.blank?

  ContextInspect.error(defined?(@full_error_message) ? @full_error_message : @error)
end

#klassObject (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

#argumentsObject



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

Returns:

  • (Boolean)


34
35
36
# File 'lib/gl_command/context.rb', line 34

def chain?
  false
end

#errorsObject

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: #{full_error_message}") if add_command_error?
  current_errors
end

#failure?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


38
39
40
# File 'lib/gl_command/context.rb', line 38

def in_chain?
  @in_chain
end

#inspectObject

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_raiseObject

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(full_error_message)
  else
    error
  end
end

#no_notify?Boolean

If a command is no_notify within another command call!, inside_no_notify_error? is true

Returns:

  • (Boolean)


64
65
66
# File 'lib/gl_command/context.rb', line 64

def no_notify?
  @no_notify.presence || inside_no_notify_error?
end

#opts_hashObject

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/gl_command/context.rb', line 50

def raise_errors?
  @raise_errors
end

#returnsObject



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?

Returns:

  • (Boolean)


58
59
60
# File 'lib/gl_command/context.rb', line 58

def success?
  !failure?
end

#to_hObject



76
77
78
# File 'lib/gl_command/context.rb', line 76

def to_h
  arguments.merge(returns)
end