just_state_machine 0.1.0 → 0.2.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: 2c74ea95f48cdea857973b9cd65ff7985ef34f21
4
- data.tar.gz: 0fa48156ccd7ef61c450855c053b2d362a5e34ee
3
+ metadata.gz: a3c6bd3c8889f049fec253dd6f28baacef949905
4
+ data.tar.gz: 4886b6301006dae04f550c9ba3d98963ca75f2af
5
5
  SHA512:
6
- metadata.gz: 0ca6342ddbe7928a00f6afa8bc9adc5cb9ee09832e877c275f431a22e7a97e411e9d8913a8243b57213e3fa33956e4d4182af0e3168a56137231e2d64270a172
7
- data.tar.gz: bfa61404cbf03c319c0e09ccf46ca01298ee8a1c12f6a2b17d26db169689bef2b3d03d1d764797eec1395810af7e4e45419461a2450ab90ef9329f97ed0a7f00
6
+ metadata.gz: 84500a6b5717d563c659e6c999bb3d5f9a4f9102c5c63167bd035349c8efc8c30b3502cb74b95955f5941fd4694bb6d6e53eb7d0d1b143cdb31edf27e0ac134d
7
+ data.tar.gz: f3a632523feda1353f2e960dcd380f942f22d18a71fc53cddf5bcf0242ef6108077fc7a072999cfbe0ea819cb94e39eb479728df6f6f92f34a6d67ab8174349a
data/README.md CHANGED
@@ -82,7 +82,38 @@ user.downgrade_title
82
82
  user.downgrade_title!
83
83
  user.can_downgrade_title?
84
84
  ```
85
+ ### State
86
+ Define your state **before** define others(validation, event, etc). It is to prevent you define transition, validation for unwanted state.
87
+ You can also define the `initial state`. Initial State is state value that is given when you don't set any value to state attribute in the instance on initialization. Initial State is optional.
85
88
 
89
+ ```ruby
90
+ class UserStateMachine < Jsm::Base
91
+ attribute_name :title
92
+
93
+ state :beginner, initial: true
94
+ state :intermediate
95
+ state :master
96
+ # more code here
97
+ end
98
+
99
+ class User
100
+ include Jsm::Client
101
+ jsm_use UserStateMachine
102
+
103
+ attr_accessor :title
104
+ #your code here
105
+
106
+ def initialize(title = nil)
107
+ @title = title
108
+ end
109
+ end
110
+
111
+ user = User.new
112
+ user.current_state # :beginner
113
+
114
+ user = User.new(:intermediate)
115
+ user.current_state # :intermediate
116
+ ```
86
117
  ### Validation
87
118
  This is useful, when you want to allow transition to a specified state allowed when it pass the validation. Validation should return true if passed validation and false if failed.
88
119
  **note**: Dont forget to define the state first, because if not then Jsm will raise error `Jsm::InvalidStateError`. This is to prevent typo when add new validation
@@ -204,7 +235,6 @@ user.errors[:title] # ["is not between 20 and 50"]
204
235
  class UserStateMachine < Jsm::Base
205
236
  attribute_name :title
206
237
 
207
- state :unconfirmed
208
238
  state :beginner
209
239
  state :intermediate
210
240
  state :master
@@ -217,7 +247,7 @@ class UserStateMachine < Jsm::Base
217
247
 
218
248
  validate :master do |user|
219
249
  unless user.current_level > 50
220
- errors.add(:title, 'have not reached 50')
250
+ user.errors.add(:title, 'have not reached 50')
221
251
  end
222
252
  end
223
253
 
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "jsm"
4
+ require "just_state_machine"
5
5
  Dir[File.expand_path("../../examples/**/*.rb",__FILE__)].each{ |f| require f }
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
@@ -1,7 +1,7 @@
1
1
  class UserStateMachine < Jsm::Base
2
2
  attribute_name :relationship
3
3
 
4
- state :single
4
+ state :single, initial: true
5
5
  state :in_relationship
6
6
  state :married
7
7
  state :divorced
@@ -31,4 +31,7 @@ class UserBasic
31
31
  jsm_use UserStateMachine
32
32
 
33
33
  attr_accessor :relationship
34
+ def initialize(relationship = nil)
35
+ @relationship = relationship
36
+ end
34
37
  end
data/jsm.gemspec CHANGED
@@ -26,6 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "pry"
27
27
  spec.add_development_dependency "pry-doc"
28
28
  spec.add_development_dependency "looksee"
29
- spec.add_development_dependency "activerecord", "~> 4.1"
29
+ spec.add_development_dependency "activerecord", ">= 4.0"
30
30
  spec.add_development_dependency "sqlite3"
31
31
  end
data/lib/jsm/base.rb CHANGED
@@ -15,6 +15,10 @@ class Jsm::Base
15
15
  # example
16
16
  # state :x
17
17
  # state :y
18
+ # if put params initial true
19
+ # it will be treated as initial_state
20
+ # example:
21
+ # state :x, initial: true
18
22
  def self.state(name, params = {})
19
23
  @states ||= Jsm::States.new
20
24
  @states.add_state(name, initial: params[:initial])
@@ -25,6 +29,14 @@ class Jsm::Base
25
29
  @states.list
26
30
  end
27
31
 
32
+ # return initial_state
33
+ # if empty return nil
34
+ def self.initial_state
35
+ if @states
36
+ @states.initial_state
37
+ end
38
+ end
39
+
28
40
  # add new event to the class and add its transition
29
41
  # example:
30
42
  # event :do_this do
data/lib/jsm/client.rb CHANGED
@@ -38,6 +38,19 @@ module Jsm::Client
38
38
  Jsm::Machines.add_machines(self, state_machine.new(self))
39
39
  end
40
40
 
41
+ # override method new
42
+ # it is used for set the instance state attribute with initial_state
43
+ # if initial_state present & instance state attribute is nil
44
+ def new(*args, &block)
45
+ obj = super
46
+ initial_state = self.state_machine.initial_state
47
+
48
+ if initial_state && !obj.current_state
49
+ obj.send(:jsm_set_state, initial_state.name)
50
+ end
51
+ obj
52
+ end
53
+
41
54
  #define type of event executor to be used
42
55
  def jsm_event_executor
43
56
  Jsm::EventExecutor::Base
@@ -1,15 +1,4 @@
1
1
  class Jsm::EventExecutor::ActiveModel < Jsm::EventExecutor::Base
2
- # it execute event for the object.
3
- # If transition failed or invalid by validation toward the object,
4
- # then it will return false
5
- def execute(event, obj)
6
- if can_be_executed?(event, obj)
7
- event.execute(obj)
8
- else
9
- false
10
- end
11
- end
12
-
13
2
  # check if the obj possible to execute the event
14
3
  def can_be_executed?(event, obj)
15
4
  state = event.can_be_transitioning_to(obj)
data/lib/jsm/states.rb CHANGED
@@ -9,14 +9,25 @@ class Jsm::States
9
9
 
10
10
  # register new state into the list
11
11
  # @param state_name
12
+ # @param params: allowed params is `initial`(boolean value, default is false)
12
13
  def add_state(state_name, params = {})
13
14
  initial = params.fetch(:initial) { false }
14
15
  if !state_unique?(state_name)
15
16
  raise Jsm::NotUniqueStateError, "state #{state_name} has been defined"
16
17
  end
17
18
 
19
+ if initial && !initial_state.nil?
20
+ raise Jsm::InvalidStateError,"can not set initial state to #{state_name}. current initial state is #{initial_state.name}"
21
+ end
22
+
18
23
  state = create_state(state_name, initial)
19
24
  list.push(state)
25
+
26
+ @initial_state = state if state.initial
27
+ end
28
+
29
+ def initial_state
30
+ @initial_state
20
31
  end
21
32
 
22
33
  def has_state?(state_name)
data/lib/jsm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jsm
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1 @@
1
+ require 'jsm'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: just_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - wendy0402
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-17 00:00:00.000000000 Z
11
+ date: 2015-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,16 +98,16 @@ dependencies:
98
98
  name: activerecord
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - "~>"
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '4.1'
103
+ version: '4.0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - "~>"
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: '4.1'
110
+ version: '4.0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: sqlite3
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -158,6 +158,7 @@ files:
158
158
  - lib/jsm/validator.rb
159
159
  - lib/jsm/validators.rb
160
160
  - lib/jsm/version.rb
161
+ - lib/just_state_machine.rb
161
162
  homepage: https://github.com/wendy0402/jsm
162
163
  licenses:
163
164
  - MIT