Class: Transitions::State

Inherits:
Object
  • Object
show all
Defined in:
lib/transitions/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ State

Returns a new instance of State.



27
28
29
30
31
32
33
# File 'lib/transitions/state.rb', line 27

def initialize(name, options = {})
  @name = name
  if machine = options.delete(:machine)
    machine.klass.define_state_query_method(name)
  end
  update(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/transitions/state.rb', line 25

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/transitions/state.rb', line 25

def options
  @options
end

Instance Method Details

#==(state) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/transitions/state.rb', line 35

def ==(state)
  if state.is_a? Symbol
    name == state
  else
    name == state.name
  end
end

#call_action(action, record) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/transitions/state.rb', line 43

def call_action(action, record)
  action = @options[action]
  case action
  when Symbol, String
    record.send(action)
  when Proc
    action.call(record)
  end
end

#display_nameObject



53
54
55
# File 'lib/transitions/state.rb', line 53

def display_name
  @display_name ||= name.to_s.gsub(/_/, ' ').capitalize
end

#for_selectObject



57
58
59
# File 'lib/transitions/state.rb', line 57

def for_select
  [display_name, name.to_s]
end

#update(options = {}) ⇒ Object



61
62
63
64
65
# File 'lib/transitions/state.rb', line 61

def update(options = {})
  @display_name = options.delete(:display) if options.key?(:display)
  @options = options
  self
end