class Ni::Context

Attributes

as_result[RW]
continue_from[RW]
current_action[RW]
current_interactor[RW]
errors[RW]
external_system_uid[RW]
halt_execution[RW]
skip_the_rest_chain[RW]
system_uid[RW]

Public Class Methods

new(interactor, action, system_uid = nil) click to toggle source
# File lib/ni/context.rb, line 41
def initialize(interactor, action, system_uid = nil)
  self.external_system_uid = system_uid
  self.system_uid = system_uid || SecureRandom.hex(15)
  self.current_interactor = interactor
  self.current_action = action
  self.errors = Errors.new
  self.as_result = false
  @data = {}
  @success = true
  @terminated = false
end

Public Instance Methods

[](name) click to toggle source
# File lib/ni/context.rb, line 155
def [](name)
  unless allow_to_read?(name)
    raise "The `#{name}` is not allowed to read"
  end

  raw_get(name)
end
[]=(name, value) click to toggle source
# File lib/ni/context.rb, line 163
def []=(name, value)
  unless allow_to_write?(name)
    raise "The `#{name}` is not allowed to write"
  end

  raw_set(name, value)
end
allow_to_read?(name) click to toggle source
# File lib/ni/context.rb, line 201
def allow_to_read?(name)
  as_result || self.current_interactor.allow_to_read?(self.current_action, name)
end
allow_to_write?(name) click to toggle source
# File lib/ni/context.rb, line 205
def allow_to_write?(name)
  as_result || self.current_interactor.allow_to_write?(self.current_action, name)
end
assign_current_interactor!(interactor) click to toggle source
# File lib/ni/context.rb, line 94
def assign_current_interactor!(interactor)
  self.current_interactor = interactor
end
assign_data!(params) click to toggle source
# File lib/ni/context.rb, line 90
def assign_data!(params)
  @data.merge!(params)
end
can_perform_next_step?() click to toggle source
# File lib/ni/context.rb, line 77
def can_perform_next_step?
  success? && !terminated?
end
cancel!() click to toggle source
# File lib/ni/context.rb, line 73
def cancel!
  @success = false
end
canceled?() click to toggle source
# File lib/ni/context.rb, line 61
def canceled?
  !@success && valid?
end
chain_skipped?() click to toggle source
# File lib/ni/context.rb, line 115
def chain_skipped?
  skip_the_rest_chain.present?
ensure
  # I now it's a bad Idea to mutate state in predicates but this operation should be atomic
  self.skip_the_rest_chain = false 
end
continue_from!(name_or_interactor) click to toggle source
# File lib/ni/context.rb, line 98
def continue_from!(name_or_interactor)
  name = name_or_interactor.is_a?(Class) ? name_or_interactor.interactor_id! : name_or_interactor
  self.continue_from = name
end
execution_halted?() click to toggle source
# File lib/ni/context.rb, line 107
def execution_halted?
  halt_execution.present?
end
failed?() click to toggle source
# File lib/ni/context.rb, line 57
def failed?
  !@success || !valid?
end
failure!() click to toggle source
# File lib/ni/context.rb, line 69
def failure!
  @success = false
end
fetch(*args) click to toggle source
# File lib/ni/context.rb, line 197
def fetch(*args)
  args.map { |name| self[name] }
end
halt_execution!() click to toggle source
# File lib/ni/context.rb, line 103
def halt_execution!
  self.halt_execution = true
end
has_key?(name) click to toggle source
# File lib/ni/context.rb, line 185
def has_key?(name)
  @data.has_key?(name.to_sym)
end
invalid?() click to toggle source
# File lib/ni/context.rb, line 193
def invalid?
  !valid?
end
method_missing(name, *args, &block) click to toggle source
# File lib/ni/context.rb, line 209
def method_missing(name, *args, &block)
  if name.to_s.ends_with?('=')
    self[name[0..-2].to_sym] = args.first
  else
    self[name]
  end
end
raw_get(name) click to toggle source

raw_get was defined only for internal purposes only. Please do not use it in your code

# File lib/ni/context.rb, line 172
def raw_get(name)
  @data[name.to_sym]
end
raw_set(name, value) click to toggle source

raw_set was defined only for internal purposes only. Please do not use it in your code

# File lib/ni/context.rb, line 177
def raw_set(name, value)
  @data[name.to_sym] = value
end
respond_to?(name, include_private=false) click to toggle source
Calls superclass method
# File lib/ni/context.rb, line 181
def respond_to?(name, include_private=false)
  super || has_key?(name)
end
resultify!() click to toggle source
# File lib/ni/context.rb, line 134
def resultify!
  self.as_result = true

  self
end
should_be_restored?() click to toggle source
# File lib/ni/context.rb, line 130
def should_be_restored?
  wait_for_execution? || self.external_system_uid.present?
end
skip_the_rest_chain!() click to toggle source
# File lib/ni/context.rb, line 111
def skip_the_rest_chain!
  self.skip_the_rest_chain = true
end
success!() click to toggle source
# File lib/ni/context.rb, line 81
def success!
  @terminated = true
end
success?() click to toggle source
# File lib/ni/context.rb, line 53
def success?
  @success && valid?
end
terminate!() click to toggle source
# File lib/ni/context.rb, line 85
def terminate!
  @success = false
  @terminated = true
end
terminated?() click to toggle source
# File lib/ni/context.rb, line 65
def terminated?
  @terminated
end
valid?() click to toggle source
# File lib/ni/context.rb, line 189
def valid?
  errors.empty?
end
wait_completed!() click to toggle source
# File lib/ni/context.rb, line 122
def wait_completed!
  self.continue_from = nil
end
wait_for_execution?() click to toggle source
# File lib/ni/context.rb, line 126
def wait_for_execution?
  continue_from.present?
end
within_interactor(interactor, action) { || ... } click to toggle source
# File lib/ni/context.rb, line 140
def within_interactor(interactor, action)
  stored_interactor = self.current_interactor
  stored_action = self.current_action

  self.current_interactor = interactor
  self.current_action = action

  result = yield

  self.current_interactor = stored_interactor
  self.current_action = stored_action

  result
end