status-manager 0.1.4 → 0.7.0

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: 63d931d22475e23678c82c510f8f704e8d1dbd06
4
+ data.tar.gz: 0920aa2e322e0f6a28b927cf347bf97fb33e0fd9
5
+ SHA512:
6
+ metadata.gz: 94db437599edf8e5cb75c92f54569256229840e53f4ceccdb0bbb37b8baf137d397be7c673f4949ff67ff63162e4c7780419b2dc83f82c7b6ff2b7c3f2e18282
7
+ data.tar.gz: ae62746a25cffa1a24c52f6dfc3833a85c4661c2a75cfc10eaaf8a21eddc63652c932956e92b572a9b3d8f0febb6d16e6626c0a5055251b5aba629d27b1cb584
data/README.md CHANGED
@@ -2,7 +2,7 @@ status-manager
2
2
  ==============
3
3
 
4
4
  ## Description
5
- Simple ActiveRecord Model Status Manager
5
+ ActiveRecord Model Status Manager, It provides easy ways managing model that have many statuses.
6
6
 
7
7
  ## Usage
8
8
 
@@ -20,36 +20,37 @@ class Product < ActiveRecord::Base
20
20
  attr_accessible :title, :sale_status
21
21
 
22
22
  # attr_as_status :status_attribute in model, {:status_value => 'status_value that is saved in database'}
23
- attr_as_status :sale_status, :onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'
24
- status_group :sale_status, :close => [:reject, :pending], :open => [:onsale, :soldout]
23
+ attr_as_status :sale_status, {:onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'}
24
+ status_group :sale_status, {:close => [:reject, :pending], :open => [:onsale, :soldout]}
25
25
 
26
26
  end
27
27
  ```
28
28
 
29
- ### Queries
29
+ #### Queries
30
30
  ```ruby
31
- ## select
31
+ ## status_list
32
32
  Product.sale_statuses #=> {:onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'}
33
33
 
34
- @onsale_product = Product.sale_status_onsale.first
34
+ ## use scope
35
+ @onsale_product = Product.sale_status_onsale.first # or Product.sale_status(:onsale).first
35
36
  @closed_product = Product.sale_status_close.first
36
37
 
37
38
  @onsale_product.sale_status_onsale? #=> true
38
39
  #or
39
40
  @closed_product.sale_status?(:close) #=> true
40
41
 
41
- ## update just attribute value
42
+ ## change attribute value
42
43
  @closed_product.sale_status_to(:onsale)
43
44
  #or
44
45
  @closed_product.sale_Status_to_onsale
45
46
 
46
- ## update with database
47
+ ## update value with database
47
48
  @closed_product.update_sale_status_to(:onsale)
48
49
  #or
49
50
  @closed_product.update_sale_status_to_onsale
50
51
  ```
51
52
 
52
- ### Callback
53
+ #### Callback
53
54
  ``` ruby
54
55
  class Product < ActiveRecord::Base
55
56
  attr_accessible :title, :sale_status
@@ -58,6 +59,7 @@ class Product < ActiveRecord::Base
58
59
  attr_as_status :sale_status, :onsale => 'onsale', :reject => 'reject', :pending => 'pending', :soldout => 'soldout'
59
60
  status_group :sale_status, :close => [:reject, :pending], :open => [:onsale, :soldout]
60
61
 
62
+ #callback update status from specific status
61
63
  before_status_update :sale_status, :onsale => :close do |product|
62
64
  puts "#{product.title} is closed"
63
65
  end
@@ -66,11 +68,13 @@ class Product < ActiveRecord::Base
66
68
  puts "closed #{product.title} is opened"
67
69
  # do something after update
68
70
  end
69
-
71
+
72
+ #callback update status
70
73
  after_status_update :sale_status, :reject do |prdocut|
71
74
  puts "#{product.title} is rejected"
72
75
  # do something after update
73
76
  end
74
77
  end
75
78
 
76
- ```
79
+ ```
80
+
@@ -1,6 +1,7 @@
1
1
  require 'status-manager/status_group_manager'
2
2
  require 'status-manager/status_update_callback'
3
3
  require 'status-manager/status_validation'
4
+ require 'status-manager/status_store'
4
5
  require 'active_support/inflector'
5
6
 
6
7
  module StatusManager
@@ -13,69 +14,77 @@ module StatusManager
13
14
  end
14
15
 
15
16
  module ClassMethods
16
- def attr_as_status (status_title, statuses={})
17
- manager_status_list[status_title] = statuses
17
+ def attr_as_status (status_attribute, status_sets={})
18
+ status_store = StatusStore.new(status_attribute, status_sets)
19
+ status_store_list.add(status_store)
20
+
21
+ scope "#{status_store.attribute_name}", lambda{ | status |
22
+ if status_store.status?(status)
23
+ where("#{self.table_name}.#{status_store.attribute_name.to_s}" => status_store_list.get(status_store.attribute_name).value(status))
24
+ elsif status_store.group_status?(status)
25
+ where("#{self.table_name}.#{status_store.attribute_name} in (?)", status_store.get_group_status_sets(status).values)
26
+ end
27
+ }
18
28
 
19
- statuses.each do |key, value|
29
+ status_store.status_sets.each do |key, value|
20
30
  #active_record scope setting
21
- scope "#{status_title}_#{key}", where("#{self.table_name}.#{status_title}" => value)
31
+ scope "#{status_store.attribute_name}_#{key}", where("#{self.table_name}.#{status_store.attribute_name}" => value)
22
32
 
23
33
  #status check method
24
- define_method "#{status_title}_#{key}?" do
25
- self.send("#{status_title}") == value
34
+ define_method "#{status_store.attribute_name}_#{key}?" do
35
+ self.send("#{status_store.attribute_name}") == value
26
36
  end
27
37
 
28
- define_method "#{status_title}_was_#{key}?" do
29
- self.send("#{status_title}_was") == value
38
+ define_method "#{status_store.attribute_name}_was_#{key}?" do
39
+ self.send("#{status_store.attribute_name}_was") == value
30
40
  end
31
41
 
32
42
  #update status
33
- define_method "update_#{status_title}_to_#{key}" do
34
- self.update_attributes("#{status_title}" => "#{value}")
43
+ define_method "update_#{status_store.attribute_name}_to_#{key}" do
44
+ self.update_attributes("#{status_store.attribute_name}" => "#{value}")
35
45
  end
36
46
 
37
- define_method "#{status_title}_to_#{key}" do
38
- self.send("#{status_title}=", value)
47
+ define_method "#{status_store.attribute_name}_to_#{key}" do
48
+ self.send("#{status_store.attribute_name}=", value)
39
49
  end
40
50
  end
41
51
 
42
52
  #status check method
43
- define_method "#{status_title}?" do |status|
44
- self.send("#{status_title}_#{status}?")
53
+ define_method "#{status_store.attribute_name}?" do |status|
54
+ self.send("#{status_store.attribute_name}_#{status}?")
45
55
  end
46
56
 
47
- define_method "#{status_title}_was?" do |status|
48
- self.send("#{status_title}_was_#{status}?")
57
+ define_method "#{status_store.attribute_name}_was?" do |status|
58
+ self.send("#{status_store.attribute_name}_was_#{status}?")
49
59
  end
50
60
 
51
61
  #status setter (do not override attr_accessible)
52
- define_method "#{status_title}_to" do |next_status|
53
- status_value = self.class.manager_status_list[status_title][next_status]
54
- self.send("#{status_title}=", status_value)
62
+ define_method "#{status_store.attribute_name}_to" do |status|
63
+ status_value = self.class.status_store_list.get(status_store.attribute_name).value(status)
64
+ self.send("#{status_store.attribute_name}=", status_value)
55
65
  end
56
66
 
57
67
  # update status
58
- define_method "update_#{status_title}_to" do |next_status|
59
- self.update_attributes(status_title.to_sym => self.class.manager_status_list[status_title][next_status])
68
+ define_method "update_#{status_store.attribute_name}_to" do |status|
69
+ self.update_attributes(status_attribute.to_sym => self.class.status_store_list.get(status_store.attribute_name).value(status))
60
70
  end
61
71
 
62
72
  #get status list
63
- define_singleton_method "#{status_title.to_s.pluralize}" do
64
- self.manager_status_list[status_title.to_sym]
73
+ define_singleton_method "#{status_store.attribute_name.to_s.pluralize}" do
74
+ self.class.status_store_list.get(status_attribute).status_sets
65
75
  end
66
76
  end
67
77
 
68
- def manager_status_list
69
- status_list = {}
70
- begin
71
- status_list = self.class_variable_get('@@manager_status_list')
72
- rescue NameError
73
- self.class_variable_set('@@manager_status_list', status_list)
78
+ def status_store_list
79
+ if self.class_variable_defined?(:@@status_store_list)
80
+ self.class_variable_get(:@@status_store_list)
81
+ else
82
+ self.class_variable_set(:@@status_store_list, StatusStoreList.new)
74
83
  end
75
- status_list
76
84
  end
77
85
 
86
+
78
87
  end
79
88
  end
80
89
 
81
- ActiveRecord::Base.send(:include, StatusManager) if defined? ActiveRecord
90
+ ActiveRecord::Base.send(:include, StatusManager) if defined? ActiveRecord
@@ -1,24 +1,23 @@
1
1
  module StatusManager
2
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
3
+ def status_group (status_attribute_name, group_status_set={})
4
+ status_store = self.status_store_list.get(status_attribute_name.to_sym)
5
+ raise "undefined #{status_attribute_name}" unless status_store
5
6
 
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
7
+ group_status_set.each do |group_status_name, group_statuses|
8
+ raise "#{status_attribute_name}-#{group_status_name} is not a group, group must have statuses" if group_statuses.size < 1
9
+ status_store.add_group_status(group_status_name, group_statuses)
11
10
 
12
11
  # set scope
13
- scope "#{status_title}_#{group_status_title}", where("#{self.table_name}.#{status_title} in (?)", group_status_values)
12
+ scope "#{status_store.attribute_name}_#{group_status_name}", where("#{self.table_name}.#{status_store.attribute_name} in (?)", status_store.get_group_status_sets(group_status_name).values)
14
13
 
15
14
  # status check method
16
- define_method "#{status_title}_#{group_status_title}?" do
17
- group_status_values.include? self.send(status_title)
15
+ define_method "#{status_attribute_name}_#{group_status_name}?" do
16
+ status_store.get_group_status_sets(group_status_name).values.include? self.send(status_attribute_name)
18
17
  end
19
18
 
20
- define_method "#{status_title}_was_#{group_status_title}?" do
21
- group_status_values.include? self.send("#{status_title}_was")
19
+ define_method "#{status_attribute_name}_was_#{group_status_name}?" do
20
+ status_store.get_group_status_sets(group_status_name).values.include? self.send("#{status_attribute_name}_was")
22
21
  end
23
22
  end
24
23
  end
@@ -0,0 +1,63 @@
1
+ module StatusManager
2
+ class StatusStoreList
3
+ attr_accessor :status_stores
4
+
5
+ def initialize
6
+ @status_stores = []
7
+ end
8
+
9
+ def add(status_store)
10
+ @status_stores << status_store
11
+ end
12
+
13
+ def get(attribute)
14
+ @status_stores.select {|status_store| status_store.attribute_name == attribute}.first
15
+ end
16
+ end
17
+
18
+ class StatusStore
19
+ attr_accessor :attribute_name, :status_sets, :group_statuses
20
+
21
+ def initialize(attribute_name, status_sets=nil )
22
+ @attribute_name = attribute_name
23
+ @status_sets = status_sets || {}
24
+ @group_statuses = {}
25
+ end
26
+
27
+ def status?(status)
28
+ @status_sets.key?(status)
29
+ end
30
+
31
+ def group_status?(group_status)
32
+ @group_statuses.key?(group_status)
33
+ end
34
+
35
+ def statuses
36
+ @status_sets.keys
37
+ end
38
+
39
+ def values
40
+ @status_sets.values
41
+ end
42
+
43
+ def value(status)
44
+ @status_sets[status]
45
+ end
46
+
47
+ def status(value)
48
+ @status_sets.select{ |status, _value| _value == value}.first[0]
49
+ end
50
+
51
+ def add_group_status(group_status_name, statuses)
52
+ @group_statuses.merge!({group_status_name => statuses})
53
+ end
54
+
55
+ def get_group_status_sets(group_status_name)
56
+ statuses = {}
57
+ @group_statuses[group_status_name].each do |status|
58
+ statuses[status] = self.value(status)
59
+ end
60
+ statuses
61
+ end
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module StatusManager
2
- VERSION = '0.1.4'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'status-manager'
3
- s.version = '0.1.4'
4
- s.date = '2013-08-17'
3
+ s.version = '0.7.0'
4
+ s.date = '2013-12-20'
5
5
  s.summary = "ActiveRecord Model Status Manager"
6
- s.description = "ActiveRecord Model Status Manager"
6
+ s.description = "ActiveRecord Model Status Manager, It provides easy ways managing model that have many statuses."
7
7
  s.authors = ["Keepcosmos"]
8
8
  s.email = 'keepcosmos@gmail.com'
9
9
  s.licenses = 'MIT'
Binary file
@@ -3,9 +3,11 @@ require 'models/product'
3
3
 
4
4
  class StatusManagerTest < Test::Unit::TestCase
5
5
 
6
- def test_status_chagne
7
- product = Product.sale_status_pending.first
6
+ def test_status_change
7
+ product = Product.sale_status(:pending).first
8
+ assert product.sale_status?(:pending)
8
9
  product.update_sale_status_to_onsale
10
+ assert product.sale_status?(:onsale)
9
11
  end
10
12
 
11
13
  def test_current_status
@@ -38,6 +40,8 @@ class StatusManagerTest < Test::Unit::TestCase
38
40
 
39
41
  def test_group_status
40
42
  closed_products = Product.sale_status_close
43
+ closed_products = Product.sale_status(:close)
44
+ assert closed_products == _closed_products
41
45
  closed_products.each do |product|
42
46
  assert product.sale_status_close?
43
47
  assert product.sale_status? :close
@@ -50,5 +54,4 @@ class StatusManagerTest < Test::Unit::TestCase
50
54
  end
51
55
  end
52
56
 
53
-
54
57
  end
metadata CHANGED
@@ -1,89 +1,67 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: status-manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
5
- prerelease:
4
+ version: 0.7.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Keepcosmos
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-17 00:00:00.000000000 Z
11
+ date: 2013-12-20 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: ActiveRecord Model Status Manager
13
+ description: ActiveRecord Model Status Manager, It provides easy ways managing model
14
+ that have many statuses.
15
15
  email: keepcosmos@gmail.com
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - !binary |-
21
- UkVBRE1FLm1k
22
- - !binary |-
23
- UmFrZWZpbGU=
24
- - !binary |-
25
- bGliL3N0YXR1cy1tYW5hZ2VyLnJi
26
- - !binary |-
27
- bGliL3N0YXR1cy1tYW5hZ2VyL3N0YXR1c19ncm91cF9tYW5hZ2VyLnJi
28
- - !binary |-
29
- bGliL3N0YXR1cy1tYW5hZ2VyL3N0YXR1c191cGRhdGVfY2FsbGJhY2sucmI=
30
- - !binary |-
31
- bGliL3N0YXR1cy1tYW5hZ2VyL3N0YXR1c192YWxpZGF0aW9uLnJi
32
- - !binary |-
33
- bGliL3N0YXR1cy1tYW5hZ2VyL3ZlcnNpb24ucmI=
34
- - !binary |-
35
- c3RhdHVzLW1hbmFnZXItMC4xLjMuZ2Vt
36
- - !binary |-
37
- c3RhdHVzLW1hbmFnZXIuZ2Vtc3BlYw==
38
- - !binary |-
39
- c3RhdHVzX21hbmFnZXJfdGVzdC5zcWxpdGUz
40
- - !binary |-
41
- dGVzdC9jb25maWcvZGF0YWJhc2UueW1s
42
- - !binary |-
43
- dGVzdC9jb25maWcvc2NoZW1hLnJi
44
- - !binary |-
45
- dGVzdC9maXh0dXJlcy9wcm9kdWN0cy55bWw=
46
- - !binary |-
47
- dGVzdC9tb2RlbHMvcHJvZHVjdC5yYg==
48
- - !binary |-
49
- dGVzdC9zdGF0dXMtbWFuYWdlcl90ZXN0LnJi
50
- - !binary |-
51
- dGVzdC90ZXN0X2luaXRpYWxpemVyLnJi
20
+ - README.md
21
+ - Rakefile
22
+ - lib/status-manager.rb
23
+ - lib/status-manager/status_group_manager.rb
24
+ - lib/status-manager/status_store.rb
25
+ - lib/status-manager/status_update_callback.rb
26
+ - lib/status-manager/status_validation.rb
27
+ - lib/status-manager/version.rb
28
+ - status-manager-0.1.3.gem
29
+ - status-manager.gemspec
30
+ - status_manager_test.sqlite3
31
+ - test/config/database.yml
32
+ - test/config/schema.rb
33
+ - test/fixtures/products.yml
34
+ - test/models/product.rb
35
+ - test/status-manager_test.rb
36
+ - test/test_initializer.rb
52
37
  homepage: https://github.com/keepcosmos/status-manager
53
38
  licenses:
54
39
  - MIT
40
+ metadata: {}
55
41
  post_install_message:
56
42
  rdoc_options: []
57
43
  require_paths:
58
44
  - lib
59
45
  required_ruby_version: !ruby/object:Gem::Requirement
60
- none: false
61
46
  requirements:
62
- - - ! '>='
47
+ - - '>='
63
48
  - !ruby/object:Gem::Version
64
49
  version: '0'
65
50
  required_rubygems_version: !ruby/object:Gem::Requirement
66
- none: false
67
51
  requirements:
68
- - - ! '>='
52
+ - - '>='
69
53
  - !ruby/object:Gem::Version
70
54
  version: '0'
71
55
  requirements: []
72
56
  rubyforge_project:
73
- rubygems_version: 1.8.25
57
+ rubygems_version: 2.1.2
74
58
  signing_key:
75
- specification_version: 3
59
+ specification_version: 4
76
60
  summary: ActiveRecord Model Status Manager
77
61
  test_files:
78
- - !binary |-
79
- dGVzdC9jb25maWcvZGF0YWJhc2UueW1s
80
- - !binary |-
81
- dGVzdC9jb25maWcvc2NoZW1hLnJi
82
- - !binary |-
83
- dGVzdC9maXh0dXJlcy9wcm9kdWN0cy55bWw=
84
- - !binary |-
85
- dGVzdC9tb2RlbHMvcHJvZHVjdC5yYg==
86
- - !binary |-
87
- dGVzdC9zdGF0dXMtbWFuYWdlcl90ZXN0LnJi
88
- - !binary |-
89
- dGVzdC90ZXN0X2luaXRpYWxpemVyLnJi
62
+ - test/config/database.yml
63
+ - test/config/schema.rb
64
+ - test/fixtures/products.yml
65
+ - test/models/product.rb
66
+ - test/status-manager_test.rb
67
+ - test/test_initializer.rb