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

- (Base) initialize(binding, field_name)

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

- (Object) binding (readonly)

Returns the value of attribute binding



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

def binding
  @binding
end

- (Object) current_state

Returns the value of attribute current_state



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

def current_state
  @current_state
end

- (Object) field_name (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

+ (Object) prepare_field(klass, field_name)

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

- (Object) find_current_state



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

- (Object) klass



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

def klass
  binding.target
end

- (Object) machine



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

def machine
  binding.machine
end

- (Object) object



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

def object
  binding.object
end

- (Object) persist!



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

def persist!
  write_attribute( value() )
end

- (Object) reload



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

def reload
  @current_state = find_current_state()
end

- (Object) value



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

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