solidstate 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -2
- data/examples/active_record.rb +26 -0
- data/examples/mongo_mapper.rb +15 -0
- data/lib/solidstate.rb +0 -6
- data/solidstate.gemspec +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 737e65abda45b1dc1b7ac8620811693c22832554
|
4
|
+
data.tar.gz: 93039656e96b052c7f92c6ac1c2c8c729cba01aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd76cc42646c4370f3f0d931a16cc112a7a9dd9116b57be6bddce77f9eb7c7465c3a1aaccbcab1ac4a4ea7ac975938216231cdcd0b7a9954259aed5fe6ea7e1b
|
7
|
+
data.tar.gz: aa6734b9c93068df1c9f3d766743aed49a4f11255ac036d67cecaf9af0a5c9e24e3da4ffa985830c746fbb651c07987a8e6c2b9e453fc4e6ea0caed3da8761d7
|
data/README.md
CHANGED
@@ -3,6 +3,14 @@ SolidState
|
|
3
3
|
|
4
4
|
Minuscule but solid state machine for Ruby classes. The only dependency is that your model responds to a getter and setter for `state`.
|
5
5
|
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
In your Gemfile:
|
9
|
+
|
10
|
+
gem 'solidstate'
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
6
14
|
``` ruby
|
7
15
|
|
8
16
|
# simplest example, using just an accessor
|
@@ -18,7 +26,7 @@ Minuscule but solid state machine for Ruby classes. The only dependency is that
|
|
18
26
|
# whether the current status is X or Y.
|
19
27
|
|
20
28
|
p = Post.new
|
21
|
-
p.state # => 'draft'
|
29
|
+
p.state # => 'draft' (default value set in database)
|
22
30
|
p.draft? # true
|
23
31
|
p.published? # => false
|
24
32
|
p.state = 'published'
|
@@ -42,6 +50,11 @@ Minuscule but solid state machine for Ruby classes. The only dependency is that
|
|
42
50
|
p.state = 'deleted'
|
43
51
|
p.valid? # => false
|
44
52
|
|
53
|
+
# you also get scopes for free if the class responds_to the `scope` method
|
54
|
+
|
55
|
+
Post.published.first # => #<Post ...>
|
56
|
+
Post.draft.count # => 1
|
57
|
+
|
45
58
|
# ok, now let's gets get fancier. we're going to declare transitions
|
46
59
|
# which will govern the possible directions in which an object's state
|
47
60
|
# can move to.
|
@@ -64,10 +77,11 @@ Minuscule but solid state machine for Ruby classes. The only dependency is that
|
|
64
77
|
# if so, sets the new state and optionally saves the record.
|
65
78
|
|
66
79
|
s.active! # => true
|
67
|
-
|
80
|
+
s.state # => 'active'
|
68
81
|
s.inactive! # => raises InvalidTransitionError
|
69
82
|
|
70
83
|
# this also works outside transition methods, of course.
|
84
|
+
|
71
85
|
s.reload # => true
|
72
86
|
s.active? # => true
|
73
87
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'active_record'
|
3
|
+
require_relative '../lib/solidstate'
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection(
|
6
|
+
"adapter" => "sqlite3",
|
7
|
+
"database" => ':memory:'
|
8
|
+
)
|
9
|
+
|
10
|
+
# ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define :version => 0 do
|
13
|
+
create_table "subscribers", :force => true do |t|
|
14
|
+
t.string "state", default: "inactive"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Subscriber < ActiveRecord::Base
|
19
|
+
include SolidState
|
20
|
+
|
21
|
+
states :inactive, :active, :unsubscribed, :disabled do
|
22
|
+
transitions :from => :inactive, :to => :active
|
23
|
+
transitions :from => :active, :to => [:unsubscribed, :disabled]
|
24
|
+
transitions :from => :unsubscribed, :to => :active
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'mongo_mapper'
|
3
|
+
require_relative '../lib/solidstate'
|
4
|
+
|
5
|
+
class Person
|
6
|
+
include MongoMapper::Document
|
7
|
+
include SolidState
|
8
|
+
|
9
|
+
STATES = %w(awake sleeping)
|
10
|
+
|
11
|
+
key :state, String, :default => STATES.first
|
12
|
+
# ensure_index :state
|
13
|
+
|
14
|
+
states *STATES
|
15
|
+
end
|
data/lib/solidstate.rb
CHANGED
data/solidstate.gemspec
CHANGED
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
|
+
version: 0.3.0
|
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: 2017-
|
11
|
+
date: 2017-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,6 +96,8 @@ files:
|
|
96
96
|
- Gemfile
|
97
97
|
- README.md
|
98
98
|
- Rakefile
|
99
|
+
- examples/active_record.rb
|
100
|
+
- examples/mongo_mapper.rb
|
99
101
|
- lib/solidstate.rb
|
100
102
|
- solidstate.gemspec
|
101
103
|
homepage: https://github.com/tomas/solidstate
|