Class: ServiceActor::Result

Inherits:
BasicObject
Defined in:
lib/service_actor/result.rb

Overview

Represents the context of an actor, holding the data from both its inputs and outputs.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Result

Returns a new instance of Result.



37
38
39
# File 'lib/service_actor/result.rb', line 37

def initialize(data = {})
  @data = data.to_h.transform_keys(&:to_sym)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)

rubocop:disable Metrics/AbcSize



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/service_actor/result.rb', line 144

def method_missing(method_name, *args) # rubocop:disable Metrics/AbcSize
  if method_name.end_with?("?") &&
      data.key?(key = method_name.to_s.chomp("?").to_sym)
    value = data[key]
    value.respond_to?(:empty?) ? !value.empty? : !!value
  elsif method_name.end_with?("=")
    data[method_name.to_s.chomp("=").to_sym] = args.first
  elsif data.key?(method_name)
    data[method_name]
  else
    warn_on_undefined_method_invocation(method_name)
  end
end

Class Method Details

.to_result(data) ⇒ Object



7
8
9
10
11
# File 'lib/service_actor/result.rb', line 7

def to_result(data)
  return data if data.is_a?(self)

  new(data.to_h)
end

Instance Method Details

#[](name) ⇒ Object



91
92
93
94
95
# File 'lib/service_actor/result.rb', line 91

def [](name)
  name = name.to_sym

  data[name]
end

#[]=(key, value) ⇒ Object



97
98
99
100
101
# File 'lib/service_actor/result.rb', line 97

def []=(key, value)
  key = key.to_sym

  data[key] = value
end

#deconstruct_keys(keys) ⇒ Object



114
115
116
117
118
# File 'lib/service_actor/result.rb', line 114

def deconstruct_keys(keys)
  deconstructed_keys = to_h.merge(success: success?, failure: failure?)

  keys ? deconstructed_keys.slice(*keys) : deconstructed_keys
end

#delete!(key) ⇒ Object



103
104
105
106
107
# File 'lib/service_actor/result.rb', line 103

def delete!(key)
  key = key.to_sym

  data.delete(key)
end

#errorObject



75
76
77
# File 'lib/service_actor/result.rb', line 75

def error
  data[:error] || nil
end

#fail!(failure_class = nil, result = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/service_actor/result.rb', line 55

def fail!(failure_class = nil, result = {})
  if failure_class.nil? || failure_class.is_a?(::Hash)
    result = failure_class.to_h
    failure_class = ::ServiceActor::Failure
  end

  data.merge!(result)
  data[:failure] = true

  ::Kernel.raise failure_class, self
end

#failure?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/service_actor/result.rb', line 71

def failure?
  data[:failure] || data[:failure?] || false
end

#inspectObject



45
46
47
# File 'lib/service_actor/result.rb', line 45

def inspect
  "<#{self.class.name} #{to_h}>"
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/service_actor/result.rb', line 85

def key?(name)
  name = name.to_sym

  to_h.key?(name)
end

#merge!(result) ⇒ Object



79
80
81
82
83
# File 'lib/service_actor/result.rb', line 79

def merge!(result)
  data.merge!(result.transform_keys(&:to_sym))

  self
end

#pretty_print(pp) ⇒ Object



49
50
51
52
53
# File 'lib/service_actor/result.rb', line 49

def pretty_print(pp)
  pp.text "#<#{self.class.name} "
  pp.pp to_h
  pp.text ">"
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/service_actor/result.rb', line 109

def respond_to?(method_name, include_private = false)
  self.class.instance_methods.include?(method_name) ||
    respond_to_missing?(method_name, include_private)
end

#success?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/service_actor/result.rb', line 67

def success?
  !failure?
end

#to_hObject



41
42
43
# File 'lib/service_actor/result.rb', line 41

def to_h
  filter_default_output(data)
end