rails_machine 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9192990128344bfbd59af9da23b9381350c08734
4
+ data.tar.gz: 882bb537865d1662e6e8fba8eb6bd8f7f6bb256f
5
+ SHA512:
6
+ metadata.gz: 1874e59b1d7759c80e4d704efd758dde68f9253ef3e73deecac7cd0e0899b34c29b057ba7fe44dcc64fcdc71c576b5fe239df04f78f2a5c82b03e6aa6b5c1249
7
+ data.tar.gz: 5dde52dcae7c035db92715402ab5d87e4e0a2af702a7c578f53bdd0bca7b8d32e5fa241285d7cf14f360464a2b340625afa032c99825b74b6c493c847d204a00
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
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.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = RailsMachine
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
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 = 'RailsMachine'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,24 @@
1
+ module RailsMachine
2
+ class Configuration
3
+
4
+ attr_reader :states, :transitions
5
+
6
+ def initialize
7
+ @states ||= []
8
+ @transitions ||= {}
9
+ end
10
+
11
+ # Just runs code
12
+ def run(&blk)
13
+ self.instance_eval(&blk)
14
+ end
15
+
16
+ def state(name)
17
+ @states << name
18
+ end
19
+
20
+ def transition(from: :any, to: :any, guards: [])
21
+ (@transitions[from] ||= [])<< { to: to, guards: guards }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module RailsMachine
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'rails_machine/configuration'
2
+
3
+ module RailsMachine
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ cattr_accessor :transitions
8
+
9
+ validate :allowed_transition, if: :state_changed?
10
+ end
11
+
12
+ def allowed_transition
13
+ from = self.state_was.to_sym
14
+ to = self.state.to_sym
15
+
16
+ transitions = (transitions_for(from) + transitions_for(:any)).select { |t| t[:to] == to || t[:to] == :any }
17
+ errors.add(:state, :transition_not_found) if transitions.empty?
18
+
19
+ errors.add(:state, :guard_failed) if transitions.none? { |t| t[:guards].all? { |guard| guard.call(self) } }
20
+ end
21
+
22
+ module ClassMethods
23
+ def rails_machine(&blk)
24
+ raise ArgumentError unless block_given?
25
+
26
+ configuration = Configuration.new
27
+ configuration.run(&blk)
28
+
29
+ self.transitions = configuration.transitions
30
+
31
+ enum state: configuration.states
32
+ end
33
+ end
34
+
35
+ protected
36
+
37
+ def transitions_for(action)
38
+ self.class.transitions[action] || []
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_machine do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Grzegorz Łuszczek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-27 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: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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
+ description: Simple implementation of state machine for Rails using enums.
42
+ email:
43
+ - grzegorz@piklus.pl
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.rdoc
50
+ - Rakefile
51
+ - lib/rails_machine.rb
52
+ - lib/rails_machine/configuration.rb
53
+ - lib/rails_machine/version.rb
54
+ - lib/tasks/rails_machine_tasks.rake
55
+ homepage: https://github.com/grzlus/rails_machine
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '2.1'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.2.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: State machine for Rails.
79
+ test_files: []
80
+ has_rdoc: