has_constant 0.2.9 → 0.3.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/Gemfile +8 -0
- data/Gemfile.lock +48 -0
- data/VERSION +1 -1
- data/has_constant.gemspec +25 -23
- data/lib/has_constant/orm/active_record.rb +2 -4
- data/lib/has_constant/orm/mongoid.rb +11 -13
- data/lib/has_constant.rb +17 -7
- data/test/has_constant_test.rb +63 -7
- metadata +29 -15
- data/.gitignore +0 -22
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.3)
|
5
|
+
activesupport (= 3.0.3)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4)
|
8
|
+
activerecord (3.0.3)
|
9
|
+
activemodel (= 3.0.3)
|
10
|
+
activesupport (= 3.0.3)
|
11
|
+
arel (~> 2.0.2)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.3)
|
14
|
+
arel (2.0.6)
|
15
|
+
bson (1.1.5)
|
16
|
+
bson_ext (1.1.5)
|
17
|
+
builder (2.1.2)
|
18
|
+
git (1.2.5)
|
19
|
+
i18n (0.5.0)
|
20
|
+
jeweler (1.5.1)
|
21
|
+
bundler (~> 1.0.0)
|
22
|
+
git (>= 1.2.5)
|
23
|
+
rake
|
24
|
+
mongo (1.1.5)
|
25
|
+
bson (>= 1.1.5)
|
26
|
+
mongoid (2.0.0.beta.20)
|
27
|
+
activemodel (~> 3.0)
|
28
|
+
mongo (~> 1.1)
|
29
|
+
tzinfo (~> 0.3.22)
|
30
|
+
will_paginate (~> 3.0.pre)
|
31
|
+
rake (0.8.7)
|
32
|
+
shoulda (2.11.3)
|
33
|
+
sqlite3-ruby (1.3.2)
|
34
|
+
tzinfo (0.3.23)
|
35
|
+
will_paginate (3.0.pre2)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
activerecord
|
42
|
+
activesupport
|
43
|
+
bson
|
44
|
+
bson_ext
|
45
|
+
jeweler
|
46
|
+
mongoid (= 2.0.0.beta.20)
|
47
|
+
shoulda
|
48
|
+
sqlite3-ruby
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/has_constant.gemspec
CHANGED
@@ -1,49 +1,48 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{has_constant}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.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{2010-
|
12
|
+
s.date = %q{2010-12-19}
|
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 = [
|
16
16
|
"LICENSE",
|
17
|
-
|
17
|
+
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
21
|
+
".rvmrc",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"has_constant.gemspec",
|
29
|
+
"lib/has_constant.rb",
|
30
|
+
"lib/has_constant/orm/active_record.rb",
|
31
|
+
"lib/has_constant/orm/mongoid.rb",
|
32
|
+
"test/has_constant_test.rb",
|
33
|
+
"test/helper.rb",
|
34
|
+
"test/unit/orm/active_record_test.rb",
|
35
|
+
"test/unit/orm/mongoid_test.rb"
|
36
36
|
]
|
37
37
|
s.homepage = %q{http://github.com/mattbeedle/has_constant}
|
38
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
39
38
|
s.require_paths = ["lib"]
|
40
39
|
s.rubygems_version = %q{1.3.7}
|
41
40
|
s.summary = %q{Allows certain fields to be limited to a set of values}
|
42
41
|
s.test_files = [
|
43
42
|
"test/has_constant_test.rb",
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
"test/helper.rb",
|
44
|
+
"test/unit/orm/active_record_test.rb",
|
45
|
+
"test/unit/orm/mongoid_test.rb"
|
47
46
|
]
|
48
47
|
|
49
48
|
if s.respond_to? :specification_version then
|
@@ -51,13 +50,16 @@ Gem::Specification.new do |s|
|
|
51
50
|
s.specification_version = 3
|
52
51
|
|
53
52
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
54
54
|
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
55
55
|
s.add_development_dependency(%q<activesupport>, [">= 0"])
|
56
56
|
else
|
57
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
57
58
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
58
59
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
59
60
|
end
|
60
61
|
else
|
62
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
61
63
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
62
64
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
63
65
|
end
|
@@ -1,9 +1,7 @@
|
|
1
1
|
module HasConstant
|
2
2
|
module Orm
|
3
3
|
module ActiveRecord
|
4
|
-
|
5
|
-
base.extend(ClassMethods)
|
6
|
-
end
|
4
|
+
extend ActiveSupport::Concern
|
7
5
|
|
8
6
|
module ClassMethods
|
9
7
|
def has_constant( name, values, options = {} )
|
@@ -43,4 +41,4 @@ module HasConstant
|
|
43
41
|
end
|
44
42
|
end
|
45
43
|
end
|
46
|
-
end
|
44
|
+
end if defined?(ActiveRecord::Base)
|
@@ -1,20 +1,18 @@
|
|
1
1
|
module HasConstant
|
2
2
|
module Orm
|
3
3
|
module Mongoid
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
class_eval do
|
8
8
|
validate :validate_has_constant_attributes
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end if @has_constant_errors
|
17
|
-
end
|
12
|
+
def validate_has_constant_attributes
|
13
|
+
@has_constant_errors.each do |key, value|
|
14
|
+
self.errors.add key, value
|
15
|
+
end if @has_constant_errors
|
18
16
|
end
|
19
17
|
|
20
18
|
module ClassMethods
|
@@ -41,13 +39,13 @@ module HasConstant
|
|
41
39
|
write_attribute singular.to_sym, val
|
42
40
|
end
|
43
41
|
end
|
44
|
-
|
42
|
+
|
45
43
|
(class << self; self; end).instance_eval do
|
46
44
|
define_method "#{singular}_is".to_sym do |values|
|
47
45
|
values = values.lines.to_a if values.respond_to?(:lines)
|
48
46
|
where(singular.to_sym => { '$in' => values.map { |v| self.send(name.to_sym).index(v) } })
|
49
47
|
end
|
50
|
-
|
48
|
+
|
51
49
|
define_method "#{singular}_is_not".to_sym do |values|
|
52
50
|
values = values.lines.to_a if values.respond_to?(:lines)
|
53
51
|
where(singular.to_sym => { '$nin' => values.map { |v| self.send(name.to_sym).index(v) } })
|
@@ -62,4 +60,4 @@ module HasConstant
|
|
62
60
|
end
|
63
61
|
end
|
64
62
|
end
|
65
|
-
end
|
63
|
+
end if defined?(Mongoid)
|
data/lib/has_constant.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
require '
|
2
|
-
require 'has_constant/orm/
|
1
|
+
require 'active_support'
|
2
|
+
require 'has_constant/orm/mongoid' if defined?(Mongoid)
|
3
|
+
require 'has_constant/orm/active_record' if defined?(ActiveRecord::Base)
|
3
4
|
require 'active_support/inflector'
|
4
5
|
module HasConstant
|
5
|
-
|
6
|
-
def self.included(base)
|
7
|
-
base.extend(ClassMethods)
|
8
|
-
end
|
6
|
+
extend ActiveSupport::Concern
|
9
7
|
|
10
8
|
# HasConstant takes a Proc containing an array of possible values for a field name
|
11
9
|
# The field name is inferred as the singular of the has constant name. For example
|
@@ -58,7 +56,19 @@ module HasConstant
|
|
58
56
|
end
|
59
57
|
|
60
58
|
define_method("#{singular}_is?") do |value|
|
61
|
-
|
59
|
+
send(singular) == value.to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
define_method("#{singular}_is_not?") do |value|
|
63
|
+
!send("#{singular}_is?", value)
|
64
|
+
end
|
65
|
+
|
66
|
+
define_method("#{singular}_in?") do |value_list|
|
67
|
+
value_list.include? send(singular)
|
68
|
+
end
|
69
|
+
|
70
|
+
define_method("#{singular}_not_in?") do |value_list|
|
71
|
+
!send("#{singular}_in?", value_list)
|
62
72
|
end
|
63
73
|
end
|
64
74
|
end
|
data/test/has_constant_test.rb
CHANGED
@@ -2,8 +2,6 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class Model
|
4
4
|
include HasConstant
|
5
|
-
|
6
|
-
attr_accessor :salutation
|
7
5
|
end
|
8
6
|
|
9
7
|
class TestHasConstant < Test::Unit::TestCase
|
@@ -22,6 +20,7 @@ class TestHasConstant < Test::Unit::TestCase
|
|
22
20
|
end
|
23
21
|
|
24
22
|
should 'be able to override accessor' do
|
23
|
+
Model.send(:attr_accessor, :salutation)
|
25
24
|
Model.has_constant :titles, ['Mr', 'Mrs'], :accessor => :salutation
|
26
25
|
m = Model.new
|
27
26
|
m.salutation = 'Mr'
|
@@ -43,10 +42,67 @@ class TestHasConstant < Test::Unit::TestCase
|
|
43
42
|
assert_equal ['Mr', 'Mrs'], Model.titles
|
44
43
|
end
|
45
44
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
context '#field_is?' do
|
46
|
+
setup do
|
47
|
+
Model.has_constant :titles, ['Mr', 'Mrs', 'Ms']
|
48
|
+
@m = Model.new
|
49
|
+
@m.title = 'Mr'
|
50
|
+
end
|
51
|
+
|
52
|
+
should 'be true when the value is equal to the supplied one' do
|
53
|
+
assert @m.title_is?('Mr')
|
54
|
+
end
|
55
|
+
|
56
|
+
should 'be false when the value is not equal to the supplied one' do
|
57
|
+
assert !@m.title_is?('Mrs')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context '#field_in?' do
|
62
|
+
setup do
|
63
|
+
Model.has_constant :titles, ['Mr', 'Mrs', 'Ms']
|
64
|
+
@m = Model.new
|
65
|
+
@m.title = 'Mr'
|
66
|
+
end
|
67
|
+
|
68
|
+
should 'be true when the field value is in the supplied set of values' do
|
69
|
+
assert @m.title_in?(['Mr', 'Ms'])
|
70
|
+
end
|
71
|
+
|
72
|
+
should 'be false when the field value is not in the supplied set of values' do
|
73
|
+
assert !@m.title_in?(['Mrs', 'Ms'])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context '#field_is_not?' do
|
78
|
+
setup do
|
79
|
+
Model.has_constant :titles, ['Mr', 'Mrs', 'Ms']
|
80
|
+
@m = Model.new
|
81
|
+
@m.title = 'Mr'
|
82
|
+
end
|
83
|
+
|
84
|
+
should 'be true when the field value is not equal to the supplied one' do
|
85
|
+
assert @m.title_is_not?('Mrs')
|
86
|
+
end
|
87
|
+
|
88
|
+
should 'be false when the field value is equal to the supplied one' do
|
89
|
+
assert !@m.title_is_not?('Mr')
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context '#field_not_in?' do
|
94
|
+
setup do
|
95
|
+
Model.has_constant :titles, ['Mr', 'Mrs', 'Ms']
|
96
|
+
@m = Model.new
|
97
|
+
@m.title = 'Mr'
|
98
|
+
end
|
99
|
+
|
100
|
+
should 'be true when the field value is not in the supplied list' do
|
101
|
+
assert @m.title_not_in?(['Mrs', 'Ms'])
|
102
|
+
end
|
103
|
+
|
104
|
+
should 'be false when the field value is in the supplied list' do
|
105
|
+
assert !@m.title_not_in?(['Mr', 'Mrs'])
|
106
|
+
end
|
51
107
|
end
|
52
108
|
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: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- mattbeedle
|
@@ -15,13 +15,14 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-12-19 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
22
|
+
type: :development
|
23
23
|
prerelease: false
|
24
|
-
|
24
|
+
name: jeweler
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
26
|
none: false
|
26
27
|
requirements:
|
27
28
|
- - ">="
|
@@ -30,12 +31,12 @@ dependencies:
|
|
30
31
|
segments:
|
31
32
|
- 0
|
32
33
|
version: "0"
|
33
|
-
|
34
|
-
version_requirements: *id001
|
34
|
+
requirement: *id001
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
|
-
|
36
|
+
type: :development
|
37
37
|
prerelease: false
|
38
|
-
|
38
|
+
name: shoulda
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
40
|
none: false
|
40
41
|
requirements:
|
41
42
|
- - ">="
|
@@ -44,8 +45,21 @@ dependencies:
|
|
44
45
|
segments:
|
45
46
|
- 0
|
46
47
|
version: "0"
|
48
|
+
requirement: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
47
50
|
type: :development
|
48
|
-
|
51
|
+
prerelease: false
|
52
|
+
name: activesupport
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirement: *id003
|
49
63
|
description: Allows certain fields to be limited to a set of values
|
50
64
|
email: mattbeedle@gmail.com
|
51
65
|
executables: []
|
@@ -57,9 +71,9 @@ extra_rdoc_files:
|
|
57
71
|
- README.rdoc
|
58
72
|
files:
|
59
73
|
- .document
|
60
|
-
- .gitignore
|
61
74
|
- .rvmrc
|
62
75
|
- Gemfile
|
76
|
+
- Gemfile.lock
|
63
77
|
- LICENSE
|
64
78
|
- README.rdoc
|
65
79
|
- Rakefile
|
@@ -77,8 +91,8 @@ homepage: http://github.com/mattbeedle/has_constant
|
|
77
91
|
licenses: []
|
78
92
|
|
79
93
|
post_install_message:
|
80
|
-
rdoc_options:
|
81
|
-
|
94
|
+
rdoc_options: []
|
95
|
+
|
82
96
|
require_paths:
|
83
97
|
- lib
|
84
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|