openhood-simple_state_machine 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/simple_state_machine.rb +63 -0
  2. metadata +75 -0
@@ -0,0 +1,63 @@
1
+ module SimpleStateMachine
2
+
3
+ def self.included(base)
4
+ base.extend Macro
5
+ end
6
+
7
+ module Macro
8
+ def state_machine(column, states)
9
+ create_empty_state_machine unless inheritable_attributes.key? :states
10
+ inheritable_attributes[:states][column.to_sym] = states
11
+ validates_inclusion_of column, :in => states
12
+ # should also override getter/setter to convert to strings
13
+ self.class_eval <<-eos
14
+ def #{column.to_s}=(value)
15
+ self[:#{column.to_s}] = value.to_s
16
+ end
17
+ def #{column.to_s}
18
+ self[:#{column.to_s}].to_sym
19
+ end
20
+ def #{column.to_s}_revert
21
+ self[:#{column.to_s}] = self.new_record? ? states[:#{column.to_s}].first.to_s : self.#{column.to_s}_was
22
+ end
23
+ eos
24
+
25
+ # define a method {state_column}_{state}? for each state
26
+ states.each do |state|
27
+ self.class_eval <<-eos
28
+ def #{column.to_s}_#{state.to_s}?
29
+ self[:#{column.to_s}] === "#{state.to_s}"
30
+ end
31
+ eos
32
+ end
33
+
34
+ end
35
+
36
+ private
37
+
38
+ def create_empty_state_machine
39
+ write_inheritable_attribute :states, {} # add a class variable
40
+ class_inheritable_reader :states # make it read-only
41
+
42
+ # set initial states on new objects
43
+ if(!instance_methods.include?("after_initialize") && ActiveRecord::VERSION::MAJOR < 3)
44
+ self.class_eval do
45
+ def after_initialize # ActiveRecord::Base requires explicit definition of this function to use the callback
46
+ end
47
+ end
48
+ end
49
+ after_initialize :set_initial_states
50
+ self.class_eval do
51
+ def set_initial_states
52
+ states.each {|column, states|
53
+ self[column] = states.first.to_s
54
+ } if(@new_record)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ ActiveRecord::Base.class_eval do
62
+ include SimpleStateMachine
63
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openhood-simple_state_machine
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Joseph Halter
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-24 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 2
30
+ - 2
31
+ version: 2.2.2
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Same as acts_as_state_machine but on multiple columns and with more strict validation, allow creation of complex events with parameters, used successfully on critical financial applications for quite a long time
35
+ email: team@openhood.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/simple_state_machine.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/openhood/simple_state_machine
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.6
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Same as acts_as_state_machine but on multiple columns and with more strict validation, allow creation of complex events with parameters, used successfully on critical financial applications for quite a long time
74
+ test_files: []
75
+