state_machine_ext 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +61 -0
- data/lib/group.rb +36 -0
- data/lib/state_machine_ext.rb +89 -0
- data/nbproject/private/private.properties +1 -0
- data/nbproject/private/private.xml +4 -0
- data/nbproject/project.properties +4 -0
- data/nbproject/project.xml +13 -0
- metadata +92 -0
data/README.rdoc
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
= state_machine extensions
|
2
|
+
state_machine_ext is an extension of the state_machine[https://github.com/pluginaweek/state_machine] gem.
|
3
|
+
|
4
|
+
== Install
|
5
|
+
gem install state_machine_ext
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
This gem adds state groups functionality and method to find all possible transitions from a state.
|
9
|
+
Below is an example of the features offered:
|
10
|
+
|
11
|
+
=== Class definition
|
12
|
+
|
13
|
+
class Order
|
14
|
+
state_machine :initial => :not_selected do
|
15
|
+
event :choose do
|
16
|
+
transition :not_selected => :selected
|
17
|
+
end
|
18
|
+
event :add_to_basket do
|
19
|
+
transition :selected => :in_basket
|
20
|
+
end
|
21
|
+
event :pay do
|
22
|
+
transition :in_basket => :paid
|
23
|
+
end
|
24
|
+
event :to_send do
|
25
|
+
transition :paid => :sent
|
26
|
+
end
|
27
|
+
|
28
|
+
#initialize groups of the states
|
29
|
+
group :not_paid do
|
30
|
+
state :not_selected
|
31
|
+
state :selected
|
32
|
+
state :in_basket
|
33
|
+
end
|
34
|
+
group :in_progress do
|
35
|
+
state :paid, :sent
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
=== Using extensions
|
41
|
+
|
42
|
+
order = Order.new
|
43
|
+
# returns the array of all the states which we can reach from the current one
|
44
|
+
order.state_all_transitions #=> [:sent, :paid, :in_basket, :selected]
|
45
|
+
# same for the particular state
|
46
|
+
order.state_all_transitions(:in_basket) #=> [:sent, :paid]
|
47
|
+
# check whether a group includes some state
|
48
|
+
order.group(:not_paid).include?(:selected) #=> true
|
49
|
+
# find groups to which belongs a state
|
50
|
+
order.find_group(:paid) #=> [:in_progress]
|
51
|
+
|
52
|
+
|
53
|
+
== Credits
|
54
|
+
|
55
|
+
=== Project Team
|
56
|
+
* Mykhaylo Sorochan - Project Manager
|
57
|
+
* Dmitriy Landberg - Software Developer
|
58
|
+
* Nataliya Shatokhina - Tester
|
59
|
+
* Sergey Mostovoy - Extension idea
|
60
|
+
|
61
|
+
Copyright (c) 2010 {Sphere Consulting Inc.}[http://www.sphereinc.com], released under the MIT license (see LICENSE).
|
data/lib/group.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module StateMachineExt
|
2
|
+
|
3
|
+
class InvalidState < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
class Group
|
7
|
+
|
8
|
+
attr_accessor :name
|
9
|
+
|
10
|
+
def initialize(name, parent)
|
11
|
+
@machine = parent
|
12
|
+
@name = name
|
13
|
+
@group_states = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def state(*args)
|
17
|
+
args.each do |arg|
|
18
|
+
is_include = false
|
19
|
+
@machine.states.each do |state|
|
20
|
+
if state.name == arg
|
21
|
+
@group_states << state
|
22
|
+
is_include = true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
raise InvalidState, "\"#{arg}\" is an unknown state machine state" unless is_include
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def include?(state)
|
30
|
+
group_state = @group_states.detect {|item| item.name == state}
|
31
|
+
group_state.nil? ? false : true
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
$:.unshift(File.expand_path(File.dirname(__FILE__) + "/../"))
|
2
|
+
require 'rubygems'
|
3
|
+
require 'state_machine'
|
4
|
+
require 'lib/group'
|
5
|
+
|
6
|
+
module StateMachineExt
|
7
|
+
|
8
|
+
class InvalidGroup < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.included(base)
|
12
|
+
base.class_eval do
|
13
|
+
|
14
|
+
def group(name, &block)
|
15
|
+
@groups = [] if @groups.nil?
|
16
|
+
group = @groups.detect {|item| item.name == name}
|
17
|
+
@groups << group = Group.new(name,self) if group.nil?
|
18
|
+
group.instance_eval(&block) if block
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_accessor :groups
|
22
|
+
alias_method :define_event_helpers_original, :define_event_helpers
|
23
|
+
|
24
|
+
def define_event_helpers
|
25
|
+
define_event_helpers_original
|
26
|
+
|
27
|
+
# Define the method that returns the group with the given name
|
28
|
+
define_instance_method(:group) do |machine, object, request_group|
|
29
|
+
group = nil
|
30
|
+
group = @groups.detect {|item| item.name == request_group}
|
31
|
+
if group.nil?
|
32
|
+
raise InvalidGroup, "\"#{request_group}\" is an unknown state machine group"
|
33
|
+
end
|
34
|
+
|
35
|
+
group
|
36
|
+
end
|
37
|
+
|
38
|
+
# Define the method that returns the state machine's group for the given
|
39
|
+
# state
|
40
|
+
define_instance_method(:find_group) do |machine, object, request_state|
|
41
|
+
@groups.inject([]) do |res,group|
|
42
|
+
res << group.name if group.include?(request_state.to_sym)
|
43
|
+
res
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
#Define the method that returns all states which can be reached
|
48
|
+
#from the given one
|
49
|
+
define_instance_method(attribute(:all_transitions)) do |machine, object, *args|
|
50
|
+
next_transitions = []
|
51
|
+
request_state = []
|
52
|
+
|
53
|
+
transitions = return_transition(next_transitions, request_state, machine, object, *args)
|
54
|
+
|
55
|
+
transitions.inject([]) do |res,trans|
|
56
|
+
res << trans.to_name unless trans.is_a?(Array)
|
57
|
+
res.uniq
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def return_transition(next_transitions, request_state, machine, object, *args)
|
63
|
+
state = args.empty? ? object.state_name : args.first
|
64
|
+
request_state << state
|
65
|
+
|
66
|
+
*args = {:from => args.first} unless args.empty?
|
67
|
+
transitions = machine.events.transitions_for(object, *args)
|
68
|
+
|
69
|
+
transitions.each do |transition|
|
70
|
+
unless request_state.include?(transition.to_name)
|
71
|
+
next_transition =
|
72
|
+
return_transition(next_transitions, request_state,
|
73
|
+
machine, object, transition.to_name)
|
74
|
+
next_transitions << next_transition unless next_transition.empty?
|
75
|
+
end
|
76
|
+
next_transitions << transition
|
77
|
+
end
|
78
|
+
|
79
|
+
next_transitions
|
80
|
+
end
|
81
|
+
private :return_transition
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
class StateMachine::Machine
|
88
|
+
include StateMachineExt
|
89
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
file.reference.ExtendedStateMachine-lib=/mnt/hgfs/server/ExtendedStateMachine/lib
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project xmlns="http://www.netbeans.org/ns/project/1">
|
3
|
+
<type>org.netbeans.modules.ruby.rubyproject</type>
|
4
|
+
<configuration>
|
5
|
+
<data xmlns="http://www.netbeans.org/ns/ruby-project/1">
|
6
|
+
<name>ExStateMachine</name>
|
7
|
+
<source-roots>
|
8
|
+
<root id="src.dir"/>
|
9
|
+
</source-roots>
|
10
|
+
<test-roots/>
|
11
|
+
</data>
|
12
|
+
</configuration>
|
13
|
+
</project>
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: state_machine_ext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sphere Consulting Inc.
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-08 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: state_machine
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 51
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 9
|
33
|
+
- 4
|
34
|
+
version: 0.9.4
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: " Extensions for the state_machine gem\n"
|
38
|
+
email:
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/group.rb
|
47
|
+
- lib/state_machine_ext.rb
|
48
|
+
- nbproject/private/private.properties
|
49
|
+
- nbproject/private/private.xml
|
50
|
+
- nbproject/project.properties
|
51
|
+
- nbproject/project.xml
|
52
|
+
- README.rdoc
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: https://github.com/SphereConsultingInc/state_machine_ext/
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 57
|
68
|
+
segments:
|
69
|
+
- 1
|
70
|
+
- 8
|
71
|
+
- 7
|
72
|
+
version: 1.8.7
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 23
|
79
|
+
segments:
|
80
|
+
- 1
|
81
|
+
- 3
|
82
|
+
- 6
|
83
|
+
version: 1.3.6
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.3.7
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Extensions for the state_machine gem
|
91
|
+
test_files: []
|
92
|
+
|