Class: AASM::Core::State

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of State.



7
8
9
10
11
12
13
# File 'lib/aasm/core/state.rb', line 7

def initialize(name, klass, state_machine, options={})
  @name = name
  @klass = klass
  @state_machine = state_machine
  @default_display_name = name.to_s.gsub(/_/, ' ').capitalize
  update(options)
end

Instance Attribute Details

#default_display_nameObject (readonly)

Returns the value of attribute default_display_name.



5
6
7
# File 'lib/aasm/core/state.rb', line 5

def default_display_name
  @default_display_name
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/aasm/core/state.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/aasm/core/state.rb', line 5

def options
  @options
end

#state_machineObject (readonly)

Returns the value of attribute state_machine.



5
6
7
# File 'lib/aasm/core/state.rb', line 5

def state_machine
  @state_machine
end

Instance Method Details

#<=>(state) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/aasm/core/state.rb', line 36

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

#==(state) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/aasm/core/state.rb', line 28

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

#display_nameObject



57
58
59
60
61
62
63
64
65
# File 'lib/aasm/core/state.rb', line 57

def display_name
  @display_name = begin
    if Module.const_defined?(:I18n)
      localized_name
    else
      @default_display_name
    end
  end
end

#fire_callbacks(action, record, *args) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/aasm/core/state.rb', line 48

def fire_callbacks(action, record, *args)
  action = @options[action]
  catch :halt_aasm_chain do
    action.is_a?(Array) ?
            action.each {|a| _fire_callbacks(a, record, args)} :
            _fire_callbacks(action, record, args)
  end
end

#for_selectObject



72
73
74
# File 'lib/aasm/core/state.rb', line 72

def for_select
  [display_name, name.to_s]
end

#initialize_copy(orig) ⇒ Object

called internally by Ruby 1.9 after clone()



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/aasm/core/state.rb', line 16

def initialize_copy(orig)
  super
  @options = {}
  orig.options.each_pair do |name, setting|
    @options[name] = if setting.is_a?(Hash) || setting.is_a?(Array)
                       setting.dup
                     else
                       setting
                     end
  end
end

#localized_nameObject Also known as: human_name



67
68
69
# File 'lib/aasm/core/state.rb', line 67

def localized_name
  AASM::Localizer.new.human_state_name(@klass, self)
end

#to_sObject



44
45
46
# File 'lib/aasm/core/state.rb', line 44

def to_s
  name.to_s
end