state_store 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # State store
2
+
3
+ ## Installation
4
+
5
+ `gem install state_store`
6
+
7
+ `require 'state_store'`
8
+
9
+ For Bundler add this in you Gemfile
10
+
11
+ `gem state_store, '~>0.0.2'`
12
+
13
+ And then run
14
+
15
+ `bundle install`
16
+
17
+ ## Usage
18
+
19
+ State store all to keep different states in one attribute.
20
+ It provide module for mixin, it is pure ruby so it is possible to use it in any ruby class.
21
+
22
+ ```ruby
23
+ class MyFile
24
+ include StateStore
25
+ attr_accessor :permission
26
+
27
+ has_states :read,:write,:execute, :in => :permission
28
+ end
29
+
30
+ file = MyFile.new
31
+ file.states = [:read,:write]
32
+ file.has_permission?(:read) #=> true
33
+ file.has_permission?(:execute) #=> false
34
+ file.permission #=> 5
35
+ file.states #=> [:read,:write]
36
+ ```
37
+
38
+ It is possible to configure behaviour for #has_states
39
+
40
+ * **:in** is method name where states are stored in numeric format
41
+ * **:as** allow to change method for states (default is :states). This is useful if you have more than one state attribute per class.
42
+
43
+ ### Adding new states
44
+
45
+ When you need to add new states and you keep want to previous states configurations be valid, then you should add new state as first in queue.
46
+
47
+ ```ruby
48
+ class Wolf
49
+ include StateStore
50
+ has_states :big,:bad, :in => :characteristic, :as => :nature
51
+ end
52
+
53
+ wolf = Wold.new()
54
+ wolf.nature = [:bad]
55
+ wolf.characteristic #=> 1
56
+
57
+ ```
58
+
59
+ Then if you decide to add _hungry_ then you change states as shown below and that will keep previous configuration valid as well as new one.
60
+
61
+ ```ruby
62
+ class Wolf
63
+ include StateStore
64
+ has_states :hungry, :big, :bad, :in => :characteristic, :as => :nature
65
+ end
66
+
67
+ wolf = Wolf.new()
68
+ wolf.nature = [:bad]
69
+ wolf.characteristic #=> 1
70
+ wolf.nature = [:hungry,:bad]
71
+ wolf.nature << :bad
72
+ wolf.has_charasteristic?(:bad) #=> true
73
+ wolf.characteristic #=> 5
74
+ ```
75
+
76
+
77
+ ## Copyright
78
+
79
+ Copyright (c) 2012 Arturs Meisters. See LICENSE.txt for
80
+ further details.
81
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -4,19 +4,20 @@ module StateStore
4
4
  module ClassMethods
5
5
 
6
6
  def has_states *states
7
- state_store_options = states && states.last.is_a?(Hash) && states.pop || {}
7
+ states_stores_options = states && states.last.is_a?(Hash) && states.pop || {}
8
8
  raise ArgumentError.new("No states given") if states.empty?
9
- raise ArgumentError.new(":in is required") unless state_store_options[:in]
9
+ raise ArgumentError.new(":in is required") unless states_stores_options[:in]
10
10
 
11
- state_store_options[:as] ||= :states
11
+ states_stores_options[:as] ||= :states
12
12
  store = StateStore::BinaryStore.new(states)
13
13
 
14
14
  @states_stores ||={}
15
15
  @states_stores_options ||={}
16
+ validate_state_store(states_stores_options)
16
17
 
17
- @states_stores[state_store_options[:as]] = store
18
- @states_stores_options[state_store_options[:as]] = state_store_options
19
- create_methods_for_state_store(state_store_options[:as])
18
+ @states_stores[states_stores_options[:as]] = store
19
+ @states_stores_options[states_stores_options[:as]] = states_stores_options
20
+ create_methods_for_state_store(states_stores_options[:as])
20
21
  end
21
22
 
22
23
  def states_stores
@@ -29,6 +30,15 @@ module StateStore
29
30
 
30
31
  private
31
32
 
33
+ def validate_state_store(options)
34
+ raise ArgumentError.new("Scope '#{options[:as]}' already exists") if states_stores_options.keys.include?(options[:as])
35
+ states_stores_options.each do |scope,conf_options|
36
+ if conf_options[:in].to_sym == options[:in].to_sym
37
+ raise ArgumentError.new("Scope '#{scope}' already store configuration in '#{conf_options[:in]}'")
38
+ end
39
+ end
40
+ end
41
+
32
42
  def create_methods_for_state_store(name)
33
43
 
34
44
  self.class_eval do
@@ -41,6 +51,7 @@ module StateStore
41
51
  define_method :"#{name}=" do |humanized_array|
42
52
  method_name = self.class.states_stores_options[name][:in]
43
53
  store = self.class.states_stores[name]
54
+ humanized_array = [humanized_array] unless humanized_array.is_a?(Array)
44
55
  self.send(:"#{method_name}=",store.value(humanized_array))
45
56
  end
46
57
  end
@@ -26,6 +26,27 @@ describe StateStore::Extension do
26
26
  }.to raise_error(ArgumentError,":in is required")
27
27
  end
28
28
 
29
+ it "should create scope in class for each state store by default with name :states" do
30
+ klass.has_states :read,:write, :in => :status
31
+ klass.states_stores.keys.should eq([:states])
32
+ klass.has_states :green,:round, :in => :other_status, :as => :properties
33
+ klass.states_stores.keys.sort.should eq([:properties,:states])
34
+ end
35
+
36
+ it "should validate if there is scope with same name" do
37
+ klass.has_states :read,:write, :in => :status
38
+ expect{
39
+ klass.has_states :green, :round, :in => :other_status
40
+ }.to raise_error(ArgumentError, "Scope 'states' already exists")
41
+ end
42
+
43
+ it "should validate if there is scope with same storage attribute" do
44
+ klass.has_states :read,:write, :in => :status
45
+ expect{
46
+ klass.has_states :green, :round, :in => :status, :as => :properties
47
+ }.to raise_error(ArgumentError, "Scope 'states' already store configuration in 'status'")
48
+ end
49
+
29
50
  it "should create store with given statuses" do
30
51
  klass.has_states :read,:write, :in => :status
31
52
  klass.states_stores[:states].statuses.should eq([:read,:write])
data/state_store.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "state_store"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Arturs Meisters"]
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.email = "arturs.meisters@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
- "README.rdoc"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  "Gemfile",
23
23
  "Gemfile.lock",
24
24
  "LICENSE.txt",
25
- "README.rdoc",
25
+ "README.md",
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "lib/state_store.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
16
- requirement: &19920720 !ruby/object:Gem::Requirement
16
+ requirement: &17268180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.12'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *19920720
24
+ version_requirements: *17268180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &19919080 !ruby/object:Gem::Requirement
27
+ requirement: &17266720 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *19919080
35
+ version_requirements: *17266720
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &19936760 !ruby/object:Gem::Requirement
38
+ requirement: &17284400 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,21 +43,21 @@ dependencies:
43
43
  version: 1.8.3
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *19936760
46
+ version_requirements: *17284400
47
47
  description: Create numeric value to Array of states.
48
48
  email: arturs.meisters@gmail.com
49
49
  executables: []
50
50
  extensions: []
51
51
  extra_rdoc_files:
52
52
  - LICENSE.txt
53
- - README.rdoc
53
+ - README.md
54
54
  files:
55
55
  - .document
56
56
  - .rspec
57
57
  - Gemfile
58
58
  - Gemfile.lock
59
59
  - LICENSE.txt
60
- - README.rdoc
60
+ - README.md
61
61
  - Rakefile
62
62
  - VERSION
63
63
  - lib/state_store.rb
@@ -83,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  segments:
85
85
  - 0
86
- hash: -2977312246934280863
86
+ hash: -4107345820124143679
87
87
  required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  none: false
89
89
  requirements:
data/README.rdoc DELETED
@@ -1,19 +0,0 @@
1
- = state_store
2
-
3
- Description goes here.
4
-
5
- == Contributing to state_store
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
- * Fork the project.
10
- * Start a feature/bugfix branch.
11
- * Commit and push until you are happy with your contribution.
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2012 Arturs Meisters. See LICENSE.txt for
18
- further details.
19
-