nxt_state_machine 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +7 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +67 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +348 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/lib/nxt_state_machine/callable.rb +63 -0
  14. data/lib/nxt_state_machine/callback_registry.rb +35 -0
  15. data/lib/nxt_state_machine/error_callback_registry.rb +38 -0
  16. data/lib/nxt_state_machine/errors/error.rb +1 -0
  17. data/lib/nxt_state_machine/errors/event_already_registered.rb +5 -0
  18. data/lib/nxt_state_machine/errors/event_without_transitions.rb +5 -0
  19. data/lib/nxt_state_machine/errors/initial_state_already_defined.rb +7 -0
  20. data/lib/nxt_state_machine/errors/invalid_callback_option.rb +5 -0
  21. data/lib/nxt_state_machine/errors/missing_configuration.rb +5 -0
  22. data/lib/nxt_state_machine/errors/state_already_registered.rb +5 -0
  23. data/lib/nxt_state_machine/errors/transition_already_registered.rb +5 -0
  24. data/lib/nxt_state_machine/errors/transition_halted.rb +12 -0
  25. data/lib/nxt_state_machine/errors/transition_not_defined.rb +5 -0
  26. data/lib/nxt_state_machine/errors/unknown_state_error.rb +5 -0
  27. data/lib/nxt_state_machine/event.rb +49 -0
  28. data/lib/nxt_state_machine/event_registry.rb +11 -0
  29. data/lib/nxt_state_machine/integrations/active_record.rb +77 -0
  30. data/lib/nxt_state_machine/integrations/attr_accessor.rb +69 -0
  31. data/lib/nxt_state_machine/integrations/hash.rb +67 -0
  32. data/lib/nxt_state_machine/state.rb +17 -0
  33. data/lib/nxt_state_machine/state_machine.rb +179 -0
  34. data/lib/nxt_state_machine/state_registry.rb +12 -0
  35. data/lib/nxt_state_machine/transition/around_callback_chain.rb +26 -0
  36. data/lib/nxt_state_machine/transition/proxy.rb +31 -0
  37. data/lib/nxt_state_machine/transition/store.rb +19 -0
  38. data/lib/nxt_state_machine/transition.rb +87 -0
  39. data/lib/nxt_state_machine/version.rb +3 -0
  40. data/lib/nxt_state_machine.rb +96 -0
  41. data/nxt_state_machine.gemspec +46 -0
  42. metadata +202 -0
@@ -0,0 +1,3 @@
1
+ module NxtStateMachine
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,96 @@
1
+ require 'active_support/all'
2
+ require 'nxt_registry'
3
+ require "nxt_state_machine/version"
4
+ require "nxt_state_machine/errors/error"
5
+ require "nxt_state_machine/errors/event_already_registered"
6
+ require "nxt_state_machine/errors/event_without_transitions"
7
+ require "nxt_state_machine/errors/initial_state_already_defined"
8
+ require "nxt_state_machine/errors/invalid_callback_option"
9
+ require "nxt_state_machine/errors/missing_configuration"
10
+ require "nxt_state_machine/errors/state_already_registered"
11
+ require "nxt_state_machine/errors/transition_already_registered"
12
+ require "nxt_state_machine/errors/transition_not_defined"
13
+ require "nxt_state_machine/errors/unknown_state_error"
14
+ require "nxt_state_machine/errors/transition_halted"
15
+ require "nxt_state_machine/callable"
16
+ require "nxt_state_machine/state_registry"
17
+ require "nxt_state_machine/callback_registry"
18
+ require "nxt_state_machine/error_callback_registry"
19
+ require "nxt_state_machine/event_registry"
20
+ require "nxt_state_machine/state"
21
+ require "nxt_state_machine/event"
22
+ require "nxt_state_machine/transition"
23
+ require "nxt_state_machine/transition/proxy"
24
+ require "nxt_state_machine/transition/store"
25
+ require "nxt_state_machine/transition/around_callback_chain"
26
+ require "nxt_state_machine/state_machine"
27
+ require "nxt_state_machine/integrations/active_record"
28
+ require "nxt_state_machine/integrations/attr_accessor"
29
+ require "nxt_state_machine/integrations/hash"
30
+
31
+ module NxtStateMachine
32
+ module ClassMethods
33
+ include NxtRegistry
34
+
35
+ def state_machine(name = :default, **opts, &config)
36
+ state_machines.resolve!(name) || state_machines.register(
37
+ name,
38
+ StateMachine.new(name, self, state_machine_event_registry, **opts).configure(&config)
39
+ )
40
+ end
41
+
42
+ def state_machines
43
+ @state_machines ||= registry :state_machines
44
+ end
45
+
46
+ def new(*args, **opts, &block)
47
+ instance = if opts.any?
48
+ super(*args, **opts, &block)
49
+ else
50
+ super(*args, &block)
51
+ end
52
+
53
+ # set each initial state for all machines
54
+ state_machines.each do |name, machine|
55
+ instance.current_state_name(name) if machine.initial_state.present?
56
+ end
57
+
58
+ instance
59
+ end
60
+
61
+ private
62
+
63
+ def state_machine_event_registry
64
+ @state_machine_event_registry ||= EventRegistry.new
65
+ end
66
+ end
67
+
68
+ module InstanceMethods
69
+ def state_machines
70
+ @state_machines ||= self.class.state_machines
71
+ end
72
+
73
+ def state_machine(name = :default)
74
+ @state_machine ||= self.class.state_machines[name]
75
+ end
76
+
77
+ def current_state_name(name = :default)
78
+ state_machines[name].current_state_name(self)
79
+ end
80
+
81
+ def current_state(name = :default)
82
+ state_machines[name].states.fetch(current_state_name(name))
83
+ end
84
+
85
+ def halt_transition(*args, **opts)
86
+ raise NxtStateMachine::Errors::TransitionHalted.new(*args, **opts)
87
+ end
88
+
89
+ delegate :initial_state, :states, to: :state_machine
90
+ end
91
+
92
+ def self.included(base)
93
+ base.extend(ClassMethods)
94
+ base.include(InstanceMethods)
95
+ end
96
+ end
@@ -0,0 +1,46 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "nxt_state_machine/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "nxt_state_machine"
7
+ spec.version = NxtStateMachine::VERSION
8
+ spec.authors = ["Andreas Robecke", "Nils Sommer", "Raphael Kallensee", "Lütfi Demirci"]
9
+ spec.email = ["a.robecke@getsafe.de"]
10
+
11
+ spec.summary = %q{A rich but straight forward state machine library}
12
+ spec.description = %q{A state machine library that can be used with ActiveRecord or in plain ruby and should be easy to customize for other integrations}
13
+ spec.homepage = "https://github.com/nxt-insurance"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/nxt-insurance/nxt_state_machine"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_dependency "activesupport"
38
+ spec.add_dependency "nxt_registry", "~> 0.1.3"
39
+
40
+ spec.add_development_dependency "bundler", "~> 2.0"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ spec.add_development_dependency "pry"
44
+ spec.add_development_dependency "activerecord"
45
+ spec.add_development_dependency "sqlite3"
46
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nxt_state_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andreas Robecke
8
+ - Nils Sommer
9
+ - Raphael Kallensee
10
+ - Lütfi Demirci
11
+ autorequire:
12
+ bindir: exe
13
+ cert_chain: []
14
+ date: 2020-01-03 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activesupport
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: nxt_registry
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: 0.1.3
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: 0.1.3
44
+ - !ruby/object:Gem::Dependency
45
+ name: bundler
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '2.0'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '2.0'
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '10.0'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '10.0'
72
+ - !ruby/object:Gem::Dependency
73
+ name: rspec
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - "~>"
77
+ - !ruby/object:Gem::Version
78
+ version: '3.0'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '3.0'
86
+ - !ruby/object:Gem::Dependency
87
+ name: pry
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ - !ruby/object:Gem::Dependency
101
+ name: activerecord
102
+ requirement: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ type: :development
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ - !ruby/object:Gem::Dependency
115
+ name: sqlite3
116
+ requirement: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ description: A state machine library that can be used with ActiveRecord or in plain
129
+ ruby and should be easy to customize for other integrations
130
+ email:
131
+ - a.robecke@getsafe.de
132
+ executables: []
133
+ extensions: []
134
+ extra_rdoc_files: []
135
+ files:
136
+ - ".gitignore"
137
+ - ".rspec"
138
+ - ".ruby-version"
139
+ - ".travis.yml"
140
+ - Gemfile
141
+ - Gemfile.lock
142
+ - LICENSE.txt
143
+ - README.md
144
+ - Rakefile
145
+ - bin/console
146
+ - bin/setup
147
+ - lib/nxt_state_machine.rb
148
+ - lib/nxt_state_machine/callable.rb
149
+ - lib/nxt_state_machine/callback_registry.rb
150
+ - lib/nxt_state_machine/error_callback_registry.rb
151
+ - lib/nxt_state_machine/errors/error.rb
152
+ - lib/nxt_state_machine/errors/event_already_registered.rb
153
+ - lib/nxt_state_machine/errors/event_without_transitions.rb
154
+ - lib/nxt_state_machine/errors/initial_state_already_defined.rb
155
+ - lib/nxt_state_machine/errors/invalid_callback_option.rb
156
+ - lib/nxt_state_machine/errors/missing_configuration.rb
157
+ - lib/nxt_state_machine/errors/state_already_registered.rb
158
+ - lib/nxt_state_machine/errors/transition_already_registered.rb
159
+ - lib/nxt_state_machine/errors/transition_halted.rb
160
+ - lib/nxt_state_machine/errors/transition_not_defined.rb
161
+ - lib/nxt_state_machine/errors/unknown_state_error.rb
162
+ - lib/nxt_state_machine/event.rb
163
+ - lib/nxt_state_machine/event_registry.rb
164
+ - lib/nxt_state_machine/integrations/active_record.rb
165
+ - lib/nxt_state_machine/integrations/attr_accessor.rb
166
+ - lib/nxt_state_machine/integrations/hash.rb
167
+ - lib/nxt_state_machine/state.rb
168
+ - lib/nxt_state_machine/state_machine.rb
169
+ - lib/nxt_state_machine/state_registry.rb
170
+ - lib/nxt_state_machine/transition.rb
171
+ - lib/nxt_state_machine/transition/around_callback_chain.rb
172
+ - lib/nxt_state_machine/transition/proxy.rb
173
+ - lib/nxt_state_machine/transition/store.rb
174
+ - lib/nxt_state_machine/version.rb
175
+ - nxt_state_machine.gemspec
176
+ homepage: https://github.com/nxt-insurance
177
+ licenses:
178
+ - MIT
179
+ metadata:
180
+ allowed_push_host: https://rubygems.org
181
+ homepage_uri: https://github.com/nxt-insurance
182
+ source_code_uri: https://github.com/nxt-insurance/nxt_state_machine
183
+ post_install_message:
184
+ rdoc_options: []
185
+ require_paths:
186
+ - lib
187
+ required_ruby_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ required_rubygems_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ requirements: []
198
+ rubygems_version: 3.0.6
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: A rich but straight forward state machine library
202
+ test_files: []