simple-state-machine 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7599d0fb8142894715d6aa0806ce317727ced8b2
4
+ data.tar.gz: 3c7a3f06c8a07949b90378c5029fb9ca3590c925
5
+ SHA512:
6
+ metadata.gz: 875a70db99c4fd93975fca11de9aecfa3b87a20a8bbb35d4cdfd9763b520e5d4445509f599bdca010d354c92fb53edb76e64bd35bc8413103f7a0725e65fcbe3
7
+ data.tar.gz: 42295dffe4389b48dfa62816d9b955a7c772fb48d35a320407a5f0344be50f950dce66103b23aaca2566f162df4cfde47da7a7719c848cec41f37078b71fdb14
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/lib/ssm.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "ssm/errors"
2
+ require "ssm/event"
3
+ require "ssm/version"
4
+
5
+ require "ssm/ssm"
data/lib/ssm/errors.rb ADDED
@@ -0,0 +1,3 @@
1
+ module SSM
2
+ class InvalidTransition < StandardError; end
3
+ end
data/lib/ssm/event.rb ADDED
@@ -0,0 +1,34 @@
1
+ module SSM
2
+ module Event
3
+ def self.included(base)
4
+ base.extend SSM::Event::ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def guards(*guards)
9
+ @guards = guards
10
+ end
11
+
12
+ def guard(resource, &block)
13
+ success = @guards&.reduce(true) do |memo, guard|
14
+ resource.send(guard) && memo
15
+ end
16
+ return(yield) if success || @guards.blank?
17
+ resource.reload
18
+ false
19
+ end
20
+
21
+ def transition!(resource, to:, &block)
22
+ @from = resource.state
23
+ ActiveRecord::Base.transaction do
24
+ resource.state = to
25
+ resource.save!
26
+ yield if block_given?
27
+ end
28
+ rescue ActiveRecord::RecordInvalid
29
+ resource.state = @from
30
+ raise SSM::InvalidTransition
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/ssm/ssm.rb ADDED
@@ -0,0 +1,46 @@
1
+ module SSM
2
+ def self.included(base)
3
+ base.extend SSM::ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def states(*_states)
8
+ _states.each do |state|
9
+ define_state_boolean(state)
10
+ define_state_scope(state)
11
+ end
12
+ end
13
+
14
+ def state_validation(*states, &block)
15
+ states.each do |state|
16
+ is_state = (state.to_s + "?").to_sym
17
+ with_options if: is_state do |resource|
18
+ yield resource
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+ def define_state_boolean(_state)
25
+ method_name = "#{ _state }?"
26
+ define_method method_name do
27
+ self.state == _state.to_s
28
+ end
29
+ end
30
+
31
+ def define_state_scope(_state)
32
+ self.send :scope, _state, -> do
33
+ where(state: _state)
34
+ end
35
+ end
36
+ end
37
+
38
+ def transition(from:)
39
+ valid_states = [from].flatten
40
+
41
+ if valid_states.include?(self.state.to_sym)
42
+ return yield
43
+ end
44
+ raise SSM::InvalidTransition
45
+ end
46
+ end
@@ -0,0 +1,2 @@
1
+ module SSM
2
+ end
@@ -0,0 +1,3 @@
1
+ module SSM
2
+ VERSION = "0.0.1"
3
+ end
data/ssm.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "ssm/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "simple-state-machine"
6
+ s.version = SSM::VERSION
7
+ s.authors = ["Michael Choi", "Si Le"]
8
+ s.email = "mchoi@jiffyondemand.com"
9
+ s.homepage = %q{https://github.com/jiffyondemand/ssm}
10
+
11
+ s.summary = %q{Simple State Machine for Ruby/Ruby on Rails}
12
+ s.description = %q{SSM is a simpler, more manually configurable implementation of a state machine}
13
+
14
+ s.date = Time.now
15
+ s.licenses = ["MIT"]
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-state-machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Choi
8
+ - Si Le
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-09-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: SSM is a simpler, more manually configurable implementation of a state
15
+ machine
16
+ email: mchoi@jiffyondemand.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - lib/ssm.rb
23
+ - lib/ssm/errors.rb
24
+ - lib/ssm/event.rb
25
+ - lib/ssm/ssm.rb
26
+ - lib/ssm/validation.rb
27
+ - lib/ssm/version.rb
28
+ - ssm.gemspec
29
+ homepage: https://github.com/jiffyondemand/ssm
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.5.1
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Simple State Machine for Ruby/Ruby on Rails
53
+ test_files: []