state_store 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/state_store/binary_store.rb +15 -0
- data/lib/state_store/extension.rb +42 -23
- data/spec/binary_store_spec.rb +12 -0
- data/spec/extension_spec.rb +21 -14
- metadata +8 -8
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
@@ -14,6 +14,11 @@ module StateStore
|
|
14
14
|
value_to_statuses(value)
|
15
15
|
end
|
16
16
|
|
17
|
+
def value(humanized_array)
|
18
|
+
raise ArgumentError.new("Out of range") if self.states < humanized_array.size
|
19
|
+
statuses_to_values(humanized_array)
|
20
|
+
end
|
21
|
+
|
17
22
|
def has_status?(symbol,value)
|
18
23
|
human_array = humanize(value)
|
19
24
|
human_array.include?(symbol)
|
@@ -23,6 +28,10 @@ module StateStore
|
|
23
28
|
statuses[index] if state.to_s == "1"
|
24
29
|
end
|
25
30
|
|
31
|
+
def index_by_state(state)
|
32
|
+
statuses[state]
|
33
|
+
end
|
34
|
+
|
26
35
|
private
|
27
36
|
|
28
37
|
def value_to_statuses(value)
|
@@ -35,6 +44,12 @@ module StateStore
|
|
35
44
|
result
|
36
45
|
end
|
37
46
|
|
47
|
+
def statuses_to_values(statuses_array)
|
48
|
+
self.statuses.map do |status|
|
49
|
+
statuses_array.include?(status) ? "1" : "0"
|
50
|
+
end.join("").to_i(2)
|
51
|
+
end
|
52
|
+
|
38
53
|
def normalized_array(array)
|
39
54
|
(Array.new(self.states - array.size, "0")+ array)
|
40
55
|
end
|
@@ -2,47 +2,66 @@ module StateStore
|
|
2
2
|
module Extension
|
3
3
|
|
4
4
|
module ClassMethods
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
raise ArgumentError.new("
|
9
|
-
|
10
|
-
|
5
|
+
|
6
|
+
def has_states *states
|
7
|
+
state_store_options = states && states.last.is_a?(Hash) && states.pop || {}
|
8
|
+
raise ArgumentError.new("No states given") if states.empty?
|
9
|
+
raise ArgumentError.new(":in is required") unless state_store_options[:in]
|
10
|
+
|
11
|
+
state_store_options[:as] ||= :states
|
12
|
+
store = StateStore::BinaryStore.new(states)
|
13
|
+
|
14
|
+
@states_stores ||={}
|
15
|
+
@states_stores_options ||={}
|
16
|
+
|
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])
|
11
20
|
end
|
12
21
|
|
13
|
-
def
|
14
|
-
@
|
22
|
+
def states_stores
|
23
|
+
@states_stores
|
15
24
|
end
|
16
25
|
|
17
|
-
def
|
18
|
-
@
|
26
|
+
def states_stores_options
|
27
|
+
@states_stores_options
|
19
28
|
end
|
20
29
|
|
21
30
|
private
|
22
31
|
|
23
|
-
def
|
24
|
-
class_eval <<-ALIAS,__FILE__,__LINE__+1
|
25
|
-
if self.instance_methods.include?(:#{self.state_store_options[:in]})
|
26
|
-
alias :status_store_original_#{self.state_store_options[:in]} :#{self.state_store_options[:in]}
|
27
|
-
end
|
28
|
-
ALIAS
|
32
|
+
def create_methods_for_state_store(name)
|
29
33
|
|
30
34
|
self.class_eval do
|
31
|
-
define_method
|
32
|
-
|
33
|
-
value = self.
|
34
|
-
self.class.
|
35
|
+
define_method name do
|
36
|
+
method_name = self.class.states_stores_options[name][:in]
|
37
|
+
value = self.send(method_name)
|
38
|
+
self.class.states_stores[name].humanize(value)
|
39
|
+
end
|
40
|
+
|
41
|
+
define_method :"#{name}=" do |humanized_array|
|
42
|
+
method_name = self.class.states_stores_options[name][:in]
|
43
|
+
store = self.class.states_stores[name]
|
44
|
+
self.send(:"#{method_name}=",store.value(humanized_array))
|
35
45
|
end
|
36
46
|
end
|
47
|
+
|
37
48
|
end
|
38
49
|
end
|
39
50
|
|
40
51
|
module InstanceMethods
|
41
52
|
def self.included(base)
|
42
|
-
|
43
53
|
def method_missing(method_name, *args)
|
44
|
-
|
45
|
-
|
54
|
+
state_method_name = method_name.to_s.match(/^has_(\w+)\?$/)
|
55
|
+
state_method_name = state_method_name && state_method_name[1].to_sym
|
56
|
+
if state_method_name
|
57
|
+
options = self.class.states_stores_options.detect{|key,value| value[:in].to_sym == state_method_name}
|
58
|
+
options = options && options[1]
|
59
|
+
if options
|
60
|
+
instance_method_name = options && options[:in]
|
61
|
+
self.class.states_stores[options[:as]].has_status?(args[0],self.send(instance_method_name))
|
62
|
+
else
|
63
|
+
super
|
64
|
+
end
|
46
65
|
else
|
47
66
|
super
|
48
67
|
end
|
data/spec/binary_store_spec.rb
CHANGED
@@ -66,4 +66,16 @@ describe StateStore::BinaryStore do
|
|
66
66
|
store.index(1,"1").should eq(:two)
|
67
67
|
store.index(1,"0").should be_nil
|
68
68
|
end
|
69
|
+
|
70
|
+
it "should return value from statuses array" do
|
71
|
+
store = klass.new([:read,:write,:execute])
|
72
|
+
store.value([]).should eq(0)
|
73
|
+
store.value([:execute]).should eq(1)
|
74
|
+
store.value([:write]).should eq(2)
|
75
|
+
store.value([:write,:execute]).should eq(3)
|
76
|
+
store.value([:read]).should eq(4)
|
77
|
+
store.value([:read,:execute]).should eq(5)
|
78
|
+
store.value([:read,:write]).should eq(6)
|
79
|
+
store.value([:read,:write,:execute]).should eq(7)
|
80
|
+
end
|
69
81
|
end
|
data/spec/extension_spec.rb
CHANGED
@@ -14,26 +14,26 @@ describe StateStore::Extension do
|
|
14
14
|
klass.send(:include,StateStore)
|
15
15
|
end
|
16
16
|
|
17
|
-
it "should raise error when no
|
17
|
+
it "should raise error when no states is given" do
|
18
18
|
expect{
|
19
|
-
klass.
|
20
|
-
}.to raise_error(ArgumentError,"No
|
19
|
+
klass.has_states()
|
20
|
+
}.to raise_error(ArgumentError,"No states given")
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should raise error when no :in key is given" do
|
24
24
|
expect{
|
25
|
-
klass.
|
25
|
+
klass.has_states :read,:write
|
26
26
|
}.to raise_error(ArgumentError,":in is required")
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should create store with given statuses" do
|
30
|
-
klass.
|
31
|
-
klass.
|
30
|
+
klass.has_states :read,:write, :in => :status
|
31
|
+
klass.states_stores[:states].statuses.should eq([:read,:write])
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should store given options in #state_store_options" do
|
35
|
-
klass.
|
36
|
-
klass.
|
35
|
+
klass.has_states :read,:write, :in => :status
|
36
|
+
klass.states_stores_options[:states].should eq({:in => :status, :as => :states})
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -42,14 +42,14 @@ describe StateStore::Extension do
|
|
42
42
|
before(:each){
|
43
43
|
klass.class_eval do
|
44
44
|
def status_name
|
45
|
-
5
|
45
|
+
@status_name || 5
|
46
46
|
end
|
47
47
|
end
|
48
48
|
klass.send(:include,StateStore)
|
49
49
|
}
|
50
50
|
|
51
51
|
it "should call store #has_status? when #has_status_name? is called on klass instance" do
|
52
|
-
klass.
|
52
|
+
klass.has_states :read,:write,:execute, :in => :status_name
|
53
53
|
klass.any_instance.stub(:status_name).and_return(5)
|
54
54
|
|
55
55
|
object.has_status_name?(:read).should be_true
|
@@ -57,12 +57,19 @@ describe StateStore::Extension do
|
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should call store #humanize when [status_name] is called on instance of class" do
|
60
|
-
klass.
|
61
|
-
object.
|
60
|
+
klass.has_states :read,:write,:execute, :in => :status_name
|
61
|
+
object.states.should eq([:read,:execute])
|
62
62
|
end
|
63
63
|
|
64
|
-
it "should
|
65
|
-
|
64
|
+
it "should assign value from array to status method" do
|
65
|
+
klass.has_states :read, :write, :execute, :in => :status_name
|
66
|
+
klass.class_eval do
|
67
|
+
def status_name=(value)
|
68
|
+
@status_name = value
|
69
|
+
end
|
70
|
+
end
|
71
|
+
object.states= [:read,:write]
|
72
|
+
object.status_name.should eq(6)
|
66
73
|
end
|
67
74
|
end
|
68
75
|
|
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.
|
4
|
+
version: 0.0.2
|
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: &
|
16
|
+
requirement: &19920720 !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: *
|
24
|
+
version_requirements: *19920720
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &19919080 !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: *
|
35
|
+
version_requirements: *19919080
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &19936760 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 1.8.3
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *19936760
|
47
47
|
description: Create numeric value to Array of states.
|
48
48
|
email: arturs.meisters@gmail.com
|
49
49
|
executables: []
|
@@ -83,7 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
83
83
|
version: '0'
|
84
84
|
segments:
|
85
85
|
- 0
|
86
|
-
hash: -
|
86
|
+
hash: -2977312246934280863
|
87
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
88
|
none: false
|
89
89
|
requirements:
|