has_constant 0.4.4 → 0.4.5

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 CHANGED
@@ -1 +1 @@
1
- 0.4.4
1
+ 0.4.5
data/has_constant.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_constant}
8
- s.version = "0.4.4"
8
+ s.version = "0.4.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mattbeedle"]
@@ -21,9 +21,13 @@ module HasConstant
21
21
 
22
22
  singular = (options[:accessor] || name.to_s.singularize).to_s
23
23
 
24
- # Add the getter method. This returns the string representation of the stored value
25
- define_method(singular) do
26
- eval("#{self.class}.#{name.to_s}[self.attributes[singular].to_i] if self.attributes[singular]")
24
+ class_eval do
25
+ field singular.to_sym, { :type => Integer }.merge(options)
26
+
27
+ index singular.to_sym if options[:index]
28
+
29
+ named_scope :by_constant, lambda { |constant,value| { :where =>
30
+ { constant.to_sym => eval("#{self.to_s}.#{constant.pluralize}.index(value)") } } }
27
31
  end
28
32
 
29
33
  define_method("#{singular}=") do |val|
@@ -40,6 +44,11 @@ module HasConstant
40
44
  end
41
45
  end
42
46
 
47
+ # Add the getter method. This returns the string representation of the stored value
48
+ define_method(singular) do
49
+ eval("#{self.class}.#{name.to_s}[self.attributes[singular].to_i] if self.attributes[singular]")
50
+ end
51
+
43
52
  (class << self; self; end).instance_eval do
44
53
  define_method "#{singular}_is".to_sym do |values|
45
54
  values = values.lines.to_a if values.respond_to?(:lines)
@@ -51,11 +60,6 @@ module HasConstant
51
60
  where(singular.to_sym => { '$nin' => values.map { |v| self.send(name.to_sym).index(v) } })
52
61
  end
53
62
  end
54
-
55
- class_eval do
56
- named_scope :by_constant, lambda { |constant,value| { :where =>
57
- { constant.to_sym => eval("#{self.to_s}.#{constant.pluralize}.index(value)") } } }
58
- end
59
63
  end
60
64
  end
61
65
  end
@@ -7,8 +7,6 @@ class MongoUser
7
7
  include HasConstant
8
8
  include HasConstant::Orm::Mongoid
9
9
 
10
- field :salutation, :type => Integer
11
-
12
10
  has_constant :salutations, ['Mr', 'Mrs']
13
11
  end if defined?(Mongoid)
14
12
 
@@ -17,13 +15,32 @@ class MongoUserWithProc
17
15
  include HasConstant
18
16
  include HasConstant::Orm::Mongoid
19
17
 
20
- field :salutation, :type => Integer
21
-
22
18
  has_constant :salutations, lambda { ['Mr', 'Mrs'] }
23
19
  end if defined?(Mongoid)
24
20
 
21
+ class MongoUserWithout
22
+ include Mongoid::Document
23
+ include HasConstant
24
+ include HasConstant::Orm::Mongoid
25
+ end
26
+
25
27
  class MongoidTest < Test::Unit::TestCase
26
28
  context 'Instance' do
29
+ should 'add the field automatically' do
30
+ MongoUserWithout.has_constant :salutations, ['Mr', 'Mrs']
31
+ assert MongoUserWithout.fields.map(&:first).include?('salutation')
32
+ end
33
+
34
+ should 'take the accessor into account when adding the field' do
35
+ MongoUserWithout.has_constant :salutations, ['Mr', 'Mrs'], :accessor => :sal
36
+ assert MongoUserWithout.fields.map(&:first).include?('sal')
37
+ end
38
+
39
+ should 'add index when index option is supplied' do
40
+ MongoUserWithout.has_constant :salutations, ['Mr', 'Mrs'], :index => true
41
+ assert MongoUserWithout.index_information.keys.any? { |k| k.match(/salutation/) }
42
+ end
43
+
27
44
  should 'save values as integers' do
28
45
  m = MongoUser.new(:salutation => 'Mr')
29
46
  m.save!
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: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 4
10
- version: 0.4.4
9
+ - 5
10
+ version: 0.4.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - mattbeedle