Class: ConvenientService::RSpec::Helpers::Classes::WrapMethod::Entities::WrappedMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity:, method:, observe_middleware:) ⇒ void

Parameters:

  • entity (Object)

    Can be any type.

  • method (String)
  • observe_middleware (Class)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 40

def initialize(entity:, method:, observe_middleware:)
  @entity = entity
  @method = method
  @observe_middleware = observe_middleware

  ##
  # TODO: When middleware is NOT used yet.
  #
  observable_middleware.middleware_events[:before_chain_next].add_observer(self)
  observable_middleware.middleware_events[:after_chain_next].add_observer(self)

  ##
  # TODO: Consider `ObjectSpace.define_finalizer`? Is it really needed?
  #
  # observable_middleware.middleware_events[:before_chain_next].delete_observer(self)
  # observable_middleware.middleware_events[:after_chain_next].delete_observer(self)
end

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



20
21
22
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 20

def entity
  @entity
end

#methodObject (readonly)

Returns the value of attribute method.



26
27
28
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 26

def method
  @method
end

#observe_middlewareObject (readonly)

Returns the value of attribute observe_middleware.



32
33
34
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 32

def observe_middleware
  @observe_middleware
end

Instance Method Details

#==(other) ⇒ Boolean?

Parameters:

  • other (Object)

    Can be any type.

Returns:

  • (Boolean, nil)


184
185
186
187
188
189
190
191
192
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 184

def ==(other)
  return unless other.instance_of?(self.class)

  return false if entity != other.entity
  return false if method != other.method
  return false if observe_middleware != other.observe_middleware

  true
end

#call(*args, **kwargs, &block) ⇒ Object

Returns Can be any type.

Parameters:

  • args (Array<Object>)
  • kwargs (Hash{Symbol => Object})
  • block (Proc, nil)

Returns:

  • (Object)

    Can be any type.



102
103
104
105
106
107
108
109
110
111
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 102

def call(*args, **kwargs, &block)
  entity.__send__(method, *args, **kwargs, &block)
rescue => exception
  @chain_exception = exception

  ##
  # NOTE: `raise` with no args inside `rescue` reraises rescued exception.
  #
  raise
end

#chain_argsArray



144
145
146
147
148
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 144

def chain_args
  ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :args) unless chain_called?

  @chain_arguments.args
end

#chain_blockProc?



164
165
166
167
168
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 164

def chain_block
  ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :block) unless chain_called?

  @chain_arguments.block
end

#chain_called?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 126

def chain_called?
  Utils.to_bool(defined? @chain_called)
end

#chain_exceptionStandardError?



174
175
176
177
178
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 174

def chain_exception
  ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :exception) unless chain_called?

  @chain_exception
end

#chain_kwargsHash



154
155
156
157
158
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 154

def chain_kwargs
  ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :kwargs) unless chain_called?

  @chain_arguments.kwargs
end

#chain_valueObject

Returns Can be any type.



134
135
136
137
138
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 134

def chain_value
  ::ConvenientService.raise Exceptions::ChainAttributePreliminaryAccess.new(attribute: :value) unless chain_called?

  @chain_value
end

#handle_after_chain_next(value, _arguments) ⇒ Object



71
72
73
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 71

def handle_after_chain_next(value, _arguments)
  @chain_value = value
end

#handle_before_chain_next(arguments) ⇒ Object



62
63
64
65
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 62

def handle_before_chain_next(arguments)
  @chain_called = true
  @chain_arguments = arguments
end

#klassClass

Returns:

  • (Class)


85
86
87
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 85

def klass
  @klass ||= Utils::Object.clamp_class(entity)
end

#observable_middlewareClass

Returns:

  • (Class)


92
93
94
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 92

def observable_middleware
  @observable_middleware ||= klass.middlewares(method, scope: scope).to_a.find { |other| other == observe_middleware.observable }
end

#reset!void

This method returns an undefined value.



116
117
118
119
120
121
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 116

def reset!
  remove_instance_variable(:@chain_called) if defined? @chain_called
  remove_instance_variable(:@chain_value) if defined? @chain_value
  remove_instance_variable(:@chain_arguments) if defined? @chain_arguments
  remove_instance_variable(:@chain_exception) if defined? @chain_exception
end

#scopeString

Returns:

  • (String)


78
79
80
# File 'lib/convenient_service/rspec/helpers/classes/wrap_method/entities/wrapped_method.rb', line 78

def scope
  @scope ||= Utils::Object.resolve_type(entity).to_sym
end