stateful_enum 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3c76765e33135bb963a09c10f7b35c0f64c623a4
4
- data.tar.gz: 13c55c02af388232d71cfa9341f4d226b66c39fe
2
+ SHA256:
3
+ metadata.gz: fdcc7c193f8597cec775968ecf6cfae38dfe09299d024b877b5da5fae7e5e5ca
4
+ data.tar.gz: 20558b138fd9df10e915e40ce6ff197b436a3fe4ce4717f935207dae21d7254e
5
5
  SHA512:
6
- metadata.gz: c20bf58167c940d157fb73821d81a0e08daf080642b78f69181e7237a49408e1d819035d6618cae86b15acfb380f3aa3b37de12f10dc5cfd53e831ad0f63ac5c
7
- data.tar.gz: 86d6d5a1e299d3721319b4dcb51f43031feb1cc0b44f5ec95f91dcf1fe2461d011e54897f8fa90aebef0d090c168424d86c46ed3453df4ec5d5ae93ac99893fe
6
+ metadata.gz: 1f89d7a82fc8264fbd93b3bd85bb320607409fa662f7726f5b79f04b65da88ee3f080ff39a9ba8d5a2a14c128a1b52fdbf7c773ac6f9cbad8df61cff7052a9e8
7
+ data.tar.gz: b2bb43940cffd1be568fff1903920f569585fc3e56a257b9f9d824427ec42de5b2aeaf675faeab48404794ea8179d671dc2a661b98f379e5a7f3f679d6900121
data/.travis.yml CHANGED
@@ -5,17 +5,8 @@ sudo: false
5
5
  cache:
6
6
  - apt
7
7
  - bundler
8
-
9
- rvm:
10
- - 2.2.6
11
- - 2.3.3
12
- - 2.4.0
13
- - ruby-head
14
-
15
- gemfile:
16
- - gemfiles/Gemfile-rails.4.1.x
17
- - gemfiles/Gemfile-rails.4.2.x
18
- - gemfiles/Gemfile-rails.5.0.0
8
+ before_install:
9
+ - gem i bundler -v '<2'
19
10
 
20
11
  addons:
21
12
  apt:
@@ -23,14 +14,31 @@ addons:
23
14
  - graphviz
24
15
 
25
16
  matrix:
26
- exclude:
27
- - rvm: 2.4.0
28
- gemfile: gemfiles/Gemfile-rails.4.1.x
29
- - rvm: 2.4.0
17
+ include:
18
+ - rvm: 2.6.0
19
+ gemfile: gemfiles/Gemfile-rails.5.2.x
20
+ - rvm: 2.6.0
21
+ gemfile: gemfiles/Gemfile-rails.5.1.x
22
+ - rvm: 2.6.0
23
+ gemfile: gemfiles/Gemfile-rails.5.0.x
24
+
25
+ - rvm: 2.5.3
30
26
  gemfile: gemfiles/Gemfile-rails.4.2.x
31
- - rvm: ruby-head
27
+
28
+ - rvm: 2.3.8
32
29
  gemfile: gemfiles/Gemfile-rails.4.1.x
30
+
31
+ - rvm: 2.4.5
32
+ gemfile: gemfiles/Gemfile-rails.5.2.x
33
+ - rvm: 2.2.10
34
+ gemfile: gemfiles/Gemfile-rails.5.2.x
35
+
33
36
  - rvm: ruby-head
34
- gemfile: gemfiles/Gemfile-rails.4.2.x
37
+ gemfile: gemfiles/Gemfile-rails.5.2.x
38
+
39
+ - rvm: 2.6.0
40
+ gemfile: gemfiles/Gemfile-rails.edge
41
+
35
42
  allow_failures:
36
43
  - rvm: ruby-head
44
+ - gemfile: gemfiles/Gemfile-rails.edge
data/README.md CHANGED
@@ -105,6 +105,7 @@ There are a few important details to note regarding this feature:
105
105
  * The "from" states and the "to" states should both be given in Symbols.
106
106
  * The "from" state can be multiple states, in which case the key can be given as an Array of states, as shown in the usage example.
107
107
  * The "from" state can be `all` that means all defined states.
108
+ * The "from" state can be an exception of Array of states, in this case the key can be a subtraction of `all` with the state to be excluded, as shown in the usage example.
108
109
 
109
110
  ### :if and :unless Condition
110
111
 
@@ -144,7 +145,7 @@ You can specify relative or absolute output path via environment variable `DEST_
144
145
 
145
146
  ## Support Rails Versions
146
147
 
147
- * Rails 4.1.x, 4.2.x, 5.0, and 5.1 (edge)
148
+ * Rails 4.1.x, 4.2.x, 5.0, 5.1, 5.2, and 6.0 (edge)
148
149
 
149
150
 
150
151
  ## Contributing
@@ -1,8 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'stateful_enum', path: '..'
3
+ gemspec path: '..'
4
4
 
5
5
  gem 'rails', '~> 4.1.0'
6
- gem 'sqlite3'
7
- gem 'test-unit-rails'
8
- gem 'ruby-graphviz'
6
+ gem 'sqlite3', '~> 1.3.6'
@@ -1,8 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'stateful_enum', path: '..'
3
+ gemspec path: '..'
4
4
 
5
5
  gem 'rails', '~> 4.2.0'
6
- gem 'sqlite3'
7
- gem 'test-unit-rails'
8
- gem 'ruby-graphviz'
6
+ gem 'sqlite3', '~> 1.3.6'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '..'
4
+
5
+ gem 'rails', '~> 5.0.0'
6
+ gem 'sqlite3', '~> 1.3.6'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '..'
4
+
5
+ gem 'rails', '~> 5.1.0'
6
+ gem 'sqlite3', '~> 1.3.6'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '..'
4
+
5
+ gem 'rails', '~> 5.2.0'
6
+ gem 'sqlite3', '~> 1.3.6'
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) do |repo_name|
4
+ repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
5
+ "https://github.com/#{repo_name}.git"
6
+ end
7
+
8
+ gemspec path: '..'
9
+
10
+ github 'rails/rails' do
11
+ gem 'rails'
12
+ end
13
+ gem 'sqlite3'
14
+ gem 'selenium-webdriver'
@@ -7,7 +7,7 @@ module StatefulEnum
7
7
  class GraphGenerator < ::Rails::Generators::NamedBase
8
8
  desc 'Draws a state machine diagram as a PNG file'
9
9
  def draw
10
- require 'graphviz'
10
+ require 'ruby-graphviz'
11
11
  StatefulEnum::Machine.prepend StatefulEnum::Graph
12
12
  class_name.constantize
13
13
  end
@@ -50,6 +50,7 @@ module StatefulEnum
50
50
  class EventDrawer < ::StatefulEnum::Machine::Event
51
51
  def initialize(g, states, prefix, suffix, name, &block)
52
52
  @g, @states, @prefix, @suffix, @name = g, states, prefix, suffix, name
53
+ @before, @after = [], []
53
54
 
54
55
  instance_eval(&block) if block
55
56
  end
@@ -46,7 +46,7 @@ module StatefulEnum
46
46
 
47
47
  class EventStore < ::StatefulEnum::Machine::Event
48
48
  def initialize(items, states, prefix, suffix, name, &block)
49
- @items, @states, @prefix, @suffix, @name = items, states, prefix, suffix, name
49
+ @items, @states, @prefix, @suffix, @name, @before, @after = items, states, prefix, suffix, name, [], []
50
50
 
51
51
  instance_eval(&block) if block
52
52
  end
@@ -4,16 +4,12 @@ module StatefulEnum
4
4
  class Machine
5
5
  def initialize(model, column, states, prefix, suffix, &block)
6
6
  @model, @column, @states, @event_names = model, column, states, []
7
- @prefix = if prefix == true
8
- "#{column}_"
9
- elsif prefix
10
- "#{prefix}_"
11
- end
12
- @suffix = if suffix == true
13
- "_#{column}"
14
- elsif suffix
15
- "_#{suffix}"
16
- end
7
+ @prefix = if prefix
8
+ prefix == true ? "#{column}_" : "#{prefix}_"
9
+ end
10
+ @suffix = if suffix
11
+ suffix == true ? "_#{column}" : "_#{suffix}"
12
+ end
17
13
 
18
14
  # undef non-verb methods e.g. Model#active!
19
15
  states.each do |state|
@@ -31,7 +27,7 @@ module StatefulEnum
31
27
 
32
28
  class Event
33
29
  def initialize(model, column, states, prefix, suffix, name, &block)
34
- @states, @name, @transitions, @before, @after = states, name, {}, nil, nil
30
+ @states, @name, @transitions, @before, @after = states, name, {}, [], []
35
31
 
36
32
  instance_eval(&block) if block
37
33
 
@@ -42,16 +38,25 @@ module StatefulEnum
42
38
  model.class_eval do
43
39
  # def assign()
44
40
  detect_enum_conflict! column, new_method_name
41
+
42
+ # defining callbacks
43
+ define_callbacks new_method_name
44
+ before.each do |before_callback|
45
+ model.set_callback new_method_name, :before, before_callback
46
+ end
47
+ after.each do |after_callback|
48
+ model.set_callback new_method_name, :after, after_callback
49
+ end
50
+
45
51
  define_method new_method_name do
46
52
  to, condition = transitions[send(column).to_sym]
47
53
  #TODO better error
48
54
  if to && (!condition || instance_exec(&condition))
49
55
  #TODO transaction?
50
- instance_eval(&before) if before
51
- original_method = self.class.send(:_enum_methods_module).instance_method "#{prefix}#{to}#{suffix}!"
52
- ret = original_method.bind(self).call
53
- instance_eval(&after) if after
54
- ret
56
+ run_callbacks new_method_name do
57
+ original_method = self.class.send(:_enum_methods_module).instance_method "#{prefix}#{to}#{suffix}!"
58
+ original_method.bind(self).call
59
+ end
55
60
  else
56
61
  false
57
62
  end
@@ -66,7 +71,10 @@ module StatefulEnum
66
71
  # def can_assign?()
67
72
  detect_enum_conflict! column, "can_#{new_method_name}?"
68
73
  define_method "can_#{new_method_name}?" do
69
- transitions.key? send(column).to_sym
74
+ state = send(column).to_sym
75
+ return false unless transitions.key? state
76
+ _to, condition = transitions[state]
77
+ !condition || instance_exec(&condition)
70
78
  end
71
79
 
72
80
  # def assign_transition()
@@ -100,11 +108,11 @@ module StatefulEnum
100
108
  end
101
109
 
102
110
  def before(&block)
103
- @before = block
111
+ @before << block
104
112
  end
105
113
 
106
114
  def after(&block)
107
- @after = block
115
+ @after << block
108
116
  end
109
117
  end
110
118
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StatefulEnum
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stateful_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-28 00:00:00.000000000 Z
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -111,7 +111,10 @@ files:
111
111
  - bin/setup
112
112
  - gemfiles/Gemfile-rails.4.1.x
113
113
  - gemfiles/Gemfile-rails.4.2.x
114
- - gemfiles/Gemfile-rails.5.0.0
114
+ - gemfiles/Gemfile-rails.5.0.x
115
+ - gemfiles/Gemfile-rails.5.1.x
116
+ - gemfiles/Gemfile-rails.5.2.x
117
+ - gemfiles/Gemfile-rails.edge
115
118
  - lib/generators/stateful_enum/graph_generator.rb
116
119
  - lib/generators/stateful_enum/plantuml_generator.rb
117
120
  - lib/stateful_enum.rb
@@ -140,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
143
  version: '0'
141
144
  requirements: []
142
145
  rubyforge_project:
143
- rubygems_version: 2.6.8
146
+ rubygems_version: 2.7.8
144
147
  signing_key:
145
148
  specification_version: 4
146
149
  summary: A state machine plugin on top of ActiveRecord::Enum
@@ -1,8 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'stateful_enum', path: '..'
4
-
5
- gem 'rails', '~> 5.0.0'
6
- gem 'sqlite3'
7
- gem 'test-unit-rails'
8
- gem 'ruby-graphviz'