aasm_ohm_persistence 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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +1 -0
- data/aasm_ohm_persistence.gemspec +24 -0
- data/lib/aasm/persistence/ohm_persistence.rb +117 -0
- data/lib/aasm_ohm_persistence.rb +1 -0
- data/lib/aasm_ohm_persistence/version.rb +3 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd98d5a84fbc31a12fe914ac93df689d5c4f00b8
|
4
|
+
data.tar.gz: 007a9c1fd0228530db036b6d58caddb5a30f2f95
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 98a6a18978b4720d1cff88447304872f65c67ef4fda9e0d222833c7cb873532f29624db8bb6d3fbcf76cd2e7d944fdc2f9556f48d03a692ed689d9097e8f6139
|
7
|
+
data.tar.gz: 019a29385ddc590b6129790475c746686176b4a52ba14e1375cf616b13e2e8d4f95e8d0208d44bc443c424c6c29042ec26edf82b4e4bd8d2c2129fab5e8afcbc
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Phuong Nguyen
|
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,54 @@
|
|
1
|
+
# AasmOhmPersistence
|
2
|
+
|
3
|
+
Use AASM with Ohm
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'aasm_ohm_persistence'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install aasm_ohm_persistence
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
So far this require ohm-contrib to work
|
22
|
+
|
23
|
+
To enable AASM for Ohm:
|
24
|
+
|
25
|
+
class MyRedisModal < Ohm::Model
|
26
|
+
include AASM
|
27
|
+
include AASM::Persistence::OhmPersistence
|
28
|
+
|
29
|
+
aasm do
|
30
|
+
# Your aasm logic
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
## Notice
|
35
|
+
|
36
|
+
Unlike active record which use instance method of class to create
|
37
|
+
callback, ohm-contrib use class method to create callbacks.
|
38
|
+
|
39
|
+
Since OhmPersistence will declare before_create inside the modal, if you
|
40
|
+
wish to include your own logic to the `before_create` callback, you need
|
41
|
+
to write it like that:
|
42
|
+
|
43
|
+
def before_create
|
44
|
+
super
|
45
|
+
# your own code
|
46
|
+
end
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'aasm_ohm_persistence/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "aasm_ohm_persistence"
|
8
|
+
spec.version = AasmOhmPersistence::VERSION
|
9
|
+
spec.authors = ["Phuong Nguyen"]
|
10
|
+
spec.email = ["phuongnd08@gmail.com"]
|
11
|
+
spec.description = %q{Persistence adapter to use AASM with Ohm}
|
12
|
+
spec.summary = %q{Persistence adapter to use AASM with Ohm}
|
13
|
+
spec.homepage = "http://github.com/phuongnd08/aasm_ohm_persistence"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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_dependency "ohm-contrib"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require "aasm/persistence/base"
|
2
|
+
require "ohm/callbacks"
|
3
|
+
|
4
|
+
module AASM
|
5
|
+
module Persistence
|
6
|
+
module OhmPersistence
|
7
|
+
# This method:
|
8
|
+
#
|
9
|
+
# * extends the model with ClassMethods
|
10
|
+
# * includes InstanceMethods
|
11
|
+
#
|
12
|
+
# Adds
|
13
|
+
#
|
14
|
+
# def before_create
|
15
|
+
# aasm_ensure_initial_state
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# As a result, you need to call super if you are going to define before_create yourself
|
19
|
+
#
|
20
|
+
# class Foo < Ohm::Model
|
21
|
+
# include AASM
|
22
|
+
# include AASM::Persistence::OhmPersistence
|
23
|
+
#
|
24
|
+
# def before_create
|
25
|
+
# super
|
26
|
+
# # your code here
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
def self.included(base)
|
31
|
+
base.send(:include, AASM::Persistence::Base)
|
32
|
+
base.extend AASM::Persistence::OhmPersistence::ClassMethods
|
33
|
+
base.send(:include, Ohm::Callbacks)
|
34
|
+
base.send(:include, AASM::Persistence::OhmPersistence::InstanceMethods)
|
35
|
+
end
|
36
|
+
|
37
|
+
module ClassMethods
|
38
|
+
def find_in_state(id, state, *args)
|
39
|
+
find(aasm_column.to_sym => state)[id]
|
40
|
+
end
|
41
|
+
|
42
|
+
def count_in_state(state, *args)
|
43
|
+
find(aasm_column.to_sym => state).count
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module InstanceMethods
|
48
|
+
def before_create
|
49
|
+
super
|
50
|
+
aasm_ensure_initial_state
|
51
|
+
end
|
52
|
+
|
53
|
+
# Writes <tt>state</tt> to the state column and persists it to the database
|
54
|
+
#
|
55
|
+
# foo = Foo.find(1)
|
56
|
+
# foo.aasm.current_state # => :opened
|
57
|
+
# foo.close!
|
58
|
+
# foo.aasm.current_state # => :closed
|
59
|
+
# Foo.find(1).aasm.current_state # => :closed
|
60
|
+
#
|
61
|
+
# NOTE: intended to be called from an event
|
62
|
+
|
63
|
+
def aasm_write_state(state)
|
64
|
+
old_value = self.send(self.class.aasm_column.to_sym)
|
65
|
+
aasm_write_state_without_persistence(state)
|
66
|
+
|
67
|
+
success = self.save
|
68
|
+
|
69
|
+
unless success
|
70
|
+
aasm_write_state_without_persistence(old_value)
|
71
|
+
return false
|
72
|
+
end
|
73
|
+
|
74
|
+
true
|
75
|
+
end
|
76
|
+
|
77
|
+
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
78
|
+
#
|
79
|
+
# foo = Foo.find(1)
|
80
|
+
# foo.aasm.current_state # => :opened
|
81
|
+
# foo.close
|
82
|
+
# foo.aasm.current_state # => :closed
|
83
|
+
# Foo.find(1).aasm.current_state # => :opened
|
84
|
+
# foo.save
|
85
|
+
# foo.aasm.current_state # => :closed
|
86
|
+
# Foo.find(1).aasm.current_state # => :closed
|
87
|
+
#
|
88
|
+
# NOTE: intended to be called from an event
|
89
|
+
def aasm_write_state_without_persistence(state)
|
90
|
+
self.send(:"#{self.class.aasm_column}=", state.to_s)
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
# Ensures that if the aasm_state column is nil and the record is new
|
96
|
+
# that the initial state gets populated before validation on create
|
97
|
+
#
|
98
|
+
# foo = Foo.new
|
99
|
+
# foo.aasm_state # => nil
|
100
|
+
# foo.valid?
|
101
|
+
# foo.aasm_state # => "open" (where :open is the initial state)
|
102
|
+
#
|
103
|
+
#
|
104
|
+
# foo = Foo.find(:first)
|
105
|
+
# foo.aasm_state # => 1
|
106
|
+
# foo.aasm_state = nil
|
107
|
+
# foo.valid?
|
108
|
+
# foo.aasm_state # => nil
|
109
|
+
#
|
110
|
+
def aasm_ensure_initial_state
|
111
|
+
aasm.enter_initial_state if send(self.class.aasm_column).blank?
|
112
|
+
end
|
113
|
+
end # InstanceMethods
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require "aasm/persistence/ohm_persistence"
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aasm_ohm_persistence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Phuong Nguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ohm-contrib
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
description: Persistence adapter to use AASM with Ohm
|
56
|
+
email:
|
57
|
+
- phuongnd08@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- aasm_ohm_persistence.gemspec
|
68
|
+
- lib/aasm/persistence/ohm_persistence.rb
|
69
|
+
- lib/aasm_ohm_persistence.rb
|
70
|
+
- lib/aasm_ohm_persistence/version.rb
|
71
|
+
homepage: http://github.com/phuongnd08/aasm_ohm_persistence
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.0.3
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Persistence adapter to use AASM with Ohm
|
95
|
+
test_files: []
|