has_constant 0.6.1 → 0.6.2
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.
- data/VERSION +1 -1
- data/has_constant.gemspec +1 -1
- data/lib/has_constant/orm/active_model.rb +1 -1
- data/lib/has_constant/orm/mongoid.rb +3 -9
- data/lib/has_constant.rb +1 -4
- data/test/has_constant_test.rb +0 -7
- data/test/unit/orm/mongoid_test.rb +30 -42
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.2
|
data/has_constant.gemspec
CHANGED
@@ -16,7 +16,7 @@ module HasConstant
|
|
16
16
|
if values.is_a?(Array)
|
17
17
|
self.class.send(name)[read_attribute(singular).to_i]
|
18
18
|
elsif values.is_a?(Hash)
|
19
|
-
self.class.send(name)[read_attribute(singular)]
|
19
|
+
HashWithIndifferentAccess.new(self.class.send(name))[read_attribute(singular)]
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -23,7 +23,7 @@ module HasConstant
|
|
23
23
|
|
24
24
|
class_eval do
|
25
25
|
unless fields.map(&:first).include?(singular.to_s)
|
26
|
-
field singular.to_sym, { :type =>
|
26
|
+
field singular.to_sym, { :type => Integer }.merge(options)
|
27
27
|
end
|
28
28
|
|
29
29
|
index singular.to_sym, :background => true if options[:index]
|
@@ -34,10 +34,8 @@ module HasConstant
|
|
34
34
|
|
35
35
|
define_method("#{singular}=") do |val|
|
36
36
|
if val.instance_of?(String)
|
37
|
-
if
|
37
|
+
if index = self.class.send(name.to_s).index(val)
|
38
38
|
write_attribute singular.to_sym, index
|
39
|
-
elsif values.is_a?(Hash) && values.has_value?(val)
|
40
|
-
write_attribute singular.to_sym, values.invert[val]
|
41
39
|
elsif !val.blank?
|
42
40
|
values = values.call if values.respond_to?(:call)
|
43
41
|
@has_constant_errors ||= {}
|
@@ -50,11 +48,7 @@ module HasConstant
|
|
50
48
|
|
51
49
|
# Add the getter method. This returns the string representation of the stored value
|
52
50
|
define_method(singular) do
|
53
|
-
if
|
54
|
-
self.class.send(name.to_s)[attributes[singular].to_i] if attributes[singular]
|
55
|
-
else
|
56
|
-
self.class.send(name.to_s)[attributes[singular]] if attributes[singular]
|
57
|
-
end
|
51
|
+
eval("#{self.class}.#{name.to_s}[self.attributes[singular].to_i] if self.attributes[singular]")
|
58
52
|
end
|
59
53
|
|
60
54
|
(class << self; self; end).instance_eval do
|
data/lib/has_constant.rb
CHANGED
@@ -42,7 +42,6 @@ module HasConstant
|
|
42
42
|
singular = (options[:accessor] || name.to_s.singularize).to_s
|
43
43
|
|
44
44
|
(class << self; self; end).instance_eval do
|
45
|
-
values = HashWithIndifferentAccess.new(values) if values.is_a?(Hash)
|
46
45
|
define_method(name.to_s, values) if values.respond_to?(:call)
|
47
46
|
define_method(name.to_s, lambda { values }) unless values.respond_to?(:call)
|
48
47
|
end
|
@@ -54,10 +53,8 @@ module HasConstant
|
|
54
53
|
# Add the setter method. This takes the string representation and converts it to an integer to store in the DB
|
55
54
|
define_method("#{singular}=") do |val|
|
56
55
|
if val.instance_of?(String)
|
57
|
-
if values.
|
56
|
+
if values.index(val)
|
58
57
|
instance_variable_set("@#{singular}", values.index(val))
|
59
|
-
elsif values.is_a?(Hash) && values.has_value?(val)
|
60
|
-
instance_variable_set("@#{singular}", values.invert[val].to_s)
|
61
58
|
else
|
62
59
|
raise ArgumentError,
|
63
60
|
"value for #{singular} must be in #{self.class.send(name.to_s).join(', ')}"
|
data/test/has_constant_test.rb
CHANGED
@@ -7,13 +7,6 @@ end
|
|
7
7
|
|
8
8
|
class TestHasConstant < Test::Unit::TestCase
|
9
9
|
|
10
|
-
should 'be able to store values in a hash' do
|
11
|
-
Model.has_constant :salutations, { :first => 'Mr', :second => 'Mrs' }
|
12
|
-
m = Model.new
|
13
|
-
m.salutation = 'Mr'
|
14
|
-
assert m.instance_variable_get("@salutation") == 'first'
|
15
|
-
end
|
16
|
-
|
17
10
|
should 'default values to translated values list' do
|
18
11
|
I18n.stubs(:t).returns(['a', 'b'])
|
19
12
|
Model.has_constant :titles
|
@@ -6,76 +6,68 @@ setup_mongoid
|
|
6
6
|
class MongoUser
|
7
7
|
include Mongoid::Document
|
8
8
|
include HasConstant
|
9
|
+
|
10
|
+
has_constant :salutations, ['Mr', 'Mrs']
|
9
11
|
end if defined?(Mongoid)
|
10
12
|
|
11
|
-
class
|
13
|
+
class MongoUserWithProc
|
12
14
|
include Mongoid::Document
|
13
15
|
include HasConstant
|
16
|
+
|
17
|
+
has_constant :salutations, lambda { ['Mr', 'Mrs'] }
|
14
18
|
end if defined?(Mongoid)
|
15
19
|
|
16
|
-
class
|
20
|
+
class MongoUserWithout
|
17
21
|
include Mongoid::Document
|
18
22
|
include HasConstant
|
19
|
-
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class AnotherUser
|
26
|
+
include Mongoid::Document
|
27
|
+
include HasConstant
|
28
|
+
end
|
20
29
|
|
21
30
|
class MongoidTest < Test::Unit::TestCase
|
22
31
|
context 'Instance' do
|
23
|
-
|
24
|
-
context 'using a hash' do
|
25
|
-
setup do
|
26
|
-
MongoUserWithHash.has_constant :salutations, { :first => 'Mr', :second => 'Mrs' }
|
27
|
-
@m = MongoUserWithHash.new(:salutation => 'Mr')
|
28
|
-
end
|
29
|
-
|
30
|
-
should 'store the hash key' do
|
31
|
-
assert_equal 'first', @m.attributes['salutation']
|
32
|
-
end
|
33
|
-
|
34
|
-
should 'return the correct value' do
|
35
|
-
assert_equal 'Mr', @m.salutation
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
32
|
should 'add the field automatically' do
|
40
|
-
|
41
|
-
assert
|
33
|
+
MongoUserWithout.has_constant :salutations, ['Mr', 'Mrs']
|
34
|
+
assert MongoUserWithout.fields.map(&:first).include?('salutation')
|
42
35
|
end
|
43
36
|
|
44
37
|
should 'not add the field if it is already there' do
|
45
|
-
|
46
|
-
|
47
|
-
assert_equal 'Mr',
|
38
|
+
MongoUserWithout.send(:field, :salutation, :type => Integer, :default => 0)
|
39
|
+
MongoUserWithout.has_constant :salutations, ['Mr', 'Mrs']
|
40
|
+
assert_equal 'Mr', MongoUserWithout.new.salutation
|
48
41
|
end
|
49
42
|
|
50
43
|
should 'be able to take default option' do
|
51
|
-
|
52
|
-
assert_equal 'Mr',
|
44
|
+
AnotherUser.has_constant :salutations, lambda { %w(Mr Mrs) }, { :default => 0 }
|
45
|
+
assert_equal 'Mr', AnotherUser.new.salutation
|
53
46
|
end
|
54
47
|
|
55
48
|
should 'take the accessor into account when adding the field' do
|
56
|
-
|
57
|
-
assert
|
49
|
+
MongoUserWithProc.has_constant :salutations, ['Mr', 'Mrs'], :accessor => :sal
|
50
|
+
assert MongoUserWithProc.fields.map(&:first).include?('sal')
|
58
51
|
end
|
59
52
|
|
60
53
|
should 'default values to translated values list' do
|
61
54
|
I18n.stubs(:t).returns(['a', 'b'])
|
62
|
-
|
63
|
-
assert_equal ['a', 'b'],
|
55
|
+
MongoUserWithout.has_constant :titles
|
56
|
+
assert_equal ['a', 'b'], MongoUserWithout.titles
|
64
57
|
end
|
65
58
|
|
66
59
|
should 'add index when index option is supplied' do
|
67
|
-
|
68
|
-
|
69
|
-
assert
|
60
|
+
MongoUserWithout.has_constant :salutations, ['Mr', 'Mrs'], :index => true
|
61
|
+
MongoUserWithout.create_indexes
|
62
|
+
assert MongoUserWithout.index_information.keys.any? { |key| key.match(/salutation/) }
|
70
63
|
end
|
71
64
|
|
72
65
|
should 'not index when index option is not supplied' do
|
73
|
-
|
74
|
-
assert !
|
66
|
+
MongoUser.create_indexes
|
67
|
+
assert !MongoUser.index_information.keys.any? { |key| key.match(/salutation/) }
|
75
68
|
end
|
76
69
|
|
77
70
|
should 'save values as integers' do
|
78
|
-
MongoUser.has_constant :salutations, %w(Mr Mrs)
|
79
71
|
m = MongoUser.new(:salutation => 'Mr')
|
80
72
|
m.save!
|
81
73
|
assert_equal 'Mr', m.salutation
|
@@ -83,29 +75,25 @@ class MongoidTest < Test::Unit::TestCase
|
|
83
75
|
end
|
84
76
|
|
85
77
|
should 'not be valid when an incorrect value is supplied' do
|
86
|
-
MongoUser.has_constant :salutations, %w(Mr Mrs)
|
87
78
|
m = MongoUser.new(:salutation => 'asefe')
|
88
79
|
assert !m.valid?
|
89
80
|
assert_equal ['must be one of Mr, Mrs'], m.errors[:salutation]
|
90
81
|
end
|
91
82
|
|
92
83
|
should 'not be valid with an incorrect value is supplied and a proc/lambda has been used' do
|
93
|
-
|
94
|
-
m = MongoUser.new(:salutation => 'asefe')
|
84
|
+
m = MongoUserWithProc.new(:salutation => 'asefe')
|
95
85
|
assert !m.valid?
|
96
86
|
assert_equal ['must be one of Mr, Mrs'], m.errors[:salutation]
|
97
87
|
end
|
98
88
|
|
99
89
|
should 'be valid when a blank value is supplied' do
|
100
|
-
|
101
|
-
m = MongoUser.new(:salutation => '')
|
90
|
+
m = MongoUserWithProc.new(:salutation => '')
|
102
91
|
assert m.valid?
|
103
92
|
end
|
104
93
|
end
|
105
94
|
|
106
95
|
context 'Named Scopes' do
|
107
96
|
setup do
|
108
|
-
MongoUser.has_constant :salutations, %w(Mr Mrs)
|
109
97
|
@man = MongoUser.create!(:salutation => 'Mr')
|
110
98
|
@woman = MongoUser.create!(:salutation => 'Mrs')
|
111
99
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_constant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 2
|
10
|
+
version: 0.6.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- mattbeedle
|