solidstate 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/solidstate.rb +21 -9
- data/solidstate.gemspec +2 -1
- data/spec/solidstate/solidstate_spec.rb +12 -0
- data/spec/spec_helper.rb +18 -0
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 02c75674c24aca47405018404c16421089516578ac830b15e41ecdb419fb6a1f
|
4
|
+
data.tar.gz: 5cf34c64280885cc441dfc61aa2c856d8764d89b99b59aaa969e870a3ce7e84d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc2da012e5e8c15ed0b7301f8b85a319fe9b5313110916cb892e68ac0924d35c68cde203c1b475d90717fa89cb5240a3c1c9e6d8a62a60cbbd937fbf1d761d1a
|
7
|
+
data.tar.gz: 0614d3eafab0d4d96534d9cffd586d0f5ef2a14bd4fee71b11567838e84b98f635950512a529373bdfc7c71e63ee477eaedd8264a2a8f5112f8e62d8259bb80a
|
data/lib/solidstate.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
3
|
+
# require 'active_support/core_ext/class/attribute_accessors'
|
2
4
|
|
3
5
|
module SolidState
|
4
6
|
extend ActiveSupport::Concern
|
5
|
-
|
6
7
|
class InvalidTransitionError < ArgumentError; end
|
7
8
|
|
8
9
|
STATE_ATTRIBUTE = :state.freeze
|
9
10
|
|
10
11
|
module ClassMethods
|
11
|
-
attr_accessor :possible_states
|
12
|
-
attr_accessor :state_transitions
|
13
|
-
|
14
12
|
def states(*list, &block)
|
15
13
|
list = list.collect(&:to_s)
|
16
14
|
|
17
|
-
raise "states have already been set! To get list of possible states, call #{name}.possible_states" if self.possible_states
|
15
|
+
raise "states for #{self.name} class have already been set! To get list of possible states, call #{name}.possible_states" if self.respond_to?(:possible_states)
|
18
16
|
raise "This is not a list of names" unless list.first.respond_to?(:downcase)
|
19
17
|
|
18
|
+
class_attribute :possible_states
|
19
|
+
class_attribute :state_transitions
|
20
|
+
|
20
21
|
self.possible_states = list
|
21
22
|
self.state_transitions = {}
|
22
23
|
|
@@ -49,7 +50,6 @@ module SolidState
|
|
49
50
|
|
50
51
|
# puts "From #{from} to #{to.join(' or ')}"
|
51
52
|
to.each do |dest|
|
52
|
-
|
53
53
|
self.state_transitions[from.to_sym] ||= []
|
54
54
|
self.state_transitions[from.to_sym].push(dest.to_sym)
|
55
55
|
|
@@ -70,11 +70,23 @@ module SolidState
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
-
def set_state(new_state)
|
74
|
-
|
73
|
+
def set_state!(new_state)
|
74
|
+
raise InvalidTransitionError.new("Cannot transition from #{state} to #{dest}") unless can_transition_to?(new_state)
|
75
75
|
self.state = new_state
|
76
76
|
end
|
77
77
|
|
78
|
+
def set_state(new_state)
|
79
|
+
set_state!(new_state) rescue false
|
80
|
+
end
|
81
|
+
|
82
|
+
def update_state(new_state)
|
83
|
+
save if set_state(new_state)
|
84
|
+
end if respond_to?(:_validators)
|
85
|
+
|
86
|
+
def update_state!(new_state)
|
87
|
+
save! if set_state!(new_state)
|
88
|
+
end if respond_to?(:_validators)
|
89
|
+
|
78
90
|
private
|
79
91
|
|
80
92
|
def ensure_valid_transition
|
@@ -90,4 +102,4 @@ module SolidState
|
|
90
102
|
possible.include?(new_state.to_sym)
|
91
103
|
end
|
92
104
|
|
93
|
-
end
|
105
|
+
end
|
data/solidstate.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "solidstate"
|
5
|
-
s.version = '0.4.
|
5
|
+
s.version = '0.4.3'
|
6
6
|
s.platform = Gem::Platform::RUBY
|
7
7
|
s.authors = ['Tomás Pollak']
|
8
8
|
s.email = ['tomas@forkhq.com']
|
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.add_development_dependency "activerecord" #, ">= 4.0.0"
|
17
17
|
s.add_development_dependency "sqlite3" #, ">= 4.0.0"
|
18
18
|
s.add_development_dependency "mongo_mapper" #, ">= 4.0.0"
|
19
|
+
s.add_development_dependency "fuubar" #, ">= 4.0.0"
|
19
20
|
|
20
21
|
s.files = `git ls-files`.split("\n")
|
21
22
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe 'Solidstate' do
|
4
|
+
|
5
|
+
describe 'inheritance' do
|
6
|
+
it 'works' do
|
7
|
+
expect(Subscriber.possible_states).to eq(['inactive', 'active', 'unsubscribed', 'disabled'])
|
8
|
+
expect(PaidSubscriber.possible_states).to eq(['inactive', 'active', 'unsubscribed', 'disabled'])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'solidstate'
|
5
|
+
|
6
|
+
class Subscriber
|
7
|
+
include SolidState
|
8
|
+
|
9
|
+
states :inactive, :active, :unsubscribed, :disabled do
|
10
|
+
transitions :from => :inactive, :to => :active
|
11
|
+
transitions :from => :active, :to => [:unsubscribed, :disabled]
|
12
|
+
transitions :from => :unsubscribed, :to => :active
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class PaidSubscriber < Subscriber
|
17
|
+
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: solidstate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,6 +86,20 @@ dependencies:
|
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: fuubar
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
89
103
|
description: Minuscule state machine.
|
90
104
|
email:
|
91
105
|
- tomas@forkhq.com
|
@@ -100,6 +114,8 @@ files:
|
|
100
114
|
- examples/mongo_mapper.rb
|
101
115
|
- lib/solidstate.rb
|
102
116
|
- solidstate.gemspec
|
117
|
+
- spec/solidstate/solidstate_spec.rb
|
118
|
+
- spec/spec_helper.rb
|
103
119
|
homepage: https://github.com/tomas/solidstate
|
104
120
|
licenses: []
|
105
121
|
metadata: {}
|
@@ -119,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
135
|
version: 1.3.6
|
120
136
|
requirements: []
|
121
137
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.
|
138
|
+
rubygems_version: 2.7.3
|
123
139
|
signing_key:
|
124
140
|
specification_version: 4
|
125
141
|
summary: Minuscule state machine.
|