enum-transition 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4326786bfdf9f0aa12df5a8ef184e5f50237c74db08cf56cfba2eeb4dc3a45e8
4
+ data.tar.gz: 7f7cd29ae5b67f1e4649c5e9ceb6d53e26e1a6db88beb5e38ffadc9eaeae4636
5
+ SHA512:
6
+ metadata.gz: 6e7910cc48097e0c7555506ef410d1a071072f78a6e83742ef7a513a9153ae7c2b12c1a29b6e12dd69505fcc039435e10117f22a2218222c02a0f0075eb5caa8
7
+ data.tar.gz: 5cc0a1f8c17a33e4173ae8fbd915724c1eddd2ea52e431a00d6c8beeaa53e4aa89263ffd7a49835888badadf655ab693a6530f0507b874e9c2120f41191a629b
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Snm Maurya
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Enum::Transition
2
+ Active Record ENUM based state machine. Whether you can control enum transation from one state to another state. Suppose you have an user model and you want to once user has been rejected he can't be verified, You can achive this job easily using this library.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'enum-transition'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install enum-transition
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # You have an user model and you want status transition should be verified or rejected from only initial
25
+
26
+ class User
27
+ include Enum::Transition
28
+
29
+ enum status: {initial: 0, verified: 1, rejected: 2]
30
+
31
+ # Enum transition
32
+ enum_columns [:status]
33
+ enum_transitions :initial, [:verified, :rejected]
34
+ end
35
+
36
+ user = User.create(status: :initial)
37
+ user.verified!
38
+ user.rejected! # ActiveRecord::RecordInvalid: Validation failed: Status can't be changed from verified to rejected
39
+
40
+ ```
41
+
42
+ ## Contributing
43
+ Contribution directions go here.
44
+
45
+ ## License
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Enum::Transition'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+
33
+ task default: :test
@@ -0,0 +1,36 @@
1
+ module Enum
2
+ module Transition
3
+ extend ActiveSupport::Concern
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ after_validation :ensure!
8
+ base.extend ClassMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def enum_columns columns
14
+ instance_variable_set("@enum_columns_array", columns)
15
+ end
16
+
17
+ def enum_transitions from_transition, to_transitions
18
+ to_transitions.each do |to_transition|
19
+ define_method "#{from_transition}_to_#{to_transition}" do
20
+ true
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def ensure!
27
+ self.class.name.constantize.instance_variable_get('@enum_columns_array').each do |column|
28
+ if !self.new_record? && self.send("#{column}_changed?")
29
+ unless self.respond_to? "#{self.send("#{column}_was")}_to_#{self.send("#{column}")}"
30
+ self.errors.add(:base, "Status can't be changed from #{self.send("#{column}_was")} to #{self.send("#{column}")}")
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module Enum
2
+ module Transition
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :enum_transition do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enum-transition
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Snm Maurya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.6
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.1.6.1
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 5.1.6
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.1.6.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: sqlite3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.3'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.3.6
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.3'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.3.6
53
+ description: Active Record ENUM based state machine, whether you can restrict status
54
+ transition from one to other
55
+ email:
56
+ - snmmaurya@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - MIT-LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - lib/enum/transition.rb
65
+ - lib/enum/transition/version.rb
66
+ - lib/tasks/enum/transition_tasks.rake
67
+ homepage: https://www.github.com/snmmaurya/enum-transition
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.7.3
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Active Record ENUM based state machine
91
+ test_files: []