huwahuwa_state 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a88a9b7bfe339222b625f4cd9563ffb04a2500e
4
+ data.tar.gz: 7103948a62a3e756c8c912237c5c59367bbd8ddb
5
+ SHA512:
6
+ metadata.gz: 9266eba4cf50dee3ae2f482990bbe6074e2d5830558e19152befe92a58a3c140243670d645419da28204d99db7186dffa0a44e6557fc2dfa880c28cca30e1aad
7
+ data.tar.gz: f46f1d27adc30667520bceb0dd624c5873220e05fb39a766785c8ecdb68b1082a462bfe0d88d547fe1511cbd3d0a811b1c3ea41d2660f960d867c0c18d7d92d8
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in huwahuwa_state.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 jiikko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+
2
+ # HuwahuwaState - Ruby simple state machines
3
+ [![Build Status](https://travis-ci.org/jiikko/huwahuwa_state.svg?branch=master)](https://travis-ci.org/jiikko/huwahuwa_state)
4
+
5
+ using ActiveRecord::Enum.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'huwahuwa_state', github: 'jiikko/huwahuwa_state'
12
+
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Requirements
19
+ - "activerecord", ">= 4.1"
20
+
21
+ ## Example
22
+ ```ruby
23
+ class User < ActiveRecord::Base
24
+ enum state: [:state_active, :state_freeze, :state_rock, :state_pending]
25
+ huwahuwa_state column_name: :state
26
+
27
+ huwahuwa_state :state_active, from: [:state_pending, :state_freeze, :state_pending] do |record|
28
+ # something
29
+ record.state_active!
30
+ # something
31
+ end
32
+
33
+ huwahuwa_state :state_rock, from: [:state_active] do |record|
34
+ record.state_rock!
35
+ end
36
+ end
37
+ ```
38
+
39
+ ```ruby
40
+ # success
41
+ user = User.create(state: User.states["state_pending"])
42
+ user.update_state!(:state_active)
43
+ user.can_state_active? # => true
44
+ user.state # => 'state_active'
45
+
46
+ # failure
47
+ user = User.create(state: User.states["state_freeze"])
48
+ user.can_state_rock? # => false
49
+ user.update_state!(:state_rock) # => HuwahuwaState::NotAcceptedUpdate
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/[my-github-username]/huwahuwa_state/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'huwahuwa_state/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "huwahuwa_state"
8
+ spec.version = HuwahuwaState::VERSION
9
+ spec.authors = ["jiikko"]
10
+ spec.email = ["n905i.1214@gmail.com"]
11
+ spec.summary = %q{State machines for Ruby.}
12
+ spec.description = %q{State machines for Ruby.}
13
+ spec.homepage = "https://github.com/jiikko/huwahuwa_state"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_dependency "activerecord", ">= 4.1"
27
+ spec.add_dependency 'activesupport'
28
+ end
@@ -0,0 +1,14 @@
1
+ require "huwahuwa_state/version"
2
+ require 'huwahuwa_state/main'
3
+
4
+ if defined?(Rails)
5
+ class Railtie < Rails::Railtie
6
+ initializer 'initialize huwahuwa_state' do
7
+ ActiveSupport.on_load(:active_record) do
8
+ ::ActiveRecord::Base.send(:include, HuwahuwaState::Main)
9
+ end
10
+ end
11
+ end
12
+ else
13
+ ::ActiveRecord::Base.send(:include, HuwahuwaState::Main)
14
+ end
@@ -0,0 +1,32 @@
1
+ module HuwahuwaState
2
+ class NotAcceptedUpdate < StandardError; end
3
+
4
+ module Main
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def self.huwahuwa_state(state_name = nil, column_name: nil, from: nil, options: [], &block)
9
+ if state_name.nil?
10
+ @@column_name = column_name || :name
11
+ return
12
+ end
13
+
14
+ raise(ArgumentError, 'missing keyword: from') if from.nil?
15
+ define_method "can_#{state_name}?" do |and_options: []| # can_hoge?が定義される
16
+ valid_optionsed = true
17
+ if options.present?
18
+ valid_optionsed = and_options.map { |x| options.include?(x) }.any?
19
+ end
20
+ valid_state = from.map(&:to_s).include?(self.send(@@column_name))
21
+ valid_state && valid_optionsed
22
+ end
23
+ self.class_variable_set("@@_block_#{state_name}", block)
24
+ end
25
+ end
26
+
27
+ def update_state!(state_name, and_options: [])
28
+ raise(NotAcceptedUpdate) unless send("can_#{state_name}?", and_options: and_options)
29
+ self.class.class_variable_get("@@_block_#{state_name}").call(self)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module HuwahuwaState
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe HuwahuwaState::Main do
4
+ context '正しい遷移する時' do
5
+ it "pendingからactiveに遷移すること" do
6
+ user = User.create(state: User.states["state_pending"])
7
+ user.update_state!(:state_active)
8
+ expect(user.state).to eq('state_active')
9
+ end
10
+
11
+ it "activeからlockへ遷移すること" do
12
+ user = User.create(state: User.states["state_active"])
13
+ user.update_state!(:state_rock)
14
+ expect(user.state).to eq('state_rock')
15
+ end
16
+ end
17
+
18
+ context '想定していない遷移をする時' do
19
+ it "catch exception" do
20
+ user = User.create(state: User.states["state_active"])
21
+ expect {
22
+ user.update_state!(:state_pending)
23
+ }.to raise_error(HuwahuwaState::NotAcceptedUpdate)
24
+ end
25
+ end
26
+
27
+ context 'optionsに値が設定されている時' do
28
+ context '正しい遷移する時' do
29
+ it 'untouchedからworkingへ遷移すること' do
30
+ issue = Issue.create(state: Issue.states["untouched"])
31
+ issue.update_state!(:working, and_options: [:worker])
32
+ expect(issue.state).to eq('working')
33
+ end
34
+
35
+ it 'resolvedからfinishedになること' do
36
+ issue = Issue.create(state: Issue.states["resolved"])
37
+ issue.update_state!(:finished, and_options: [:manager])
38
+ expect(issue.state).to eq('finished')
39
+ end
40
+ end
41
+
42
+ context '想定していない遷移をする時' do
43
+ it "catch exception" do
44
+ issue = Issue.create(state: Issue.states["untouched"])
45
+ expect {
46
+ issue.update_state!(:working, and_options: [:other_user])
47
+ }.to raise_error(HuwahuwaState::NotAcceptedUpdate)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe HuwahuwaState do
4
+ it 'has a version number' do
5
+ expect(HuwahuwaState::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :name
5
+ t.integer :state
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class CreateIssues < ActiveRecord::Migration
2
+ def change
3
+ create_table :issues do |t|
4
+ t.string :name
5
+ t.integer :state
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'pry'
4
+ require "active_record"
5
+ require 'huwahuwa_state'
6
+ require 'support/models/user'
7
+ require 'support/models/issue'
8
+
9
+ ActiveRecord::Base.establish_connection(
10
+ adapter: 'sqlite3',
11
+ database: ':memory:'
12
+ )
13
+ ActiveRecord::Migrator.migrate(File.expand_path('../migrations', __FILE__))
@@ -0,0 +1,12 @@
1
+ class Issue < ActiveRecord::Base
2
+ enum state: [:untouched, :working, :pending_review, :resolved, :finished]
3
+ huwahuwa_state column_name: :state
4
+
5
+ huwahuwa_state :working, from: [:untouched, :pending_review, :resolved], options: [:worker] do |record|
6
+ record.working!
7
+ end
8
+
9
+ huwahuwa_state :finished, from: [:resolved], options: [:manager] do |record|
10
+ record.finished!
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ class User < ActiveRecord::Base
2
+ enum state: [:state_active, :state_freeze, :state_rock, :state_pending]
3
+ huwahuwa_state column_name: :state
4
+
5
+ huwahuwa_state :state_active, from: [:state_pending, :state_freeze, :state_pending] do |record|
6
+ # some
7
+ record.state_active!
8
+ # some
9
+ end
10
+
11
+ huwahuwa_state :state_rock, from: [:state_active] do |record|
12
+ record.state_rock!
13
+ end
14
+
15
+ huwahuwa_state :state_pending, from: [] do |record|
16
+ # do nothing
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huwahuwa_state
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jiikko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '4.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '4.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: State machines for Ruby.
112
+ email:
113
+ - n905i.1214@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - huwahuwa_state.gemspec
126
+ - lib/huwahuwa_state.rb
127
+ - lib/huwahuwa_state/main.rb
128
+ - lib/huwahuwa_state/version.rb
129
+ - spec/huwahuwa_state/main_spec.rb
130
+ - spec/huwahuwa_state_spec.rb
131
+ - spec/migrations/001_create_users.rb
132
+ - spec/migrations/002_create_issues.rb
133
+ - spec/spec_helper.rb
134
+ - spec/support/models/issue.rb
135
+ - spec/support/models/user.rb
136
+ homepage: https://github.com/jiikko/huwahuwa_state
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.2.0
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: State machines for Ruby.
160
+ test_files:
161
+ - spec/huwahuwa_state/main_spec.rb
162
+ - spec/huwahuwa_state_spec.rb
163
+ - spec/migrations/001_create_users.rb
164
+ - spec/migrations/002_create_issues.rb
165
+ - spec/spec_helper.rb
166
+ - spec/support/models/issue.rb
167
+ - spec/support/models/user.rb