trax_core 0.0.78 → 0.0.79
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trax/core/types/enum.rb +10 -2
- data/lib/trax_core/version.rb +1 -1
- data/spec/trax/core/types/enum_spec.rb +101 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb038c42261e07a3b01048fc35fa99c4a1ee8b93
|
4
|
+
data.tar.gz: 6ecb73367f017583abb6e27d854a398713c9d339
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73a01218a18838ea372bb4245381734d8cb107d30745d86723197ce116b0054946e0bcd679ab7110c8dc69998b5633079aad5a8f9563e3ab7cc926ba0577c38c
|
7
|
+
data.tar.gz: 6fa0352b4e6d2f1fa5ca4ec49e20546acaf37b9580c7a35a7e1e25c1b2a5436f0d8397842a1b39826225dfbfd876ddd65ed9103092dcdca2ac034b595356f00a
|
data/lib/trax/core/types/enum.rb
CHANGED
@@ -143,8 +143,16 @@ module Trax
|
|
143
143
|
|
144
144
|
### Hooks ###
|
145
145
|
def self.inherited(subklass)
|
146
|
-
subklass
|
147
|
-
|
146
|
+
super(subklass)
|
147
|
+
|
148
|
+
if self.instance_variable_defined?(:@_values_hash)
|
149
|
+
subklass.instance_variable_set(:@_values_hash, ::Hash.new.merge(@_values_hash.deep_dup))
|
150
|
+
subklass.instance_variable_set(:@_names_hash, ::Hash.new.merge(@_names_hash.deep_dup))
|
151
|
+
else
|
152
|
+
subklass.instance_variable_set(:@_values_hash, ::Hash.new)
|
153
|
+
subklass.instance_variable_set(:@_names_hash, ::Hash.new)
|
154
|
+
end
|
155
|
+
|
148
156
|
subklass.allow_nil = false
|
149
157
|
subklass.raise_on_invalid = false
|
150
158
|
end
|
data/lib/trax_core/version.rb
CHANGED
@@ -10,6 +10,18 @@ describe ::Trax::Core::Types::Enum do
|
|
10
10
|
define :da, 2
|
11
11
|
define :ca, 3
|
12
12
|
end
|
13
|
+
|
14
|
+
enum :Category do
|
15
|
+
define :default, 1
|
16
|
+
define :clothing, 2
|
17
|
+
define :shoes, 3
|
18
|
+
define :accessories, 4
|
19
|
+
end
|
20
|
+
|
21
|
+
enum :ExtendedCategory, :extend => "MyFakeEnumNamespace::Category" do
|
22
|
+
define :watches, 5
|
23
|
+
define :sunglasses, 6
|
24
|
+
end
|
13
25
|
end
|
14
26
|
end
|
15
27
|
|
@@ -28,4 +40,93 @@ describe ::Trax::Core::Types::Enum do
|
|
28
40
|
|
29
41
|
it { expect(subject).to eq nil }
|
30
42
|
end
|
43
|
+
|
44
|
+
context "category enum" do
|
45
|
+
subject do
|
46
|
+
"::MyFakeEnumNamespace::Category".constantize
|
47
|
+
end
|
48
|
+
|
49
|
+
let(:expected_names) { [:default, :clothing, :shoes, :accessories] }
|
50
|
+
let(:expected_values) { [1,2,3,4] }
|
51
|
+
|
52
|
+
describe ".key?" do
|
53
|
+
it { subject.key?(:default).should eq true }
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "[](val)" do
|
57
|
+
it { subject[:default].to_i.should eq 1 }
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "[](val)" do
|
61
|
+
it { subject["default"].to_i.should eq 1 }
|
62
|
+
end
|
63
|
+
|
64
|
+
describe ".value?" do
|
65
|
+
it { subject.value?(1).should eq true }
|
66
|
+
end
|
67
|
+
|
68
|
+
describe ".keys" do
|
69
|
+
it { subject.keys.should eq [:default, :clothing, :shoes, :accessories] }
|
70
|
+
end
|
71
|
+
|
72
|
+
describe ".names" do
|
73
|
+
it { subject.keys.should eq expected_names }
|
74
|
+
end
|
75
|
+
|
76
|
+
describe ".values" do
|
77
|
+
it { subject.values.should eq expected_values }
|
78
|
+
end
|
79
|
+
|
80
|
+
context "duplicate enum name" do
|
81
|
+
it { expect{subject.define_enum_value(:default, 6)}.to raise_error(::Trax::Core::Errors::DuplicateEnumValue) }
|
82
|
+
end
|
83
|
+
|
84
|
+
context "duplicate enum value" do
|
85
|
+
it {expect{subject.define_enum_value(:newthing, 1)}.to raise_error(::Trax::Core::Errors::DuplicateEnumValue) }
|
86
|
+
end
|
87
|
+
|
88
|
+
context "InstanceMethods" do
|
89
|
+
let(:described_object) do
|
90
|
+
"::MyFakeEnumNamespace::Category".constantize
|
91
|
+
end
|
92
|
+
subject { described_object.new(:clothing) }
|
93
|
+
|
94
|
+
it { subject.choice.should eq :clothing }
|
95
|
+
it { subject.choice.should eq 2 }
|
96
|
+
it { expect(subject.next_value.to_sym).to eq :shoes }
|
97
|
+
it { expect(subject.previous_value.to_sym).to eq :default }
|
98
|
+
|
99
|
+
context "selection of values" do
|
100
|
+
it { subject.select_next_value.should eq described_object.new(:shoes).choice }
|
101
|
+
end
|
102
|
+
context "value is last" do
|
103
|
+
subject { described_object.new(:accessories) }
|
104
|
+
it { subject.next_value?.should eq false }
|
105
|
+
it { subject.previous_value?.should eq true }
|
106
|
+
|
107
|
+
context "selection of value" do
|
108
|
+
it { expect(subject.select_next_value).to eq described_object.new(:accessories) }
|
109
|
+
it { expect(subject.select_previous_value).to eq described_object.new(:shoes) }
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "inheritance" do
|
116
|
+
let(:described_object) do
|
117
|
+
"::MyFakeEnumNamespace::ExtendedCategory".constantize
|
118
|
+
end
|
119
|
+
|
120
|
+
it { expect(described_object.names).to include(:watches)}
|
121
|
+
it { expect(described_object.new(:clothing).to_i).to eq 2 }
|
122
|
+
it { expect(described_object.new(:watches).to_i).to eq 5 }
|
123
|
+
|
124
|
+
context "does not mutate original hash" do
|
125
|
+
let(:described_object) do
|
126
|
+
"::MyFakeEnumNamespace::Category".constantize
|
127
|
+
end
|
128
|
+
|
129
|
+
it { expect(described_object.names).to_not include(:watches)}
|
130
|
+
end
|
131
|
+
end
|
31
132
|
end
|