Class: Igniter::Embed::Contractable::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter/embed/contractable/config.rb

Defined Under Namespace

Classes: CapabilityAttachment, EventHandler

Constant Summary collapse

FAILURE_EVENTS =
%i[primary_error candidate_error acceptance_failure store_error].freeze
SUPPORTED_EVENTS =
(
  %i[primary_success primary_error candidate_success candidate_error divergence acceptance_failure store_error observation] +
  [:failure]
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ Config

Returns a new instance of Config.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/igniter/embed/contractable/config.rb', line 23

def initialize(name:)
  @name = name.to_sym
  @role = nil
  @stage = :captured
  @stage_explicit = false
  @async_enabled = true
  @sample_value = 1.0
  @async_adapter = nil
  @metadata_value = {}
  @input_redactor = ->(*, **) { {} }
  @redaction_input_policy = :custom
  @acceptance_policy = :exact
  @acceptance_options = {}
  @event_handlers = []
  @capability_attachments = []
  @clock_callable = Time
end

Instance Attribute Details

#acceptance_optionsObject

Returns the value of attribute acceptance_options.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def acceptance_options
  @acceptance_options
end

#acceptance_policyObject

Returns the value of attribute acceptance_policy.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def acceptance_policy
  @acceptance_policy
end

#candidate_callableObject

Returns the value of attribute candidate_callable.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def candidate_callable
  @candidate_callable
end

#candidate_normalizerObject

Returns the value of attribute candidate_normalizer.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def candidate_normalizer
  @candidate_normalizer
end

#capability_attachmentsObject (readonly)

Returns the value of attribute capability_attachments.



16
17
18
# File 'lib/igniter/embed/contractable/config.rb', line 16

def capability_attachments
  @capability_attachments
end

#clock_callableObject

Returns the value of attribute clock_callable.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def clock_callable
  @clock_callable
end

#event_handlersObject (readonly)

Returns the value of attribute event_handlers.



16
17
18
# File 'lib/igniter/embed/contractable/config.rb', line 16

def event_handlers
  @event_handlers
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/igniter/embed/contractable/config.rb', line 16

def name
  @name
end

#observation_callbackObject

Returns the value of attribute observation_callback.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def observation_callback
  @observation_callback
end

#primary_callableObject

Returns the value of attribute primary_callable.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def primary_callable
  @primary_callable
end

#primary_normalizerObject

Returns the value of attribute primary_normalizer.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def primary_normalizer
  @primary_normalizer
end

#redaction_input_policyObject

Returns the value of attribute redaction_input_policy.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def redaction_input_policy
  @redaction_input_policy
end

#store_adapterObject

Returns the value of attribute store_adapter.



17
18
19
# File 'lib/igniter/embed/contractable/config.rb', line 17

def store_adapter
  @store_adapter
end

Instance Method Details

#accept(policy = nil, **options) ⇒ Object



151
152
153
154
155
156
# File 'lib/igniter/embed/contractable/config.rb', line 151

def accept(policy = nil, **options)
  return acceptance_policy unless policy

  self.acceptance_policy = policy.to_sym
  self.acceptance_options = options
end

#async(value = nil) ⇒ Object



80
81
82
83
84
# File 'lib/igniter/embed/contractable/config.rb', line 80

def async(value = nil)
  return @async_enabled if value.nil?

  @async_enabled = !!value
end

#async_adapter(value = nil) ⇒ Object



98
99
100
101
102
# File 'lib/igniter/embed/contractable/config.rb', line 98

def async_adapter(value = nil)
  return @async_adapter || default_async_adapter unless value

  @async_adapter = value
end

#candidate(callable = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/igniter/embed/contractable/config.rb', line 48

def candidate(callable = nil)
  return candidate_callable unless callable

  self.candidate_callable = callable
  apply_core_contractable_defaults(callable)
end

#capability(name, target) ⇒ Object

Raises:



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/igniter/embed/contractable/config.rb', line 139

def capability(name, target)
  capability_name = name.to_sym
  raise SugarError, "capability :#{capability_name} is already configured" if capability_configured?(capability_name)

  capability_attachments << CapabilityAttachment.new(
    name: capability_name,
    target: target,
    kind: capability_kind(target)
  )
  self
end

#handlers_for(event) ⇒ Object



135
136
137
# File 'lib/igniter/embed/contractable/config.rb', line 135

def handlers_for(event)
  event_handlers.select { |handler| handler.event == event.to_sym }
end

#metadata(value = nil, &block) ⇒ Object



110
111
112
113
114
# File 'lib/igniter/embed/contractable/config.rb', line 110

def (value = nil, &block)
  return @metadata_value unless value || block

  @metadata_value = block || value
end

#metadata_payloadObject



180
181
182
# File 'lib/igniter/embed/contractable/config.rb', line 180

def 
  .respond_to?(:call) ? .call : 
end

#normalize_candidate(callable = nil, &block) ⇒ Object



61
62
63
64
65
# File 'lib/igniter/embed/contractable/config.rb', line 61

def normalize_candidate(callable = nil, &block)
  return candidate_normalizer unless callable || block

  self.candidate_normalizer = callable || block
end

#normalize_inputs(args, kwargs) ⇒ Object



176
177
178
# File 'lib/igniter/embed/contractable/config.rb', line 176

def normalize_inputs(args, kwargs)
  input_redactor.call(*args, **kwargs)
end

#normalize_primary(callable = nil, &block) ⇒ Object



55
56
57
58
59
# File 'lib/igniter/embed/contractable/config.rb', line 55

def normalize_primary(callable = nil, &block)
  return primary_normalizer unless callable || block

  self.primary_normalizer = callable || block
end

#nowObject



184
185
186
# File 'lib/igniter/embed/contractable/config.rb', line 184

def now
  clock_callable.respond_to?(:now) ? clock_callable.now : clock_callable.call
end

#observed_service?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/igniter/embed/contractable/config.rb', line 166

def observed_service?
  candidate_callable.nil?
end

#on(event, callable = nil, &block) ⇒ Object

Raises:



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/igniter/embed/contractable/config.rb', line 122

def on(event, callable = nil, &block)
  handler = callable || block
  raise SugarError, "on :#{event} requires a block or callable" unless handler

  normalized_event = event.to_sym
  raise SugarError, "unsupported event :#{event}" unless SUPPORTED_EVENTS.include?(normalized_event)

  event_names(normalized_event).each do |event_name|
    event_handlers << EventHandler.new(event: event_name, handler: handler, source: normalized_event)
  end
  self
end

#on_observation(callable = nil, &block) ⇒ Object



116
117
118
119
120
# File 'lib/igniter/embed/contractable/config.rb', line 116

def on_observation(callable = nil, &block)
  return observation_callback unless callable || block

  self.observation_callback = callable || block
end

#primary(callable = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/igniter/embed/contractable/config.rb', line 41

def primary(callable = nil)
  return primary_callable unless callable

  self.primary_callable = callable
  apply_core_contractable_defaults(callable)
end

#redact_inputs(callable = nil, &block) ⇒ Object



104
105
106
107
108
# File 'lib/igniter/embed/contractable/config.rb', line 104

def redact_inputs(callable = nil, &block)
  return @input_redactor unless callable || block

  @input_redactor = callable || block
end

#role(value = nil) ⇒ Object



67
68
69
70
71
# File 'lib/igniter/embed/contractable/config.rb', line 67

def role(value = nil)
  return @role || inferred_role unless value

  @role = value.to_sym
end

#sample(value = nil) ⇒ Object



86
87
88
89
90
# File 'lib/igniter/embed/contractable/config.rb', line 86

def sample(value = nil)
  return @sample_value if value.nil?

  @sample_value = value
end

#sampled?Boolean

Returns:

  • (Boolean)


170
171
172
173
174
# File 'lib/igniter/embed/contractable/config.rb', line 170

def sampled?
  value = sample_value
  value = value.call if value.respond_to?(:call)
  value.to_f >= 1.0 || rand < value.to_f
end

#stage(value = nil) ⇒ Object



73
74
75
76
77
78
# File 'lib/igniter/embed/contractable/config.rb', line 73

def stage(value = nil)
  return @stage unless value

  @stage = value.to_sym
  @stage_explicit = true
end

#store(value = nil) ⇒ Object



92
93
94
95
96
# File 'lib/igniter/embed/contractable/config.rb', line 92

def store(value = nil)
  return store_adapter unless value

  self.store_adapter = value
end

#validate!Object

Raises:

  • (ArgumentError)


158
159
160
161
162
163
164
# File 'lib/igniter/embed/contractable/config.rb', line 158

def validate!
  raise ArgumentError, "contractable #{name} requires a primary callable" unless primary_callable
  raise ArgumentError, "contractable #{name} requires normalize_primary" unless primary_normalizer

  return if observed_service?
  raise ArgumentError, "contractable #{name} requires normalize_candidate when candidate is configured" unless candidate_normalizer
end