solidstate 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2440bdd08c03d227ce1343783e7fc863d197db67
4
- data.tar.gz: 56ea18976e16dce884b21b2b1bc0a94a7be1b564
3
+ metadata.gz: 737e65abda45b1dc1b7ac8620811693c22832554
4
+ data.tar.gz: 93039656e96b052c7f92c6ac1c2c8c729cba01aa
5
5
  SHA512:
6
- metadata.gz: 6d59cea2fb06b79b7f660cd4dcd8fff0927e843760434f3eba84edafcf87e7671a3577b06632b3eba225520eae0e0104fbb2445db1f5fd6addf493dcf42c4286
7
- data.tar.gz: a1f6bb99d36c2ef57ea9914477d6863ed811895cfc817df780bfac72bde5cb082c10b53e1bf19f468af9e01e1900010118ddff87d7ebed846a24dc386ea58f95
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
@@ -72,12 +72,6 @@ module SolidState
72
72
  end
73
73
  end
74
74
 
75
- def once_not_subscribed
76
-
77
- end
78
-
79
- def after
80
-
81
75
  def set_state(new_state)
82
76
  return false unless can_transition_to?(new_state)
83
77
  self.state = new_state
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "solidstate"
5
- s.version = '0.2.0'
5
+ s.version = '0.3.0'
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ['Tomás Pollak']
8
8
  s.email = ['tomas@forkhq.com']
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.2.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-03-11 00:00:00.000000000 Z
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