has_constant 0.6.2 → 0.7.0
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 +3 -4
- data/lib/has_constant/orm/mongoid.rb +46 -19
- data/test/unit/orm/mongoid_test.rb +60 -0
- metadata +18 -18
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.7.0
|
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.
|
8
|
+
s.version = "0.7.0"
|
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-02-
|
12
|
+
s.date = %q{2011-02-26}
|
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 = [
|
@@ -36,7 +36,7 @@ Gem::Specification.new do |s|
|
|
36
36
|
]
|
37
37
|
s.homepage = %q{http://github.com/mattbeedle/has_constant}
|
38
38
|
s.require_paths = ["lib"]
|
39
|
-
s.rubygems_version = %q{1.
|
39
|
+
s.rubygems_version = %q{1.5.2}
|
40
40
|
s.summary = %q{Allows certain fields to be limited to a set of values}
|
41
41
|
s.test_files = [
|
42
42
|
"test/has_constant_test.rb",
|
@@ -46,7 +46,6 @@ Gem::Specification.new do |s|
|
|
46
46
|
]
|
47
47
|
|
48
48
|
if s.respond_to? :specification_version then
|
49
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
49
|
s.specification_version = 3
|
51
50
|
|
52
51
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
@@ -20,6 +20,7 @@ module HasConstant
|
|
20
20
|
super(name, values, options)
|
21
21
|
|
22
22
|
singular = (options[:accessor] || name.to_s.singularize).to_s
|
23
|
+
plural = (options[:accessor] || name.to_s)
|
23
24
|
|
24
25
|
class_eval do
|
25
26
|
unless fields.map(&:first).include?(singular.to_s)
|
@@ -32,34 +33,60 @@ module HasConstant
|
|
32
33
|
{ constant.to_sym => eval("#{self.to_s}.#{constant.pluralize}.index(value)") } } }
|
33
34
|
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
# Define the setter method here
|
37
|
+
if options[:as] == :array
|
38
|
+
define_method("#{plural}=") do |value_set|
|
39
|
+
indexes = value_set.map do |value|
|
40
|
+
values.index(value)
|
41
|
+
end
|
42
|
+
write_attribute plural, indexes
|
43
|
+
end
|
44
|
+
else
|
45
|
+
define_method("#{singular}=") do |val|
|
46
|
+
if val.instance_of?(String)
|
47
|
+
if index = self.class.send(name.to_s).index(val)
|
48
|
+
write_attribute singular.to_sym, index
|
49
|
+
elsif !val.blank?
|
50
|
+
values = values.call if values.respond_to?(:call)
|
51
|
+
@has_constant_errors ||= {}
|
52
|
+
@has_constant_errors.merge!(singular.to_sym => "must be one of #{values.join(', ')}")
|
53
|
+
end
|
54
|
+
else
|
55
|
+
write_attribute singular.to_sym, val
|
43
56
|
end
|
44
|
-
else
|
45
|
-
write_attribute singular.to_sym, val
|
46
57
|
end
|
47
58
|
end
|
48
59
|
|
49
60
|
# Add the getter method. This returns the string representation of the stored value
|
50
|
-
|
51
|
-
|
61
|
+
if options[:as] == :array
|
62
|
+
define_method(plural) do
|
63
|
+
attributes[plural].map { |val| values[val] }
|
64
|
+
end
|
65
|
+
else
|
66
|
+
define_method(singular) do
|
67
|
+
eval("#{self.class}.#{name.to_s}[self.attributes[singular].to_i] if self.attributes[singular]")
|
68
|
+
end
|
52
69
|
end
|
53
70
|
|
54
71
|
(class << self; self; end).instance_eval do
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
72
|
+
if options[:as] == :array
|
73
|
+
define_method "#{plural}_include".to_sym do |value|
|
74
|
+
if value.is_a?(String)
|
75
|
+
where(plural.to_sym => values.index(value))
|
76
|
+
else
|
77
|
+
where(plural.to_sym => value.map { |v| send(plural).index(v) })
|
78
|
+
end
|
79
|
+
end
|
80
|
+
else
|
81
|
+
define_method "#{singular}_is".to_sym do |values|
|
82
|
+
values = values.lines.to_a if values.respond_to?(:lines)
|
83
|
+
where(singular.to_sym.in => values.map { |v| self.send(name.to_sym).index(v) })
|
84
|
+
end
|
59
85
|
|
60
|
-
|
61
|
-
|
62
|
-
|
86
|
+
define_method "#{singular}_is_not".to_sym do |values|
|
87
|
+
values = values.lines.to_a if values.respond_to?(:lines)
|
88
|
+
where(singular.to_sym.nin => values.map { |v| self.send(name.to_sym).index(v) })
|
89
|
+
end
|
63
90
|
end
|
64
91
|
end
|
65
92
|
end
|
@@ -27,8 +27,68 @@ class AnotherUser
|
|
27
27
|
include HasConstant
|
28
28
|
end
|
29
29
|
|
30
|
+
class Thing
|
31
|
+
include Mongoid::Document
|
32
|
+
include HasConstant
|
33
|
+
end
|
34
|
+
|
30
35
|
class MongoidTest < Test::Unit::TestCase
|
31
36
|
context 'Instance' do
|
37
|
+
context 'when storing arrays' do
|
38
|
+
setup do
|
39
|
+
MongoUserWithout.has_constant :sports, %w(running cycling tennis),
|
40
|
+
:as => :array
|
41
|
+
@user = MongoUserWithout.new :sports => %w(running tennis)
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'setter' do
|
45
|
+
should 'take an array' do
|
46
|
+
assert_equal [0,2], @user.attributes['sports']
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'getter' do
|
51
|
+
should 'return array of strings' do
|
52
|
+
assert_equal %w(running tennis), @user.sports
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'named scopes' do
|
57
|
+
setup do
|
58
|
+
@u = MongoUserWithout.create! :sports => %w(running cycling)
|
59
|
+
@u2 = MongoUserWithout.create! :sports => %w(running tennis)
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'includes scope' do
|
63
|
+
should 'return all where one of the array items is matched' do
|
64
|
+
assert_equal 1, MongoUserWithout.sports_include('cycling').count
|
65
|
+
assert MongoUserWithout.sports_include('cycling').include?(@u)
|
66
|
+
end
|
67
|
+
|
68
|
+
should 'work with array arguement' do
|
69
|
+
result = MongoUserWithout.sports_include(%w(running tennis))
|
70
|
+
assert_equal 1, result.count
|
71
|
+
assert result.include?(@u2)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'using a hash' do
|
78
|
+
setup do
|
79
|
+
Thing.has_constant :salutations, { :first => 'Mr', :second => 'Mrs' }
|
80
|
+
@u = Thing.new :salutation => 'Mrs'
|
81
|
+
end
|
82
|
+
|
83
|
+
should 'store the hash key' do
|
84
|
+
assert_equal 'second', @u.attributes['salutation']
|
85
|
+
end
|
86
|
+
|
87
|
+
should 'return the correct value' do
|
88
|
+
assert_equal 'Mrs', @u.salutation
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
32
92
|
should 'add the field automatically' do
|
33
93
|
MongoUserWithout.has_constant :salutations, ['Mr', 'Mrs']
|
34
94
|
assert MongoUserWithout.fields.map(&:first).include?('salutation')
|
metadata
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
name: has_constant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
hash: 3
|
5
|
-
prerelease:
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 0.7.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- mattbeedle
|
@@ -15,14 +15,12 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-26 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
type: :development
|
23
|
-
|
24
|
-
name: jeweler
|
25
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
24
|
none: false
|
27
25
|
requirements:
|
28
26
|
- - ">="
|
@@ -31,12 +29,12 @@ dependencies:
|
|
31
29
|
segments:
|
32
30
|
- 0
|
33
31
|
version: "0"
|
34
|
-
|
32
|
+
name: jeweler
|
33
|
+
version_requirements: *id001
|
34
|
+
prerelease: false
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
type: :development
|
37
|
-
|
38
|
-
name: shoulda
|
39
|
-
version_requirements: &id002 !ruby/object:Gem::Requirement
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
38
|
none: false
|
41
39
|
requirements:
|
42
40
|
- - ">="
|
@@ -45,12 +43,12 @@ dependencies:
|
|
45
43
|
segments:
|
46
44
|
- 0
|
47
45
|
version: "0"
|
48
|
-
|
46
|
+
name: shoulda
|
47
|
+
version_requirements: *id002
|
48
|
+
prerelease: false
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
type: :development
|
51
|
-
|
52
|
-
name: activesupport
|
53
|
-
version_requirements: &id003 !ruby/object:Gem::Requirement
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
52
|
none: false
|
55
53
|
requirements:
|
56
54
|
- - ">="
|
@@ -59,7 +57,9 @@ dependencies:
|
|
59
57
|
segments:
|
60
58
|
- 0
|
61
59
|
version: "0"
|
62
|
-
|
60
|
+
name: activesupport
|
61
|
+
version_requirements: *id003
|
62
|
+
prerelease: false
|
63
63
|
description: Allows certain fields to be limited to a set of values
|
64
64
|
email: mattbeedle@gmail.com
|
65
65
|
executables: []
|
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
116
|
requirements: []
|
117
117
|
|
118
118
|
rubyforge_project:
|
119
|
-
rubygems_version: 1.
|
119
|
+
rubygems_version: 1.5.2
|
120
120
|
signing_key:
|
121
121
|
specification_version: 3
|
122
122
|
summary: Allows certain fields to be limited to a set of values
|