state_machine_setters 1.0.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.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/lib/state_machine_setters.rb +33 -0
- data/lib/state_machine_setters/version.rb +3 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/state_machine_setters_spec.rb +48 -0
- data/state_machine_setters.gemspec +19 -0
- metadata +84 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create default@state_machine_setters
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Adam Hunter
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# StateMachineSetters
|
2
|
+
|
3
|
+
Adds setters for state machine events to trigger state transitions by setting attributes.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
class ExampleModel < ActiveRecord::Base
|
8
|
+
state_machine :state do
|
9
|
+
event :pay do
|
10
|
+
transition :unpaid => :paid
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
state_machine_setter :state
|
15
|
+
end
|
16
|
+
|
17
|
+
@example = ExampleModel.new
|
18
|
+
@example.pay = true # triggers @example.pay
|
19
|
+
|
20
|
+
# params[:example] == {:pay => {:purchased_by_id => 10, :purchase_amount => 50}}
|
21
|
+
@example.update_attributes(params[:example]) # will set the attributes with the hash then call `pay`
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
gem 'state_machine_setters'
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
$ gem install state_machine_setters
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
TODO: Write usage instructions here
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "state_machine"
|
2
|
+
require "state_machine_setters/version"
|
3
|
+
|
4
|
+
module StateMachineSetters
|
5
|
+
|
6
|
+
def state_machine_setter(state_machine_name)
|
7
|
+
ensure_state_machine!(state_machine_name)
|
8
|
+
add_state_machine_setters(state_machine_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def ensure_state_machine!(name)
|
14
|
+
return if respond_to?(:state_machines) && state_machines[name]
|
15
|
+
raise MissingStateMachineError.new("#{name} has not been defined.")
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_state_machine_setters(name)
|
19
|
+
state_machines[name].events.map(&:name).each do |event|
|
20
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
21
|
+
def #{event}=(value)
|
22
|
+
self.attributes = value if value.is_a?(Hash)
|
23
|
+
#{event}
|
24
|
+
end
|
25
|
+
RUBY
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class MissingStateMachineError < StandardError ; end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
Object.extend StateMachineSetters
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.unshift('../lib')
|
2
|
+
require 'state_machine_setters'
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
6
|
+
# loaded once.
|
7
|
+
#
|
8
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
end
|
14
|
+
|
15
|
+
class StateMachineExample
|
16
|
+
attr_accessor :state
|
17
|
+
state_machine :state do
|
18
|
+
event :run do
|
19
|
+
transition :still => :moving
|
20
|
+
end
|
21
|
+
end
|
22
|
+
state_machine_setter :state
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StateMachineSetters do
|
4
|
+
it "is a module" do
|
5
|
+
StateMachineSetters.should be_a(Module)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "loads state machine" do
|
9
|
+
Class.should respond_to(:state_machine)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "adds `state_machine_setter` to class" do
|
13
|
+
Class.should respond_to(:state_machine_setter)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "will raise a missing state_machine error when calling state_machine_setter without defining any state_machine first" do
|
17
|
+
expect { Class.state_machine_setter :some_machine }.to raise_error(StateMachineSetters::MissingStateMachineError)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "will raise a missing state_machine error when calling state_machine_setter without defining the state_machine first" do
|
21
|
+
expect { StateMachineExample.state_machine_setter :some_machine }.to raise_error(StateMachineSetters::MissingStateMachineError)
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "instances" do
|
25
|
+
before :each do
|
26
|
+
@example = StateMachineExample.new
|
27
|
+
end
|
28
|
+
|
29
|
+
it "adds a setter for each defined state transition in a class" do
|
30
|
+
@example.should respond_to(:run=)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "will pass given attributes to an attributes= method when called" do
|
34
|
+
@example.should_receive(:attributes=).with(:awesome => 'yes')
|
35
|
+
@example.run = {:awesome => 'yes'}
|
36
|
+
end
|
37
|
+
|
38
|
+
it "will not pass attributes to attributes= if no hash is provided" do
|
39
|
+
@example.should_not_receive(:attributes=)
|
40
|
+
@example.run = true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "will call the respective state transition when calling the setter" do
|
44
|
+
@example.should_receive(:run)
|
45
|
+
@example.run = true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/state_machine_setters/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Adam Hunter", "Chris Peters"]
|
6
|
+
gem.email = ["adamhunter@me.com", "chrispeters.psu@gmail.com"]
|
7
|
+
gem.description = %q{Adds setter methods based off of state machine transitions to allow triggering state changes from form attributes.}
|
8
|
+
gem.summary = %q{Trigger state machine transitions from setters.}
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "state_machine_setters"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = StateMachineSetters::VERSION
|
16
|
+
|
17
|
+
gem.add_dependency "state_machine", ">= 1.1.2"
|
18
|
+
gem.add_development_dependency "rspec", "~> 2.10.0"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: state_machine_setters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Hunter
|
9
|
+
- Chris Peters
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-06-14 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: state_machine
|
17
|
+
requirement: &70347273677700 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70347273677700
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
requirement: &70347273676580 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.10.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *70347273676580
|
37
|
+
description: Adds setter methods based off of state machine transitions to allow triggering
|
38
|
+
state changes from form attributes.
|
39
|
+
email:
|
40
|
+
- adamhunter@me.com
|
41
|
+
- chrispeters.psu@gmail.com
|
42
|
+
executables: []
|
43
|
+
extensions: []
|
44
|
+
extra_rdoc_files: []
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- .rspec
|
48
|
+
- .rvmrc
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/state_machine_setters.rb
|
54
|
+
- lib/state_machine_setters/version.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
- spec/state_machine_setters_spec.rb
|
57
|
+
- state_machine_setters.gemspec
|
58
|
+
homepage:
|
59
|
+
licenses: []
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.16
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Trigger state machine transitions from setters.
|
82
|
+
test_files:
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- spec/state_machine_setters_spec.rb
|