Class: SimpleStates::Event

Inherits:
Struct
  • Object
show all
Defined in:
lib/simple_states/event.rb

Constant Summary collapse

MSGS =
{
  invalid_state: 'If multiple target states are defined, then a valid target state must be passed as an attribute ({ state: :state }). %p given, known states: %p.',
  unknown_state: 'Unknown state %p for %p for event %p. Known states are: %p'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/simple_states/event.rb', line 2

def name
  @name
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



2
3
4
# File 'lib/simple_states/event.rb', line 2

def opts
  @opts
end

Instance Method Details

#call(obj, data, opts) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/simple_states/event.rb', line 8

def call(obj, data, opts)
  return false if not ordered?(obj, data) or not applies?(obj, data)

  run_callbacks(:before, obj, data)
  set_attrs(obj, data)
  yield
  obj.save! if opts[:save]

  raise_unknown_state(obj, data) unless known_state?(obj)
  run_callbacks(:after, obj, data)
  obj.save! if opts[:save]
  true
end

#reset(obj) ⇒ Object



22
23
24
# File 'lib/simple_states/event.rb', line 22

def reset(obj)
  Array(opts[:to]).each { |state| set_attr(obj, :"#{state}_at", nil) }
end