Class: StateFu::Persistence::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/persistence/base.rb

Direct Known Subclasses

ActiveRecord, Attribute, RelaxDB

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binding, field_name) ⇒ Base

Returns a new instance of Base



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/persistence/base.rb', line 16

def initialize( binding, field_name )

  @binding       = binding
  @field_name    = field_name
  @current_state = find_current_state()

  if current_state.nil?
    Logging.warn("undefined state for binding #{binding} on #{object} with field_name #{field_name.inspect}")
    Logging.warn("Machine for #{object} has no states: #{machine}") if machine.states.empty?
  else
    persist!
    Logging.debug("#{object} resumes #{binding.method_name} at #{current_state.name}")
  end
end

Instance Attribute Details

#bindingObject (readonly)

Returns the value of attribute binding



9
10
11
# File 'lib/persistence/base.rb', line 9

def binding
  @binding
end

#current_stateObject

Returns the value of attribute current_state



9
10
11
# File 'lib/persistence/base.rb', line 9

def current_state
  @current_state
end

#field_nameObject (readonly)

Returns the value of attribute field_name



9
10
11
# File 'lib/persistence/base.rb', line 9

def field_name
  @field_name
end

Class Method Details

.prepare_field(klass, field_name) ⇒ Object

define this method in subclasses to do any preparation



12
13
14
# File 'lib/persistence/base.rb', line 12

def self.prepare_field( klass, field_name )
  Logging.warn("Abstract method in #{self}.prepare_field called. Override me!")
end

Instance Method Details

#find_current_stateObject



31
32
33
34
35
36
37
38
39
# File 'lib/persistence/base.rb', line 31

def find_current_state
  string = read_attribute()
  if string.blank?
    machine.initial_state
  else
    state_name = string.to_sym
    state      = machine.states[ state_name ] || raise( StateFu::InvalidStateName, string )
  end
end

#klassObject



53
54
55
# File 'lib/persistence/base.rb', line 53

def klass
  binding.target
end

#machineObject



45
46
47
# File 'lib/persistence/base.rb', line 45

def machine
  binding.machine
end

#objectObject



49
50
51
# File 'lib/persistence/base.rb', line 49

def object
  binding.object
end

#persist!Object



67
68
69
# File 'lib/persistence/base.rb', line 67

def persist!
  write_attribute( value() )
end

#reloadObject



41
42
43
# File 'lib/persistence/base.rb', line 41

def reload
  @current_state = find_current_state()
end

#valueObject



63
64
65
# File 'lib/persistence/base.rb', line 63

def value()
  @current_state && @current_state.name.to_s
end