status-manager 0.1.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.
- data/README.md +4 -0
- data/Rakefile +13 -0
- data/lib/status-manager/version.rb +3 -0
- data/lib/status-manager.rb +57 -0
- data/status-manager.gemspec +15 -0
- data/status_manager_test.sqlite3 +0 -0
- data/test/config/database.yml +4 -0
- data/test/config/schema.rb +6 -0
- data/test/fixtures/products.yml +24 -0
- data/test/models/product.rb +8 -0
- data/test/status-manager_test.rb +33 -0
- data/test/test_initializer.rb +15 -0
- metadata +80 -0
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
desc "Default: run unit tests."
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc "Test Status Manager"
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << %w[lib test]
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module StatusManager
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def acts_as_status (status_title, status={})
|
9
|
+
@@status_manager_status_list ||= {}
|
10
|
+
raise "#{status_title} variable is already exist" if @@status_manager_status_list.key? status_title.to_sym
|
11
|
+
@@status_manager_status_list[status_title.to_sym] = {}
|
12
|
+
|
13
|
+
status.each do |key, value|
|
14
|
+
#set status
|
15
|
+
@@status_manager_status_list[status_title.to_sym][key.to_sym] = value
|
16
|
+
|
17
|
+
#scope setting
|
18
|
+
scope "#{status_title}_#{key}", where("#{self.table_name}.#{status_title}" => value)
|
19
|
+
|
20
|
+
#true / false
|
21
|
+
define_method "#{status_title}_#{key}?" do
|
22
|
+
eval("self.#{status_title} == '#{value}'")
|
23
|
+
end
|
24
|
+
|
25
|
+
define_method "#{status_title}_to" do |will_status|
|
26
|
+
eval("self.#{status_title} = @@status_manager_status_list[:#{status_title}][will_status]")
|
27
|
+
end
|
28
|
+
|
29
|
+
define_method "update_#{status_title}_#{key}" do
|
30
|
+
self.update_attributes(status_title.to_sym => value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
define_method "update_#{status_title}" do |will_status|
|
35
|
+
self.update_attributes(status_title.to_sym => status[will_status.to_sym])
|
36
|
+
end
|
37
|
+
|
38
|
+
define_singleton_method "#{status_title}_group" do |group_title, status_list|
|
39
|
+
all_status = @@status_manager_status_list[status_title.to_sym]
|
40
|
+
group_status_values = []
|
41
|
+
|
42
|
+
status_list.each do |key|
|
43
|
+
group_status_values << all_status[key.to_sym]
|
44
|
+
raise "#{group_title} : #{key} is not a #{status_title} member" unless all_status.include? key
|
45
|
+
end
|
46
|
+
|
47
|
+
scope "#{status_title}_#{group_title}", where("#{self.table_name}.#{status_title} in (:status)", :status => group_status_values)
|
48
|
+
|
49
|
+
define_method "#{status_title}_#{group_title}?" do
|
50
|
+
group_status_values.include? self.send(status_title.to_sym)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ActiveRecord::Base.send(:include, StatusManager) if defined? ActiveRecord
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'status-manager'
|
3
|
+
s.version = '0.1.1'
|
4
|
+
s.date = '2013-07-15'
|
5
|
+
s.summary = "ActiveRecord Model Status Manager"
|
6
|
+
s.description = "ActiveRecord Model Status Manager"
|
7
|
+
s.authors = ["Keepcosmos"]
|
8
|
+
s.email = 'keepcosmos@gmail.com'
|
9
|
+
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
|
14
|
+
s.homepage = 'https://github.com/keepcosmos/status-manager'
|
15
|
+
end
|
Binary file
|
@@ -0,0 +1,24 @@
|
|
1
|
+
first_product:
|
2
|
+
id: 1
|
3
|
+
title: beer
|
4
|
+
status: 'onsale'
|
5
|
+
second_product:
|
6
|
+
id: 2
|
7
|
+
title: 'ipad'
|
8
|
+
status: 'onsale'
|
9
|
+
third_product:
|
10
|
+
id: 3
|
11
|
+
title: 'iphone'
|
12
|
+
status: 'pending'
|
13
|
+
fourth_product:
|
14
|
+
id: 4
|
15
|
+
title: 'gem'
|
16
|
+
status: 'reject'
|
17
|
+
fifth_product:
|
18
|
+
id: 5
|
19
|
+
title: 'ruby'
|
20
|
+
status: 'soldout'
|
21
|
+
sixth_product:
|
22
|
+
id: 7
|
23
|
+
title: 'book'
|
24
|
+
status: 'soldout'
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class Product < ActiveRecord::Base
|
2
|
+
attr_accessible :title, :status
|
3
|
+
|
4
|
+
# acts_as_status :status_attribute in model, {:status_value => 'status_value that is saved in database'}
|
5
|
+
acts_as_status :status, :onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'
|
6
|
+
status_group :close, [:reject, :pending]
|
7
|
+
|
8
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_initializer'
|
2
|
+
require 'models/product'
|
3
|
+
|
4
|
+
class StatusManagerTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_current_status
|
7
|
+
product = Product.first
|
8
|
+
assert_equal true, product.status_onsale?
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_status_scope
|
12
|
+
products = Product.status_reject
|
13
|
+
products.each do |product|
|
14
|
+
assert product.status_reject?, "#{product.id} is not rejected product"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_status_update
|
19
|
+
product = Product.status_reject.first
|
20
|
+
product.status_to(:soldout)
|
21
|
+
assert product.status_soldout?
|
22
|
+
assert product.update_status_onsale
|
23
|
+
assert product.status_onsale?
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_status_group_scope
|
27
|
+
products = Product.status_close
|
28
|
+
products.each do |product|
|
29
|
+
assert product.status_close?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../..')
|
2
|
+
$:.unshift(File.dirname(__FILE__) + '/../../lib')
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'active_record'
|
7
|
+
require 'active_record/fixtures'
|
8
|
+
|
9
|
+
require 'status-manager'
|
10
|
+
|
11
|
+
ActiveRecord::Base.configurations = YAML::load_file("#{File.dirname(__FILE__)}/config/database.yml")['test']
|
12
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations)
|
13
|
+
load("#{File.dirname(__FILE__)}/config/schema.rb")
|
14
|
+
|
15
|
+
ActiveRecord::Fixtures.create_fixtures 'test/fixtures', 'products'
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: status-manager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Keepcosmos
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-15 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ActiveRecord Model Status Manager
|
15
|
+
email: keepcosmos@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- !binary |-
|
21
|
+
UkVBRE1FLm1k
|
22
|
+
- !binary |-
|
23
|
+
UmFrZWZpbGU=
|
24
|
+
- !binary |-
|
25
|
+
bGliL3N0YXR1cy1tYW5hZ2VyLnJi
|
26
|
+
- !binary |-
|
27
|
+
bGliL3N0YXR1cy1tYW5hZ2VyL3ZlcnNpb24ucmI=
|
28
|
+
- !binary |-
|
29
|
+
c3RhdHVzLW1hbmFnZXIuZ2Vtc3BlYw==
|
30
|
+
- !binary |-
|
31
|
+
c3RhdHVzX21hbmFnZXJfdGVzdC5zcWxpdGUz
|
32
|
+
- !binary |-
|
33
|
+
dGVzdC9jb25maWcvZGF0YWJhc2UueW1s
|
34
|
+
- !binary |-
|
35
|
+
dGVzdC9jb25maWcvc2NoZW1hLnJi
|
36
|
+
- !binary |-
|
37
|
+
dGVzdC9maXh0dXJlcy9wcm9kdWN0cy55bWw=
|
38
|
+
- !binary |-
|
39
|
+
dGVzdC9tb2RlbHMvcHJvZHVjdC5yYg==
|
40
|
+
- !binary |-
|
41
|
+
dGVzdC9zdGF0dXMtbWFuYWdlcl90ZXN0LnJi
|
42
|
+
- !binary |-
|
43
|
+
dGVzdC90ZXN0X2luaXRpYWxpemVyLnJi
|
44
|
+
homepage: https://github.com/keepcosmos/status-manager
|
45
|
+
licenses: []
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.25
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: ActiveRecord Model Status Manager
|
68
|
+
test_files:
|
69
|
+
- !binary |-
|
70
|
+
dGVzdC9jb25maWcvZGF0YWJhc2UueW1s
|
71
|
+
- !binary |-
|
72
|
+
dGVzdC9jb25maWcvc2NoZW1hLnJi
|
73
|
+
- !binary |-
|
74
|
+
dGVzdC9maXh0dXJlcy9wcm9kdWN0cy55bWw=
|
75
|
+
- !binary |-
|
76
|
+
dGVzdC9tb2RlbHMvcHJvZHVjdC5yYg==
|
77
|
+
- !binary |-
|
78
|
+
dGVzdC9zdGF0dXMtbWFuYWdlcl90ZXN0LnJi
|
79
|
+
- !binary |-
|
80
|
+
dGVzdC90ZXN0X2luaXRpYWxpemVyLnJi
|