status-manager 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -16,23 +16,32 @@ gem 'status-manager'
16
16
 
17
17
  ```ruby
18
18
  class Product < ActiveRecord::Base
19
- attr_accessible :title, :status
19
+ attr_accessible :title, :my_status
20
20
 
21
- # acts_as_status :status_attribute in model, {:status_value => 'status_value that is saved in database'}
22
- acts_as_status :status, :onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'
23
- status_group :close, [:reject, :pending]
21
+ # attr_as_status :status_attribute in model, {:status_value => 'status_value that is saved in database'}
22
+ attr_as_status :my_status, :onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'
23
+ status_group :my_status, :close => [:reject, :pending], :open => [:onsale, :soldout]
24
24
 
25
25
  end
26
26
  ```
27
27
 
28
28
  ```ruby
29
- # select
30
- @onsale_products = Product.status_onsale
31
- @closed_products = Product.status_close
29
+ ## select
30
+ @onsale_product = Product.my_status_onsale.first
31
+ @closed_product = Product.my_status_close.first
32
+
33
+ @onsale_product.my_status_onsale? #=> true
34
+ #or
35
+ @closed_product.my_status?(:close) #=> true
36
+
37
+ ## update just attribute value
38
+ @closed_product.my_status_to(:onsale)
39
+ #or
40
+ @closed_product.my_Status_to_onsale
41
+
42
+ ## update with database
43
+ @closed_product.update_my_status(:onsale)
44
+ #or
45
+ @closed_product.update_my_status_onsale
32
46
 
33
- assert @onsale_products.first.status_onsale?
34
- assert @closed_products.first.status_close?
35
47
 
36
- # update
37
- @closed_products.first.status_to(:onsale) ## => update just attribute value
38
- @closed_products.first.update_status_onsale ## => update with database
@@ -0,0 +1,22 @@
1
+ module StatusManager
2
+ module StatusGroupManager
3
+ def status_group (status_title, group_status_list={})
4
+ raise "undefined #{status_title}" unless self.manager_status_list.key? status_title.to_sym
5
+
6
+ group_status_list.each do |group_status_title, group_statuses|
7
+ group_status_values = []
8
+ group_statuses.each do |status|
9
+ group_status_values << self.manager_status_list[status_title][status]
10
+ end
11
+
12
+ # set scope
13
+ scope "#{status_title}_#{group_status_title}", where("#{self.table_name}.#{status_title} in (?)", group_status_values)
14
+
15
+ # status check method
16
+ define_method "#{status_title}_#{group_status_title}?" do
17
+ group_status_values.include? self.send(status_title.to_sym)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module StatusManager
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -1,3 +1,5 @@
1
+ require 'status-manager/status-group-manager'
2
+
1
3
  module StatusManager
2
4
 
3
5
  def self.included(base)
@@ -5,51 +7,55 @@ module StatusManager
5
7
  end
6
8
 
7
9
  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
10
+ def attr_as_status (status_title, statuses={})
11
+ manager_status_list[status_title] = statuses
16
12
 
17
- #scope setting
13
+ statuses.each do |key, value|
14
+ #active_record scope setting
18
15
  scope "#{status_title}_#{key}", where("#{self.table_name}.#{status_title}" => value)
19
16
 
20
- #true / false
21
- define_method "#{status_title}_#{key}?" do
22
- eval("self.#{status_title} == '#{value}'")
17
+ #status check method
18
+ define_method "#{status_title}_#{key}?" do
19
+ self.send("#{status_title}") == value
23
20
  end
24
21
 
25
- define_method "#{status_title}_to" do |will_status|
26
- eval("self.#{status_title} = @@status_manager_status_list[:#{status_title}][will_status]")
27
- end
22
+ #update status
23
+ define_method "update_#{status_title}_to_#{key}" do
24
+ self.update_attributes("#{status_title}" => "#{value}")
25
+ end
28
26
 
29
- define_method "update_#{status_title}_#{key}" do
30
- self.update_attributes(status_title.to_sym => value)
27
+ define_method "#{status_title}_to_#{key}" do
28
+ self.send("#{status_title}=", value)
31
29
  end
32
30
  end
33
31
 
34
- define_method "update_#{status_title}" do |will_status|
35
- self.update_attributes(status_title.to_sym => status[will_status.to_sym])
32
+ #status check method
33
+ define_method "#{status_title}?" do |status|
34
+ self.send("#{status_title}_#{status}?")
36
35
  end
37
36
 
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 = []
37
+ #status setter (do not override attr_accessible)
38
+ define_method "#{status_title}_to" do |next_status|
39
+ status_value = self.class.manager_status_list[status_title][next_status]
40
+ self.send("#{status_title}=", status_value)
41
+ end
41
42
 
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
43
+ # update status
44
+ define_method "update_#{status_title}" do |next_status|
45
+ self.update_attributes(status_title.to_sym => self.class.manager_status_list[status_title][next_status])
46
+ end
46
47
 
47
- scope "#{status_title}_#{group_title}", where("#{self.table_name}.#{status_title} in (:status)", :status => group_status_values)
48
+ extend StatusManager::StatusGroupManager
49
+ end
48
50
 
49
- define_method "#{status_title}_#{group_title}?" do
50
- group_status_values.include? self.send(status_title.to_sym)
51
- end
51
+ def manager_status_list
52
+ status_list = {}
53
+ begin
54
+ status_list = self.class_variable_get('@@manager_status_list')
55
+ rescue NameError
56
+ self.class_variable_set('@@manager_status_list', status_list)
52
57
  end
58
+ status_list
53
59
  end
54
60
  end
55
61
  end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'status-manager'
3
- s.version = '0.1.2'
4
- s.date = '2013-07-15'
3
+ s.version = '0.1.3'
4
+ s.date = '2013-08-16'
5
5
  s.summary = "ActiveRecord Model Status Manager"
6
6
  s.description = "ActiveRecord Model Status Manager"
7
7
  s.authors = ["Keepcosmos"]
Binary file
@@ -1,6 +1,6 @@
1
1
  ActiveRecord::Schema.define(:version => 0) do
2
2
  create_table :products, :force => true do |t|
3
3
  t.column :title, :string
4
- t.column :status, :string
4
+ t.column :sale_status, :string
5
5
  end
6
6
  end
@@ -1,24 +1,24 @@
1
1
  first_product:
2
2
  id: 1
3
3
  title: beer
4
- status: 'onsale'
4
+ sale_status: 'onsale'
5
5
  second_product:
6
6
  id: 2
7
7
  title: 'ipad'
8
- status: 'onsale'
8
+ sale_status: 'onsale'
9
9
  third_product:
10
10
  id: 3
11
11
  title: 'iphone'
12
- status: 'pending'
12
+ sale_status: 'pending'
13
13
  fourth_product:
14
14
  id: 4
15
15
  title: 'gem'
16
- status: 'reject'
16
+ sale_status: 'reject'
17
17
  fifth_product:
18
18
  id: 5
19
19
  title: 'ruby'
20
- status: 'soldout'
20
+ sale_status: 'soldout'
21
21
  sixth_product:
22
22
  id: 7
23
23
  title: 'book'
24
- status: 'soldout'
24
+ sale_status: 'soldout'
@@ -1,8 +1,9 @@
1
1
  class Product < ActiveRecord::Base
2
- attr_accessible :title, :status
2
+ attr_accessible :title, :sale_status
3
3
 
4
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]
5
+ attr_as_status :sale_status, :onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'
6
+ status_group :sale_status, :close => [:reject, :pending], :open => [:onsale, :soldout]
7
+
7
8
 
8
9
  end
@@ -4,31 +4,67 @@ require 'models/product'
4
4
  class StatusManagerTest < Test::Unit::TestCase
5
5
 
6
6
  def test_current_status
7
- product = Product.first
8
- puts product.status
9
- assert_equal true, product.status_onsale?
10
- end
11
-
12
- def test_status_scope
13
- products = Product.status_reject
7
+ products = Product.sale_status_onsale
14
8
  products.each do |product|
15
- assert product.status_reject?, "#{product.id} is not rejected product"
9
+ assert product.sale_status_onsale?
10
+ assert product.sale_status? :onsale
11
+ end
12
+
13
+ rejected_products = Product.sale_status_reject
14
+ rejected_products.each do |product|
15
+ assert product.sale_status_reject?
16
+ assert product.sale_status? :reject
16
17
  end
17
18
  end
18
19
 
19
20
  def test_status_update
20
- product = Product.status_reject.first
21
- product.status_to(:soldout)
22
- assert product.status_soldout?
23
- assert product.update_status_onsale
24
- assert product.status_onsale?
21
+ product = Product.sale_status_onsale.first
22
+ product.sale_status_to :reject
23
+ product.sale_status_to_reject
24
+ assert product.sale_status_reject?
25
+ assert product.sale_status? :reject
26
+
27
+ product.update_sale_status :soldout
28
+ assert Product.find(product.id).sale_status_soldout?
29
+
30
+ product.update_sale_status_to_onsale
31
+ assert Product.find(product.id).sale_status_onsale?
25
32
  end
26
33
 
27
- def test_status_group_scope
28
- products = Product.status_close
29
- products.each do |product|
30
- assert product.status_close?
34
+ def test_group_status
35
+ closed_products = Product.sale_status_close
36
+ closed_products.each do |product|
37
+ assert product.sale_status_close?
38
+ assert product.sale_status? :close
39
+ end
40
+
41
+ opened_products = Product.sale_status_open
42
+ opened_products.each do |product|
43
+ assert product.sale_status_open?
44
+ assert product.sale_status? :open
31
45
  end
32
46
  end
33
47
 
48
+ # def test_current_status
49
+ # product = Product.first
50
+ # assert_equal true, product.sale_status_onsale?
51
+ # end
52
+
53
+ # def test_status_scope
54
+ # products = Product.sale_status_reject
55
+ # products.each do |product|
56
+ # assert product.sale_status_reject?, "#{product.id} is not rejected product"
57
+ # end
58
+ # end
59
+
60
+ # def test_status_update
61
+ # product = Product.sale_status_reject.first
62
+ # product.sale_status_to(:soldout)
63
+ # assert product.sale_status_soldout?
64
+ # assert product.update_status_to_onsale
65
+ # assert product.sale_status_onsale?
66
+ # product.update_status(:soldout)
67
+ # assert product.sale_status_soldout?
68
+ # end
69
+
34
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: status-manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-15 00:00:00.000000000 Z
12
+ date: 2013-08-16 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ActiveRecord Model Status Manager
15
15
  email: keepcosmos@gmail.com
@@ -24,9 +24,9 @@ files:
24
24
  - !binary |-
25
25
  bGliL3N0YXR1cy1tYW5hZ2VyLnJi
26
26
  - !binary |-
27
- bGliL3N0YXR1cy1tYW5hZ2VyL3ZlcnNpb24ucmI=
27
+ bGliL3N0YXR1cy1tYW5hZ2VyL3N0YXR1cy1ncm91cC1tYW5hZ2VyLnJi
28
28
  - !binary |-
29
- c3RhdHVzLW1hbmFnZXItMC4xLjEuZ2Vt
29
+ bGliL3N0YXR1cy1tYW5hZ2VyL3ZlcnNpb24ucmI=
30
30
  - !binary |-
31
31
  c3RhdHVzLW1hbmFnZXIuZ2Vtc3BlYw==
32
32
  - !binary |-
Binary file