mongoid-normalize-strings 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/mongoid-normalize-strings/normalize.rb +7 -1
- data/lib/mongoid-normalize-strings/version.rb +1 -1
- data/spec/normalize_spec.rb +38 -13
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b9be0339d3e3d5e725d876319b9f7e0edaea3fd
|
4
|
+
data.tar.gz: 747ecbfb2499bbf8f4c87e9855917170e365c185
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7aa5176a0c512f6ecb87bea7ba82c45d60fb4657b3d2656ab227ce6b597d30f7875dec3667c6cb6241efb6e87f2bd8747d9f051ddc4c9daa7c59edd04f1478a5
|
7
|
+
data.tar.gz: e172ac25a50d3dc73a457c5104e0c7d5120a2d9d36bbb2a4b1de8a8577edbb63443e234487d682d07adc86fc8fa9ef29fc46d66a678a677785d1b89553af8a6a
|
data/README.md
CHANGED
@@ -19,13 +19,17 @@ Or install it yourself as:
|
|
19
19
|
## Usage
|
20
20
|
|
21
21
|
```ruby
|
22
|
-
class
|
22
|
+
class City
|
23
23
|
include Mongoid::Document
|
24
24
|
include Mongoid::NormalizeStrings
|
25
25
|
|
26
26
|
field :name, type: String
|
27
27
|
normalize :name
|
28
28
|
end
|
29
|
+
|
30
|
+
city = City.create(name: 'Córdoba')
|
31
|
+
city.name # => 'Córdoba'
|
32
|
+
city.name_normalized # => 'cordoba'
|
29
33
|
```
|
30
34
|
|
31
35
|
## Contributing
|
@@ -17,7 +17,13 @@ module Mongoid
|
|
17
17
|
# Returns normalized_fields Class intance variable
|
18
18
|
#
|
19
19
|
def normalized_fields
|
20
|
-
|
20
|
+
normalized_fields = (@normalized_fields || Set.new)
|
21
|
+
|
22
|
+
if self.superclass.methods.include? :normalized_fields
|
23
|
+
normalized_fields + self.superclass.normalized_fields
|
24
|
+
else
|
25
|
+
normalized_fields
|
26
|
+
end
|
21
27
|
end
|
22
28
|
end
|
23
29
|
|
data/spec/normalize_spec.rb
CHANGED
@@ -9,23 +9,48 @@ describe Mongoid::NormalizeStrings do
|
|
9
9
|
normalize :name
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
describe 'hooks' do
|
13
|
+
it 'normalize fields after create' do
|
14
|
+
city = City.create(name: 'Córdoba')
|
15
|
+
expect(city.name_normalized).to eq('cordoba')
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
it 'normalize fields after save' do
|
19
|
+
city = City.create(name: 'Córdoba')
|
20
|
+
city.name = 'Ávila'
|
21
|
+
city.save
|
22
|
+
expect(city.reload.name_normalized).to eq('avila')
|
23
|
+
end
|
22
24
|
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
+
describe 'inheritance' do
|
27
|
+
context 'when superclass has normalized_fields' do
|
28
|
+
it 'return superclass + class normalized fields' do
|
29
|
+
class MyCity < City
|
30
|
+
field :state
|
31
|
+
normalize :state
|
32
|
+
end
|
33
|
+
|
34
|
+
expect(City.normalized_fields).to match_array([:name])
|
35
|
+
expect(MyCity.normalized_fields).to match_array([:name, :state])
|
36
|
+
end
|
26
37
|
end
|
27
38
|
|
28
|
-
|
29
|
-
|
39
|
+
context 'when superclass has not normalized_fields' do
|
40
|
+
it 'return class normalized_fields' do
|
41
|
+
class A
|
42
|
+
include Mongoid::Document
|
43
|
+
end
|
44
|
+
|
45
|
+
class B < A
|
46
|
+
include Mongoid::NormalizeStrings
|
47
|
+
|
48
|
+
field :name
|
49
|
+
normalize :name
|
50
|
+
end
|
51
|
+
|
52
|
+
expect(B.normalized_fields).to match_array([:name])
|
53
|
+
end
|
54
|
+
end
|
30
55
|
end
|
31
56
|
end
|