selectable_attr 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,430 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+
4
+ describe SelectableAttr do
5
+
6
+ def assert_enum_class_methods(klass, attr = :enum1)
7
+ klass.send("#{attr}_enum").length.should == 3
8
+ expected_hash_array = [
9
+ {:id => 1, :key => :entry1, :name => "エントリ1"},
10
+ {:id => 2, :key => :entry2, :name => "エントリ2"},
11
+ {:id => 3, :key => :entry3, :name => "エントリ3"}
12
+ ]
13
+ klass.send("#{attr}_hash_array").should == expected_hash_array
14
+ klass.send("#{attr}_enum").to_hash_array.should == expected_hash_array
15
+ klass.send("#{attr}_entries").
16
+ map{|entry| {:id => entry.id, :key => entry.key, :name => entry.name} }.
17
+ should == expected_hash_array
18
+
19
+ klass.send("#{attr}_ids").should == [1,2,3]
20
+ klass.send("#{attr}_ids", :entry2, :entry3).should == [2,3]
21
+ klass.send("#{attr}_keys").should == [:entry1, :entry2, :entry3]
22
+ klass.send("#{attr}_keys", 1,3).should == [:entry1, :entry3]
23
+ klass.send("#{attr}_names").should == ["エントリ1", "エントリ2", "エントリ3"]
24
+ klass.send("#{attr}_names", 1,2).should == ["エントリ1", "エントリ2"]
25
+ klass.send("#{attr}_names", :entry1, :entry2).should == ["エントリ1", "エントリ2"]
26
+ klass.send("#{attr}_options").should == [['エントリ1', 1], ['エントリ2', 2], ['エントリ3', 3]]
27
+ klass.send("#{attr}_options", :entry2, :entry3).should == [['エントリ2', 2], ['エントリ3', 3]]
28
+ klass.send("#{attr}_options", 1,2).should == [['エントリ1', 1], ['エントリ2', 2]]
29
+
30
+ klass.send("#{attr}_id_by_key", nil).should be_nil
31
+ klass.send("#{attr}_id_by_key", :entry1).should == 1
32
+ klass.send("#{attr}_id_by_key", :entry2).should == 2
33
+ klass.send("#{attr}_id_by_key", :entry3).should == 3
34
+ klass.send("#{attr}_name_by_key", nil).should be_nil
35
+ klass.send("#{attr}_name_by_key", :entry1).should == "エントリ1"
36
+ klass.send("#{attr}_name_by_key", :entry2).should == "エントリ2"
37
+ klass.send("#{attr}_name_by_key", :entry3).should == "エントリ3"
38
+
39
+ klass.send("#{attr}_key_by_id", nil).should be_nil
40
+ klass.send("#{attr}_key_by_id", 1).should == :entry1
41
+ klass.send("#{attr}_key_by_id", 2).should == :entry2
42
+ klass.send("#{attr}_key_by_id", 3).should == :entry3
43
+ klass.send("#{attr}_name_by_id", nil).should be_nil
44
+ klass.send("#{attr}_name_by_id", 1).should == "エントリ1"
45
+ klass.send("#{attr}_name_by_id", 2).should == "エントリ2"
46
+ klass.send("#{attr}_name_by_id", 3).should == "エントリ3"
47
+ end
48
+
49
+ def assert_single_enum_instance_methods(obj, attr = :enum1)
50
+ obj.send("#{attr}=", 1)
51
+ obj.send(attr).should == 1
52
+ obj.enum1_key.should == :entry1
53
+ obj.enum1_name.should == "エントリ1"
54
+ obj.enum1_entry.to_hash.should == {:id => 1, :key => :entry1, :name => "エントリ1"}
55
+
56
+ obj.enum1_key = :entry2
57
+ obj.send(attr).should == 2
58
+ obj.enum1_key.should == :entry2
59
+ obj.enum1_name.should == "エントリ2"
60
+ obj.enum1_entry.to_hash.should == {:id => 2, :key => :entry2, :name => "エントリ2"}
61
+
62
+ obj.send("#{attr}=", 3)
63
+ obj.send(attr).should == 3
64
+ obj.enum1_key.should == :entry3
65
+ obj.enum1_name.should == "エントリ3"
66
+ obj.enum1_entry.to_hash.should == {:id => 3, :key => :entry3, :name => "エントリ3"}
67
+ end
68
+
69
+ class EnumBase
70
+ include ::SelectableAttr::Base
71
+ end
72
+
73
+ describe "selectable_attr with default" do
74
+ class EnumMock1 < EnumBase
75
+ selectable_attr :enum1, :default => 2 do
76
+ entry 1, :entry1, "エントリ1"
77
+ entry 2, :entry2, "エントリ2"
78
+ entry 3, :entry3, "エントリ3"
79
+ end
80
+ end
81
+
82
+ class EnumMock1WithEnum < EnumBase
83
+ selectable_attr :enum1, :default => 2 do
84
+ entry 1, :entry1, "エントリ1"
85
+ entry 2, :entry2, "エントリ2"
86
+ entry 3, :entry3, "エントリ3"
87
+ end
88
+ end
89
+
90
+ it "test_selectable_attr1" do
91
+ assert_enum_class_methods(EnumMock1)
92
+ mock1 = EnumMock1.new
93
+ mock1.enum1.should == 2
94
+ assert_single_enum_instance_methods(mock1)
95
+
96
+ assert_enum_class_methods(EnumMock1WithEnum)
97
+ mock1 = EnumMock1WithEnum.new
98
+ mock1.enum1.should == 2
99
+ assert_single_enum_instance_methods(mock1)
100
+
101
+ EnumMock1.selectable_attr_type_for(:enum1).should == :single
102
+ EnumMock1WithEnum.selectable_attr_type_for(:enum1).should == :single
103
+ end
104
+ end
105
+
106
+
107
+ describe "attr_enumeable_base" do
108
+ class EnumMock2 < EnumBase
109
+ attr_enumeable_base do |attr|
110
+ attr.to_s.gsub(/(.*)_code(.*)$/){"#{$1}#{$2}"}
111
+ end
112
+
113
+ selectable_attr :enum_code1 do
114
+ entry 1, :entry1, "エントリ1"
115
+ entry 2, :entry2, "エントリ2"
116
+ entry 3, :entry3, "エントリ3"
117
+ end
118
+ end
119
+
120
+ class EnumMock2WithEnum < EnumBase
121
+ attr_enumeable_base do |attr|
122
+ attr.to_s.gsub(/(.*)_code(.*)$/){"#{$1}#{$2}"}
123
+ end
124
+
125
+ enum :enum_code1 do
126
+ entry 1, :entry1, "エントリ1"
127
+ entry 2, :entry2, "エントリ2"
128
+ entry 3, :entry3, "エントリ3"
129
+ end
130
+ end
131
+
132
+ it "test_selectable_attr2" do
133
+ assert_enum_class_methods(EnumMock2)
134
+ assert_single_enum_instance_methods(EnumMock2.new, :enum_code1)
135
+ assert_enum_class_methods(EnumMock2WithEnum)
136
+ assert_single_enum_instance_methods(EnumMock2WithEnum.new, :enum_code1)
137
+ end
138
+ end
139
+
140
+
141
+ def assert_multi_enum_instance_methods(obj, patterns)
142
+ obj.enum_array1_hash_array.should == [
143
+ {:id => 1, :key => :entry1, :name => "エントリ1", :select => false},
144
+ {:id => 2, :key => :entry2, :name => "エントリ2", :select => false},
145
+ {:id => 3, :key => :entry3, :name => "エントリ3", :select => false}
146
+ ]
147
+ obj.enum_array1_selection.should == [false, false, false]
148
+ obj.enum_array1.should be_nil
149
+ obj.enum_array1_entries.should == []
150
+ obj.enum_array1_keys.should == []
151
+ obj.enum_array1_names.should == []
152
+
153
+ obj.enum_array1 = patterns[0]
154
+ obj.enum_array1.should == patterns[0]
155
+ obj.enum_array1_hash_array.should == [
156
+ {:id => 1, :key => :entry1, :name => "エントリ1", :select => false},
157
+ {:id => 2, :key => :entry2, :name => "エントリ2", :select => false},
158
+ {:id => 3, :key => :entry3, :name => "エントリ3", :select => false}
159
+ ]
160
+ obj.enum_array1_selection.should == [false, false, false]
161
+ obj.enum_array1_entries.should == []
162
+ obj.enum_array1_keys.should == []
163
+ obj.enum_array1_names.should == []
164
+
165
+ obj.enum_array1 = patterns[1]
166
+ obj.enum_array1.should == patterns[1]
167
+ obj.enum_array1_hash_array.should == [
168
+ {:id => 1, :key => :entry1, :name => "エントリ1", :select => false},
169
+ {:id => 2, :key => :entry2, :name => "エントリ2", :select => false},
170
+ {:id => 3, :key => :entry3, :name => "エントリ3", :select => true}
171
+ ]
172
+ obj.enum_array1_selection.should == [false, false, true]
173
+ obj.enum_array1_entries.map(&:id).should == [3]
174
+ obj.enum_array1_keys.should == [:entry3]
175
+ obj.enum_array1_names.should == ['エントリ3']
176
+
177
+ obj.enum_array1 = patterns[3]
178
+ obj.enum_array1.should == patterns[3]
179
+ obj.enum_array1_hash_array.should == [
180
+ {:id => 1, :key => :entry1, :name => "エントリ1", :select => false},
181
+ {:id => 2, :key => :entry2, :name => "エントリ2", :select => true},
182
+ {:id => 3, :key => :entry3, :name => "エントリ3", :select => true}
183
+ ]
184
+ obj.enum_array1_selection.should == [false, true, true]
185
+ obj.enum_array1_entries.map(&:id).should == [2, 3]
186
+ obj.enum_array1_keys.should == [:entry2, :entry3]
187
+ obj.enum_array1_names.should == ['エントリ2', 'エントリ3']
188
+
189
+ obj.enum_array1 = patterns[7]
190
+ obj.enum_array1.should == patterns[7]
191
+ obj.enum_array1_hash_array.should == [
192
+ {:id => 1, :key => :entry1, :name => "エントリ1", :select => true},
193
+ {:id => 2, :key => :entry2, :name => "エントリ2", :select => true},
194
+ {:id => 3, :key => :entry3, :name => "エントリ3", :select => true}
195
+ ]
196
+ obj.enum_array1_selection.should == [true, true, true]
197
+ obj.enum_array1_ids.should == [1, 2, 3]
198
+ obj.enum_array1_entries.map(&:id).should == [1, 2, 3]
199
+ obj.enum_array1_keys.should == [:entry1, :entry2, :entry3]
200
+ obj.enum_array1_names.should == ['エントリ1', 'エントリ2', 'エントリ3']
201
+
202
+ obj.enum_array1_ids = [1,3]; obj.enum_array1.should == patterns[5]
203
+ obj.enum_array1_ids = [1,2]; obj.enum_array1.should == patterns[6]
204
+ obj.enum_array1_ids = [2]; obj.enum_array1.should == patterns[2]
205
+
206
+ obj.enum_array1_keys = [:entry1,:entry3]; obj.enum_array1.should == patterns[5]
207
+ obj.enum_array1_keys = [:entry1,:entry2]; obj.enum_array1.should == patterns[6]
208
+ obj.enum_array1_keys = [:entry2]; obj.enum_array1.should == patterns[2]
209
+
210
+ obj.enum_array1_selection = [true, false, true]; obj.enum_array1.should == patterns[5]
211
+ obj.enum_array1_selection = [true, true, false]; obj.enum_array1.should == patterns[6]
212
+ obj.enum_array1_selection = [false, true, false]; obj.enum_array1.should == patterns[2]
213
+
214
+ obj.enum_array1_ids = "1,3"; obj.enum_array1.should == patterns[5]
215
+ obj.enum_array1_ids = "1,2"; obj.enum_array1.should == patterns[6]
216
+ obj.enum_array1_ids = "2"; obj.enum_array1.should == patterns[2]
217
+ end
218
+
219
+ describe ":convert_with => :binary_string" do
220
+ class EnumMock3 < EnumBase
221
+ multi_selectable_attr :enum_array1, :convert_with => :binary_string do
222
+ entry 1, :entry1, "エントリ1"
223
+ entry 2, :entry2, "エントリ2"
224
+ entry 3, :entry3, "エントリ3"
225
+ end
226
+ end
227
+
228
+ class EnumMock3WithEnumArray < EnumBase
229
+ enum_array :enum_array1, :convert_with => :binary_string do
230
+ entry 1, :entry1, "エントリ1"
231
+ entry 2, :entry2, "エントリ2"
232
+ entry 3, :entry3, "エントリ3"
233
+ end
234
+ end
235
+
236
+ it "test_multi_selectable_attr_with_binary_string" do
237
+ expected = (0..7).map{|i| '%-03b' % i} # ["000", "001", "010", "011", "100", "101", "110", "111"]
238
+ assert_enum_class_methods(EnumMock3, :enum_array1)
239
+ assert_multi_enum_instance_methods(EnumMock3.new, expected)
240
+ assert_enum_class_methods(EnumMock3WithEnumArray, :enum_array1)
241
+ assert_multi_enum_instance_methods(EnumMock3WithEnumArray.new, expected)
242
+ EnumMock3.selectable_attr_type_for(:enum_array1).should == :multi
243
+ end
244
+ end
245
+
246
+ describe "multi_selectable_attr" do
247
+ class EnumMock4 < EnumBase
248
+ multi_selectable_attr :enum_array1 do
249
+ entry 1, :entry1, "エントリ1"
250
+ entry 2, :entry2, "エントリ2"
251
+ entry 3, :entry3, "エントリ3"
252
+ end
253
+ end
254
+
255
+ class EnumMock4WithEnumArray < EnumBase
256
+ enum_array :enum_array1 do
257
+ entry 1, :entry1, "エントリ1"
258
+ entry 2, :entry2, "エントリ2"
259
+ entry 3, :entry3, "エントリ3"
260
+ end
261
+ end
262
+
263
+ it "test_multi_selectable_attr2" do
264
+ # [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
265
+ expected =
266
+ (0..7).map do |i|
267
+ s = '%03b' % i
268
+ a = s.split('').map{|v| v.to_i}
269
+ ret = []
270
+ a.each_with_index{|val, pos| ret << pos + 1 if val == 1}
271
+ ret
272
+ end
273
+ assert_enum_class_methods(EnumMock4, :enum_array1)
274
+ assert_multi_enum_instance_methods(EnumMock4.new, expected)
275
+ assert_enum_class_methods(EnumMock4WithEnumArray, :enum_array1)
276
+ assert_multi_enum_instance_methods(EnumMock4WithEnumArray.new, expected)
277
+ end
278
+ end
279
+
280
+ describe "convert_with" do
281
+ class EnumMock5 < EnumBase
282
+ multi_selectable_attr :enum_array1, :convert_with => :comma_string do
283
+ entry 1, :entry1, "エントリ1"
284
+ entry 2, :entry2, "エントリ2"
285
+ entry 3, :entry3, "エントリ3"
286
+ end
287
+ end
288
+
289
+ class EnumMock5WithEnumArray < EnumBase
290
+ enum_array :enum_array1, :convert_with => :comma_string do
291
+ entry 1, :entry1, "エントリ1"
292
+ entry 2, :entry2, "エントリ2"
293
+ entry 3, :entry3, "エントリ3"
294
+ end
295
+ end
296
+
297
+ it "test_multi_selectable_attr_with_comma_string" do
298
+ # ["", "3", "2", "2,3", "1", "1,3", "1,2", "1,2,3"]
299
+ expected =
300
+ (0..7).map do |i|
301
+ s = '%03b' % i
302
+ a = s.split('').map{|v| v.to_i}
303
+ ret = []
304
+ a.each_with_index{|val, pos| ret << pos + 1 if val == 1}
305
+ ret.join(',')
306
+ end
307
+ assert_enum_class_methods(EnumMock5, :enum_array1)
308
+ assert_multi_enum_instance_methods(EnumMock5.new, expected)
309
+ assert_enum_class_methods(EnumMock5WithEnumArray, :enum_array1)
310
+ assert_multi_enum_instance_methods(EnumMock5WithEnumArray.new, expected)
311
+ end
312
+ end
313
+
314
+ describe "selectable_attr_name_pattern" do
315
+ class EnumMock6 < EnumBase
316
+ # self.selectable_attr_name_pattern = /(_cd$|_code$|_cds$|_codes$)/
317
+ selectable_attr :category_id do
318
+ entry "01", :category1, "カテゴリ1"
319
+ entry "02", :category2, "カテゴリ2"
320
+ end
321
+ end
322
+
323
+ class EnumMock7 < EnumBase
324
+ self.selectable_attr_name_pattern = /(_cd$|_id$|_cds$|_ids$)/
325
+ selectable_attr :category_id do
326
+ entry "01", :category1, "カテゴリ1"
327
+ entry "02", :category2, "カテゴリ2"
328
+ end
329
+ end
330
+
331
+ it "test_selectable_attr_name_pattern" do
332
+ EnumMock6.selectable_attr_name_pattern.should == /(_cd$|_code$|_cds$|_codes$)/
333
+ EnumMock6.respond_to?(:category_enum).should == false
334
+ EnumMock6.respond_to?(:category_id_enum).should == true
335
+ EnumMock6.new.respond_to?(:category_key).should == false
336
+ EnumMock6.new.respond_to?(:category_id_key).should == true
337
+
338
+ EnumMock7.selectable_attr_name_pattern.should == /(_cd$|_id$|_cds$|_ids$)/
339
+ EnumMock7.respond_to?(:category_enum).should == true
340
+ EnumMock7.respond_to?(:category_id_enum).should == false
341
+ EnumMock7.new.respond_to?(:category_key).should == true
342
+ EnumMock7.new.respond_to?(:category_id_key).should == false
343
+ end
344
+ end
345
+
346
+ describe "has_attr" do
347
+ class ConnectableMock1 < EnumBase
348
+ class << self
349
+ def columns; end
350
+ def connection; end
351
+ def connectec?; end
352
+ def table_exists?; end
353
+ end
354
+ end
355
+
356
+ it "should return false if column does exist" do
357
+ ConnectableMock1.should_receive(:connected?).and_return(true)
358
+ ConnectableMock1.should_receive(:table_exists?).and_return(true)
359
+ ConnectableMock1.should_receive(:columns).and_return([mock(:column1, :name => :column1)])
360
+ ConnectableMock1.has_attr(:column1).should == true
361
+ end
362
+
363
+ it "should return false if column doesn't exist" do
364
+ ConnectableMock1.should_receive(:connected?).and_return(true)
365
+ ConnectableMock1.should_receive(:table_exists?).and_return(true)
366
+ ConnectableMock1.should_receive(:columns).and_return([mock(:column1, :name => :column1)])
367
+ ConnectableMock1.has_attr(:unknown_column).should == false
368
+ end
369
+
370
+ it "should return nil unless connected" do
371
+ ConnectableMock1.should_receive(:connected?).and_return(false)
372
+ ConnectableMock1.should_receive(:table_exists?).and_return(false)
373
+ ConnectableMock1.has_attr(:unknown_column).should == false
374
+ end
375
+
376
+ it "should return nil if can't connection cause of Exception" do
377
+ ConnectableMock1.should_receive(:connected?).twice.and_return(false)
378
+ ConnectableMock1.should_receive(:connection).and_raise(IOError.new("can't connect to DB"))
379
+ ConnectableMock1.has_attr(:unknown_column).should == nil
380
+ end
381
+ end
382
+
383
+ describe "enum_for" do
384
+ class EnumMock10 < EnumBase
385
+ selectable_attr :enum1, :default => 2 do
386
+ entry 1, :entry1, "エントリ1"
387
+ entry 2, :entry2, "エントリ2"
388
+ entry 3, :entry3, "エントリ3"
389
+ end
390
+ end
391
+
392
+ it "return constant by Symbol access" do
393
+ enum1 = EnumMock10.enum_for(:enum1)
394
+ enum1.class.should == SelectableAttr::Enum
395
+ end
396
+
397
+ it "return constant by String access" do
398
+ enum1 = EnumMock10.enum_for('enum1')
399
+ enum1.class.should == SelectableAttr::Enum
400
+ end
401
+
402
+ it "return nil for unexist attr" do
403
+ enum1 = EnumMock10.enum_for('unexist_attr')
404
+ enum1.should == nil
405
+ end
406
+
407
+ end
408
+
409
+ describe "define_accessor" do
410
+ class DefiningMock1 < EnumBase
411
+ class << self
412
+ def attr_accessor_with_default(*args); end
413
+ end
414
+ end
415
+
416
+ it "should call attr_accessor_with_default when both of attr_accessor and default are given" do
417
+ DefiningMock1.should_receive(:attr_accessor_with_default).with(:enum1, 1)
418
+ DefiningMock1.define_accessor(:attr => :enum1, :attr_accessor => true, :default => 1)
419
+ end
420
+
421
+ it "should call attr_accessor_with_default when default are given but attr_accessor is not TRUE" do
422
+ $stderr.should_receive(:puts).with("WARNING! :default option ignored for enum1")
423
+ DefiningMock1.define_accessor(:attr => :enum1, :attr_accessor => false, :default => 1)
424
+ end
425
+
426
+
427
+ end
428
+
429
+
430
+ end
@@ -0,0 +1,129 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
3
+
4
+ describe SelectableAttr::Enum do
5
+
6
+ Enum1 = SelectableAttr::Enum.new do
7
+ entry 1, :book, '書籍'
8
+ entry 2, :dvd, 'DVD'
9
+ entry 3, :cd, 'CD'
10
+ entry 4, :vhs, 'VHS'
11
+ end
12
+
13
+ it "test_define" do
14
+ Enum1[1].id.should == 1
15
+ Enum1[2].id.should == 2
16
+ Enum1[3].id.should == 3
17
+ Enum1[4].id.should == 4
18
+ Enum1[1].key.should == :book
19
+ Enum1[2].key.should == :dvd
20
+ Enum1[3].key.should == :cd
21
+ Enum1[4].key.should == :vhs
22
+ Enum1[1].name.should == '書籍'
23
+ Enum1[2].name.should == 'DVD'
24
+ Enum1[3].name.should == 'CD'
25
+ Enum1[4].name.should == 'VHS'
26
+
27
+ Enum1[:book].id.should == 1
28
+ Enum1[:dvd ].id.should == 2
29
+ Enum1[:cd ].id.should == 3
30
+ Enum1[:vhs ].id.should == 4
31
+ Enum1[:book].key.should == :book
32
+ Enum1[:dvd ].key.should == :dvd
33
+ Enum1[:cd ].key.should == :cd
34
+ Enum1[:vhs ].key.should == :vhs
35
+ Enum1[:book].name.should == '書籍'
36
+ Enum1[:dvd].name.should == 'DVD'
37
+ Enum1[:cd].name.should == 'CD'
38
+ Enum1[:vhs].name.should == 'VHS'
39
+
40
+ Enum1.values.should == [['書籍', 1], ['DVD', 2], ['CD', 3], ['VHS', 4]]
41
+ Enum1.values(:name, :id).should == [['書籍', 1], ['DVD', 2], ['CD', 3], ['VHS', 4]]
42
+ Enum1.values(:name, :key).should == [['書籍', :book], ['DVD', :dvd], ['CD', :cd], ['VHS', :vhs]]
43
+ end
44
+
45
+ InetAccess = SelectableAttr::Enum.new do
46
+ entry 1, :email, 'Eメール', :protocol => 'mailto:'
47
+ entry 2, :website, 'ウェブサイト', :protocol => 'http://'
48
+ entry 3, :ftp, 'FTP', :protocol => 'ftp://'
49
+ end
50
+
51
+ it "test_define_with_options" do
52
+ InetAccess[1].id.should == 1
53
+ InetAccess[2].id.should == 2
54
+ InetAccess[3].id.should == 3
55
+ InetAccess[1].key.should == :email
56
+ InetAccess[2].key.should == :website
57
+ InetAccess[3].key.should == :ftp
58
+
59
+
60
+ InetAccess[1].name.should == 'Eメール'
61
+ InetAccess[2].name.should == 'ウェブサイト'
62
+ InetAccess[3].name.should == 'FTP'
63
+ InetAccess[1][:protocol].should == 'mailto:'
64
+ InetAccess[2][:protocol].should == 'http://'
65
+ InetAccess[3][:protocol].should == 'ftp://'
66
+
67
+ InetAccess[9].id.should be_nil
68
+ InetAccess[9].key.should be_nil
69
+ InetAccess[9].name.should be_nil
70
+ InetAccess[9][:protocol].should be_nil
71
+ InetAccess[9][:xxxx].should be_nil
72
+ end
73
+
74
+ it "test_get_by_option" do
75
+ InetAccess[:protocol => 'mailto:'].should == InetAccess[1]
76
+ InetAccess[:protocol => 'http://'].should == InetAccess[2]
77
+ InetAccess[:protocol => 'ftp://'].should == InetAccess[3]
78
+ end
79
+
80
+ it "test_null?" do
81
+ InetAccess[1].null?.should == false
82
+ InetAccess[2].null?.should == false
83
+ InetAccess[3].null?.should == false
84
+ InetAccess[9].null?.should == true
85
+ InetAccess[:protocol => 'mailto:'].null?.should == false
86
+ InetAccess[:protocol => 'http://'].null?.should == false
87
+ InetAccess[:protocol => 'ftp://'].null?.should == false
88
+ InetAccess[:protocol => 'svn://'].null?.should == true
89
+ end
90
+
91
+ it "test_null_object?" do
92
+ InetAccess[1].null_object?.should == false
93
+ InetAccess[2].null_object?.should == false
94
+ InetAccess[3].null_object?.should == false
95
+ InetAccess[9].null_object?.should == true
96
+ InetAccess[:protocol => 'mailto:'].null_object?.should == false
97
+ InetAccess[:protocol => 'http://'].null_object?.should == false
98
+ InetAccess[:protocol => 'ftp://'].null_object?.should == false
99
+ InetAccess[:protocol => 'svn://'].null_object?.should == true
100
+ end
101
+
102
+ it "test_to_hash_array" do
103
+ Enum1.to_hash_array.should == [
104
+ {:id => 1, :key => :book, :name => '書籍'},
105
+ {:id => 2, :key => :dvd, :name => 'DVD'},
106
+ {:id => 3, :key => :cd, :name => 'CD'},
107
+ {:id => 4, :key => :vhs, :name => 'VHS'}
108
+ ]
109
+
110
+ InetAccess.to_hash_array.should == [
111
+ {:id => 1, :key => :email, :name => 'Eメール', :protocol => 'mailto:'},
112
+ {:id => 2, :key => :website, :name => 'ウェブサイト', :protocol => 'http://'},
113
+ {:id => 3, :key => :ftp, :name => 'FTP', :protocol => 'ftp://'}
114
+ ]
115
+ end
116
+
117
+ describe "find" do
118
+ it "with options" do
119
+ InetAccess.find(:protocol => 'http://').should == InetAccess[2]
120
+ InetAccess.find(:protocol => 'svn+ssh://').should == SelectableAttr::Enum::Entry::NULL
121
+ end
122
+
123
+ it "with block" do
124
+ InetAccess.find{|entry| entry.key.to_s =~ /tp/}.should == InetAccess[3]
125
+ InetAccess.find{|entry| entry.key.to_s =~ /XXXXXX/}.should == SelectableAttr::Enum::Entry::NULL
126
+ end
127
+ end
128
+
129
+ end
@@ -0,0 +1,11 @@
1
+ $KCODE='u'
2
+
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require File.join(File.dirname(__FILE__), '..', 'init')
5
+
6
+ def assert_hash(expected, actual)
7
+ keys = (expected.keys + actual.keys).uniq
8
+ keys.each do |key|
9
+ assert_equal expected[key], actual[key], "unmatch value for #{key.inspect}"
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ require 'yaml'
2
+ require 'yaml_waml'
3
+
4
+ namespace :i18n do
5
+ namespace :selectable_attr do
6
+ task :load_all_models => :environment do
7
+ Dir.glob(File.join(RAILS_ROOT, 'app', 'models', '**', '*.rb')) do |file_name|
8
+ require file_name
9
+ end
10
+ end
11
+
12
+ desc "Export i18n resources for selectable_attr entries"
13
+ task :export => :"i18n:selectable_attr:load_all_models" do
14
+ obj = {I18n.locale => SelectableAttr::Enum.i18n_export}
15
+ puts YAML.dump(obj)
16
+ end
17
+ end
18
+ end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: selectable_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.7
5
+ platform: ruby
6
+ authors:
7
+ - Takeshi Akima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-19 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: selectable_attr generates extra methods dynamically for attribute which has options
17
+ email: akima@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - .gitignore
26
+ - MIT-LICENSE
27
+ - README
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - init.rb
31
+ - install.rb
32
+ - lib/selectable_attr.rb
33
+ - lib/selectable_attr/base.rb
34
+ - lib/selectable_attr/enum.rb
35
+ - lib/selectable_attr/version.rb
36
+ - selectable_attr.gemspec
37
+ - spec/selectable_attr_base_alias_spec.rb
38
+ - spec/selectable_attr_enum_spec.rb
39
+ - spec/spec_helper.rb
40
+ - tasks/selectable_attr_tasks.rake
41
+ - uninstall.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/akm/selectable_attr/
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: selectable_attr generates extra methods dynamically
70
+ test_files:
71
+ - spec/selectable_attr_base_alias_spec.rb
72
+ - spec/selectable_attr_enum_spec.rb
73
+ - spec/spec_helper.rb