Class: Ohm::StatefulModel

Inherits:
Model
  • Object
show all
Defined in:
lib/ohm/stateful_model.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ StatefulModel

Returns a new instance of StatefulModel.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ohm/stateful_model.rb', line 30

def initialize(*args)
  if self.class.state_machine_class
    @state_machine = self.class.state_machine_class.new

    # define Ohm::Model attribute
    self.class.attribute(state_machine_name)

    # override state setting method so it updates both the attributes hash
    # and the state machine's state
    define_singleton_method(:"#{state_machine_name}=") do |new_state|
      @attributes[state_machine_name] = new_state
      @state_machine.state = new_state
    end

    # hook every transition so we can update the attributes hash
    @state_machine.after_transition_proc = lambda do |obj, transition|
      update_attributes(state_machine_name => obj.state)
    end

    super
    initialize_value
  else
    super
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/ohm/stateful_model.rb', line 60

def method_missing(method, *args, &block)
  if @state_machine.respond_to?(method)
    @state_machine.send(method, *args, &block)
  else
    raise NoMethodError.new("undefined method `#{method}' for #{self.class.name}")
  end
end

Class Attribute Details

.state_machine_classObject (readonly)

Returns the value of attribute state_machine_class.



11
12
13
# File 'lib/ohm/stateful_model.rb', line 11

def state_machine_class
  @state_machine_class
end

.state_machine_nameObject (readonly)

Returns the value of attribute state_machine_name.



11
12
13
# File 'lib/ohm/stateful_model.rb', line 11

def state_machine_name
  @state_machine_name
end

Instance Attribute Details

#state_machineObject (readonly)

Returns the value of attribute state_machine.



28
29
30
# File 'lib/ohm/stateful_model.rb', line 28

def state_machine
  @state_machine
end

Class Method Details

.use_state_machine(state_machine_class, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ohm/stateful_model.rb', line 13

def use_state_machine(state_machine_class, options = {})
  if @state_machine_class
    raise "A state machine is already in use for this model"
  else
    if state_machine_class.ancestors.include?(Ohm::State)
      @state_machine_class = state_machine_class
      @state_machine_name = options[:attribute_name]
    else
      raise "#{state_machine_class} must inherit from Ohm::State"
    end
  end
end

Instance Method Details

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ohm/stateful_model.rb', line 56

def respond_to_missing?(method)
  @state_machine.respond_to?(method)
end