Class: Interactor::Schema::Context

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema = []) ⇒ Context

Returns a new instance of Context.



10
11
12
13
14
# File 'lib/interactor/schema/context.rb', line 10

def initialize(schema = [])
  @schema = schema
  @table = {}
  define_schema_methods(schema)
end

Class Method Details

.build(context = {}, schema = []) ⇒ Object



4
5
6
7
8
# File 'lib/interactor/schema/context.rb', line 4

def self.build(context = {}, schema = [])
  self === context ? context : new(schema).tap do |instance|
    instance.assign(context)
  end
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
# File 'lib/interactor/schema/context.rb', line 16

def [](key)
  @table[key]
end

#_calledObject



49
50
51
# File 'lib/interactor/schema/context.rb', line 49

def _called
  @called ||= []
end

#_schemaObject



53
54
55
# File 'lib/interactor/schema/context.rb', line 53

def _schema
  @schema
end

#assign(context) ⇒ Object



20
21
22
23
# File 'lib/interactor/schema/context.rb', line 20

def assign(context)
  filtered_context = context.select { |k, _| _schema.include?(k) }
  @table.merge!(filtered_context)
end

#called!(interactor) ⇒ Object



39
40
41
# File 'lib/interactor/schema/context.rb', line 39

def called!(interactor)
  _called << interactor
end

#fail!(context = {}) ⇒ Object

Raises:

  • (Interactor::Failure)


33
34
35
36
37
# File 'lib/interactor/schema/context.rb', line 33

def fail!(context = {})
  @table.merge!(context)
  @failure = true
  raise Interactor::Failure, self
end

#failure?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/interactor/schema/context.rb', line 29

def failure?
  @failure || false
end

#rollback!Object



43
44
45
46
47
# File 'lib/interactor/schema/context.rb', line 43

def rollback!
  return false if @rolled_back
  _called.reverse_each(&:rollback)
  @rolled_back = true
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  !failure?
end

#to_hObject



57
58
59
# File 'lib/interactor/schema/context.rb', line 57

def to_h
  @table
end