has_constant 0.2.2 → 0.2.4

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.2.2
1
+ 0.2.4
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.2.2"
8
+ s.version = "0.2.4"
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-07-26}
12
+ s.date = %q{2010-08-14}
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 = [
@@ -3,12 +3,26 @@ module HasConstant
3
3
  module Mongoid
4
4
  def self.included( base )
5
5
  base.extend(ClassMethods)
6
+ base.send(:include, InstanceMethods)
7
+ base.class_eval do
8
+ validate :validate_has_constant_attributes
9
+ end
10
+ end
11
+
12
+ module InstanceMethods
13
+ def validate_has_constant_attributes
14
+ @has_constant_errors.each do |key, value|
15
+ self.errors.add key, value
16
+ end if @has_constant_errors
17
+ end
6
18
  end
7
19
 
8
20
  module ClassMethods
9
21
  def has_constant( name, values, options = {} )
10
22
  super(name, values, options)
11
23
 
24
+ values = values.call if values.respond_to?(:call)
25
+
12
26
  singular = (options[:accessor] || name.to_s.singularize).to_s
13
27
 
14
28
  # Add the getter method. This returns the string representation of the stored value
@@ -18,7 +32,12 @@ module HasConstant
18
32
 
19
33
  define_method("#{singular}=") do |val|
20
34
  if val.instance_of?(String)
21
- write_attribute singular.to_sym, self.class.send(name.to_s).index(val)
35
+ if index = self.class.send(name.to_s).index(val)
36
+ write_attribute singular.to_sym, index
37
+ else
38
+ @has_constant_errors ||= {}
39
+ @has_constant_errors.merge!(singular.to_sym => "must be one of #{values.join(', ')}")
40
+ end
22
41
  else
23
42
  write_attribute singular.to_sym, val
24
43
  end
data/lib/has_constant.rb CHANGED
@@ -47,7 +47,11 @@ module HasConstant
47
47
  # Add the setter method. This takes the string representation and converts it to an integer to store in the DB
48
48
  define_method("#{singular}=") do |val|
49
49
  if val.instance_of?(String)
50
- instance_variable_set("@#{singular}", values.index(val))
50
+ if values.index(val)
51
+ instance_variable_set("@#{singular}", values.index(val))
52
+ else
53
+ raise ArgumentError, "value for #{singular} must be in #{self.class.send(name.to_s).join(', ')}"
54
+ end
51
55
  else
52
56
  instance_variable_set("@#{singular}", val)
53
57
  end
@@ -13,6 +13,14 @@ class TestHasConstant < Test::Unit::TestCase
13
13
  assert Model.new.respond_to?(:title=)
14
14
  end
15
15
 
16
+ should 'raise an exception when a value is provided which is not in the list' do
17
+ Model.has_constant :titles, ['Mr', 'Mrs']
18
+ m = Model.new
19
+ assert_raise ArgumentError do
20
+ m.title = 'Ms'
21
+ end
22
+ end
23
+
16
24
  should 'be able to override accessor' do
17
25
  Model.has_constant :titles, ['Mr', 'Mrs'], :accessor => :salutation
18
26
  m = Model.new
@@ -12,15 +12,39 @@ class MongoUser
12
12
  has_constant :salutations, ['Mr', 'Mrs']
13
13
  end if defined?(Mongoid)
14
14
 
15
+ class MongoUserWithProc
16
+ include Mongoid::Document
17
+ include HasConstant
18
+ include HasConstant::Orm::Mongoid
19
+
20
+ field :salutation, :type => Integer
21
+
22
+ has_constant :salutations, lambda { ['Mr', 'Mrs'] }
23
+ end if defined?(Mongoid)
24
+
15
25
  class MongoidTest < Test::Unit::TestCase
16
- should 'save values as integers' do
17
- m = MongoUser.new(:salutation => 'Mr')
18
- m.save!
19
- assert_equal 'Mr', m.salutation
20
- assert_equal 0, m.attributes['salutation']
26
+ context 'Instance' do
27
+ should 'save values as integers' do
28
+ m = MongoUser.new(:salutation => 'Mr')
29
+ m.save!
30
+ assert_equal 'Mr', m.salutation
31
+ assert_equal 0, m.attributes['salutation']
32
+ end
33
+
34
+ should 'not be valid when an incorrect value is supplied' do
35
+ m = MongoUser.new(:salutation => 'asefe')
36
+ assert !m.valid?
37
+ assert_equal ['must be one of Mr, Mrs'], m.errors[:salutation]
38
+ end
39
+
40
+ should 'not be valid with an incorrect value is supplied and a proc/lambda has been used' do
41
+ m = MongoUserWithProc.new(:salutation => 'asefe')
42
+ assert !m.valid?
43
+ assert_equal ['must be one of Mr, Mrs'], m.errors[:salutation]
44
+ end
21
45
  end
22
46
 
23
- context 'scopes' do
47
+ context 'Named Scopes' do
24
48
  setup do
25
49
  @man = MongoUser.create!(:salutation => 'Mr')
26
50
  @woman = MongoUser.create!(:salutation => 'Mrs')
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: 19
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 4
10
+ version: 0.2.4
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: 2010-07-26 00:00:00 +02:00
18
+ date: 2010-08-14 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency