has_constant 0.8.0 → 0.8.1

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.8.0
1
+ 0.8.1
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.0"
8
+ s.version = "0.8.1"
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-03-28}
12
+ s.date = %q{2011-04-24}
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 = [
@@ -40,15 +40,25 @@ module HasConstant
40
40
 
41
41
  index singular.to_sym, :background => true if options[:index]
42
42
 
43
- named_scope :by_constant, lambda { |constant,value| { :where =>
44
- { constant.to_sym => eval("#{self.to_s}.#{constant.pluralize}.index(value)") } } }
43
+ named_scope :by_constant, lambda { |constant, value|
44
+ if self.send(constant.pluralize).respond_to?(:key)
45
+ value_for_query = self.send(constant.pluralize).key(value)
46
+ else
47
+ value_for_query = self.send(constant.pluralize).index(value)
48
+ end
49
+ where(constant.to_sym => value_for_query)
50
+ }
45
51
  end
46
52
 
47
53
  # Define the setter method here
48
54
  if options[:as] == :array
49
55
  define_method("#{plural}=") do |value_set|
50
- indexes = value_set.map do |value|
51
- self.class.send(plural).index(value)
56
+ indexes = (value_set.blank? ? [] : value_set).map do |value|
57
+ if self.class.send(plural).respond_to?(:key)
58
+ self.class.send(plural).key(value)
59
+ else
60
+ self.class.send(plural).index(value)
61
+ end
52
62
  end
53
63
  write_attribute plural, indexes
54
64
  end
@@ -93,9 +103,17 @@ module HasConstant
93
103
  if options[:as] == :array
94
104
  define_method "#{plural}_include".to_sym do |value|
95
105
  if value.is_a?(String)
96
- where(plural.to_sym => values.index(value))
106
+ if values.respond_to?(:key)
107
+ where(plural.to_sym => values.key(value))
108
+ else
109
+ where(plural.to_sym => values.index(value))
110
+ end
97
111
  else
98
- where(plural.to_sym => value.map { |v| send(plural).index(v) })
112
+ if values.respond_to?(:key)
113
+ where(plural.to_sym => value.map { |v| send(plural).key(v) })
114
+ else
115
+ where(plural.to_sym => value.map { |v| send(plural).index(v) })
116
+ end
99
117
  end
100
118
  end
101
119
  else
@@ -113,7 +131,12 @@ module HasConstant
113
131
 
114
132
  define_method "#{singular}_is_not".to_sym do |values|
115
133
  values = values.lines.to_a if values.respond_to?(:lines)
116
- where(singular.to_sym.nin => values.map { |v| self.send(name.to_sym).index(v) })
134
+ if self.send(plural).respond_to?(:key)
135
+ values_for_query = values.map { |v| self.send(plural).key(v) }
136
+ else
137
+ values_for_query = values.map { |v| self.send(plural).index(v) }
138
+ end
139
+ where(singular.to_sym.nin => values_for_query)
117
140
  end
118
141
  end
119
142
  end
@@ -35,6 +35,7 @@ class Thing
35
35
  end
36
36
 
37
37
  class MongoidTest < Test::Unit::TestCase
38
+
38
39
  context 'Instance' do
39
40
  context 'when storing arrays' do
40
41
  setup do
@@ -90,6 +91,13 @@ class MongoidTest < Test::Unit::TestCase
90
91
  should 'return the correct value' do
91
92
  assert_equal 'Mrs', @u.reload.salutation
92
93
  end
94
+
95
+ context '#update_attribute' do
96
+ should 'store the correct value' do
97
+ @u.update_attribute :salutation, 'Mr'
98
+ assert_equal 'Mr', @u.reload.salutation
99
+ end
100
+ end
93
101
  end
94
102
 
95
103
  should 'add the field automatically' do
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: 63
4
+ hash: 61
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 0
10
- version: 0.8.0
9
+ - 1
10
+ version: 0.8.1
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-03-28 00:00:00 +02:00
18
+ date: 2011-04-24 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency