has_constant 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/has_constant.gemspec +2 -2
- data/lib/has_constant/orm/mongoid.rb +19 -3
- data/test/unit/orm/mongoid_test.rb +5 -2
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.3
|
data/has_constant.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{has_constant}
|
8
|
-
s.version = "0.7.
|
8
|
+
s.version = "0.7.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["mattbeedle"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-03-23}
|
13
13
|
s.description = %q{Allows certain fields to be limited to a set of values}
|
14
14
|
s.email = %q{mattbeedle@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,7 +28,13 @@ module HasConstant
|
|
28
28
|
field plural.to_sym, { :type => Array, :default => [] }.
|
29
29
|
merge(options)
|
30
30
|
else
|
31
|
-
|
31
|
+
if values.is_a?(Hash) || values.respond_to?(:call) &&
|
32
|
+
values.call.is_a?(Hash)
|
33
|
+
type = String
|
34
|
+
else
|
35
|
+
type = Integer
|
36
|
+
end
|
37
|
+
field singular.to_sym, { :type => type }.merge(options)
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
@@ -50,7 +56,13 @@ module HasConstant
|
|
50
56
|
|
51
57
|
define_method("#{singular}=") do |val|
|
52
58
|
if val.instance_of?(String)
|
53
|
-
if
|
59
|
+
if self.class.send(name).respond_to?(:key)
|
60
|
+
index = self.class.send(name.to_s).key(val).to_s
|
61
|
+
elsif self.class.send(name).respond_to?(:index)
|
62
|
+
index = self.class.send(name.to_s).index(val)
|
63
|
+
index = index.to_s if self.class.send(name.to_s).is_a?(Hash)
|
64
|
+
end
|
65
|
+
if index
|
54
66
|
write_attribute singular.to_sym, index
|
55
67
|
elsif !val.blank?
|
56
68
|
values = values.call if values.respond_to?(:call)
|
@@ -70,7 +82,11 @@ module HasConstant
|
|
70
82
|
end
|
71
83
|
|
72
84
|
define_method(singular) do
|
73
|
-
|
85
|
+
if attributes[singular]
|
86
|
+
res = self.class.send(name.to_s)[attributes[singular].to_i] rescue nil
|
87
|
+
res ||= self.class.send(name.to_s)[attributes[singular]]
|
88
|
+
res ||= self.class.send(name.to_s)[attributes[singular].to_sym]
|
89
|
+
end
|
74
90
|
end
|
75
91
|
|
76
92
|
(class << self; self; end).instance_eval do
|
@@ -30,6 +30,8 @@ end
|
|
30
30
|
class Thing
|
31
31
|
include Mongoid::Document
|
32
32
|
include HasConstant
|
33
|
+
|
34
|
+
field :salutation, :type => String
|
33
35
|
end
|
34
36
|
|
35
37
|
class MongoidTest < Test::Unit::TestCase
|
@@ -78,14 +80,15 @@ class MongoidTest < Test::Unit::TestCase
|
|
78
80
|
setup do
|
79
81
|
Thing.has_constant :salutations, { :first => 'Mr', :second => 'Mrs' }
|
80
82
|
@u = Thing.new :salutation => 'Mrs'
|
83
|
+
@u.save!
|
81
84
|
end
|
82
85
|
|
83
86
|
should 'store the hash key' do
|
84
|
-
assert_equal 'second', @u.attributes['salutation']
|
87
|
+
assert_equal 'second', @u.reload.attributes['salutation']
|
85
88
|
end
|
86
89
|
|
87
90
|
should 'return the correct value' do
|
88
|
-
assert_equal 'Mrs', @u.salutation
|
91
|
+
assert_equal 'Mrs', @u.reload.salutation
|
89
92
|
end
|
90
93
|
end
|
91
94
|
|
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: 5
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 3
|
10
|
+
version: 0.7.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- mattbeedle
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-03-23 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|