pretends_like_state_machine 0.0.1

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
+ SHA1:
3
+ metadata.gz: 4df10f03539a19ff773334aebc84ec9c80a3106f
4
+ data.tar.gz: f8f4c44aed38a5b0ac7c778f023de1b1d466a5ff
5
+ SHA512:
6
+ metadata.gz: 82fe345a840cc7194bb7685cb84c343375f9d516817fafc9db087e0f474bdcd2f823196f91d4f8d733da982dba848cd71536042e46fea09213ce4561c9b12e80
7
+ data.tar.gz: 8767d8e965240cd8ed46d4d13c538039aaa2e588566c8b013cb6f9d51f2d9f0f48db81e72340ee92a166328f3545d6c118a68725f2828f838f00046578b0a8bb
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Pablo Torrecilla
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/Rakefile ADDED
@@ -0,0 +1,32 @@
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 = 'PretendsLikeStateMachine'
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
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,4 @@
1
+ require 'pretends_like_state_machine/mongoid/pretends_like_state_machine' if defined?(::Mongoid)
2
+
3
+ module PretendsLikeStateMachine
4
+ end
@@ -0,0 +1,23 @@
1
+ module PretendsLikeStateMachine
2
+ module Mongoid
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def pretends_like_state_machine(options = {})
7
+ field :state, type: Symbol
8
+
9
+ options[:states].each do |state|
10
+ define_method("#{state}!") do
11
+ update_attribute(:state, state)
12
+ end
13
+
14
+ define_method("#{state}?") do
15
+ self.state == state
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Mongoid::Document.send :include, PretendsLikeStateMachine::Mongoid
@@ -0,0 +1,3 @@
1
+ module PretendsLikeStateMachine
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :pretends_like_state_machine do
3
+ # # Task goes here
4
+ # end
data/test/mongoid.yml ADDED
@@ -0,0 +1,69 @@
1
+ development:
2
+ # Configure available database sessions. (required)
3
+ sessions:
4
+ # Defines the default session. (required)
5
+ default:
6
+ # Defines the name of the default database that Mongoid can connect to.
7
+ # (required).
8
+ database: wtg_development
9
+ # Provides the hosts the default session can connect to. Must be an array
10
+ # of host:port pairs. (required)
11
+ hosts:
12
+ - localhost:27017
13
+ options:
14
+ # Change the default write concern. (default = { w: 1 })
15
+ # write:
16
+ # w: 1
17
+
18
+ # Change the default consistency model to primary, secondary.
19
+ # 'secondary' will send reads to secondaries, 'primary' sends everything
20
+ # to master. (default: primary)
21
+ # read: secondary_preferred
22
+
23
+ # How many times Moped should attempt to retry an operation after
24
+ # failure. (default: 30)
25
+ # max_retries: 30
26
+
27
+ # The time in seconds that Moped should wait before retrying an
28
+ # operation on failure. (default: 1)
29
+ # retry_interval: 1
30
+ # Configure Mongoid specific options. (optional)
31
+ options:
32
+ # Includes the root model name in json serialization. (default: false)
33
+ # include_root_in_json: false
34
+
35
+ # Include the _type field in serializaion. (default: false)
36
+ # include_type_for_serialization: false
37
+
38
+ # Preload all models in development, needed when models use
39
+ # inheritance. (default: false)
40
+ # preload_models: false
41
+
42
+ # Protect id and type from mass assignment. (default: true)
43
+ # protect_sensitive_fields: true
44
+
45
+ # Raise an error when performing a #find and the document is not found.
46
+ # (default: true)
47
+ # raise_not_found_error: true
48
+
49
+ # Raise an error when defining a scope with the same name as an
50
+ # existing method. (default: false)
51
+ # scope_overwrite_exception: false
52
+
53
+ # Use Active Support's time zone in conversions. (default: true)
54
+ # use_activesupport_time_zone: true
55
+
56
+ # Ensure all times are UTC in the app side. (default: false)
57
+ # use_utc: false
58
+ test:
59
+ sessions:
60
+ default:
61
+ database: wtg_test
62
+ hosts:
63
+ - localhost:27017
64
+ options:
65
+ read: primary
66
+ # In the test environment we lower the retries and retry interval to
67
+ # low amounts for fast failures.
68
+ max_retries: 1
69
+ retry_interval: 0
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class MyProcess
4
+ include Mongoid::Document
5
+
6
+ pretends_like_state_machine states: [:waiting, :processing, :processed]
7
+ end
8
+
9
+ describe PretendsLikeStateMachine do
10
+ it 'state defaults to nil' do
11
+ MyProcess.new.state.must_be_nil
12
+ end
13
+
14
+ it 'transition to a valid state must perform a transition to the state' do
15
+ my_process = MyProcess.new
16
+ my_process.waiting!
17
+ my_process.state.must_equal :waiting
18
+ end
19
+
20
+ it 'the `state?` operation must return true if the machine is in `state` and false in other case' do
21
+ my_process = MyProcess.new
22
+ my_process.waiting!
23
+ my_process.waiting?.must_equal true
24
+ my_process.processing?.must_equal false
25
+ end
26
+
27
+ it 'transition to an invalid state must throw a NoMethodError exception' do
28
+ my_process = MyProcess.new
29
+ assert_raises NoMethodError do
30
+ my_process.meow!
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,8 @@
1
+ ENV["MONGOID_ENV"] = "test"
2
+
3
+ require 'test_helper'
4
+ require 'mongoid'
5
+ require 'pretends_like_state_machine'
6
+ require 'minitest/autorun'
7
+
8
+ Mongoid.load!('./test/mongoid.yml')
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretends_like_state_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Torrecilla
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-11 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.0
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.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongoid
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: A minimal, trivial, deadly simple state machine for Mongoid
42
+ email:
43
+ - pau@nosolopau.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - Rakefile
50
+ - lib/pretends_like_state_machine.rb
51
+ - lib/pretends_like_state_machine/mongoid/pretends_like_state_machine.rb
52
+ - lib/pretends_like_state_machine/version.rb
53
+ - lib/tasks/pretends_like_state_machine_tasks.rake
54
+ - test/mongoid.yml
55
+ - test/pretends_like_state_machine_test.rb
56
+ - test/test_helper.rb
57
+ homepage: http://nosolopau.com
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.0
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: A minimal, trivial, deadly simple state machine for Mongoid
81
+ test_files:
82
+ - test/mongoid.yml
83
+ - test/pretends_like_state_machine_test.rb
84
+ - test/test_helper.rb