tiny_sweeper 0.0.4 → 0.0.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.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/README.md +15 -12
- data/lib/tiny_sweeper.rb +12 -10
- data/lib/tiny_sweeper/version.rb +1 -1
- data/spec/tiny_sweeper_spec.rb +25 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d882388d33abc912ce5a2053ca3875fa522de795
|
4
|
+
data.tar.gz: 9ba763bf797822519587ff6dd9083bb09ec8bac1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7801be0bb60028f792f690d3027851a3d686070b2b57bce51dab5802171680ac5e75783621a537ff4ba0f526887a7369c4f49479b60db5efa4783e0b3a7e23d6
|
7
|
+
data.tar.gz: 1085c3a1a2f13dc85ccd99f6e667fb1c31c6c55ca9878f4967c886b843bfa9b061bfd03e0ec0105c7d4e9deaa6a01a41de011d70acf9a5eab5f651a8a1cae2ec
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -11,10 +11,10 @@ It's a handy way to clean attributes on your Rails models, though it's independe
|
|
11
11
|
|
12
12
|
```ruby
|
13
13
|
class Sundae
|
14
|
-
attr_accessor :topping
|
14
|
+
attr_accessor :ice_cream, :topping
|
15
15
|
|
16
16
|
include TinySweeper
|
17
|
-
sweep(:topping) { |
|
17
|
+
sweep(:ice_cream, :topping) { |flavor| flavor.strip.downcase }
|
18
18
|
end
|
19
19
|
```
|
20
20
|
|
@@ -22,8 +22,10 @@ Now your Sundae toppings will be tidied up:
|
|
22
22
|
|
23
23
|
```ruby
|
24
24
|
dessert = Sundae.new
|
25
|
+
dessert.ice_cream = ' CHOCOlate '
|
25
26
|
dessert.topping = ' ButTTERscotCH '
|
26
|
-
dessert.
|
27
|
+
dessert.ice_cream #=> 'chocolate'. Tidy!
|
28
|
+
dessert.topping #=> 'butterscotch'. Tidy!
|
27
29
|
```
|
28
30
|
|
29
31
|
TinySweeper will not bother you about your nil values; they're your job to handle.
|
@@ -32,7 +34,7 @@ TinySweeper will not bother you about your nil values; they're your job to handl
|
|
32
34
|
Sundae.new.topping = nil # No topping? TinySweeper won't sweep it.
|
33
35
|
```
|
34
36
|
|
35
|
-
If you have an object with lots of attributes that need cleaning, you can do that, too:
|
37
|
+
If you have an object with lots of attributes that need cleaning (because, say, they were loaded from the database), you can do that, too:
|
36
38
|
|
37
39
|
```ruby
|
38
40
|
dessert.sweep_up!
|
@@ -44,8 +46,6 @@ Sundae.sweep_up!(dessert)
|
|
44
46
|
|
45
47
|
Just spit-balling here...
|
46
48
|
|
47
|
-
It'd be nice to define sweep-ups for multiple fields.
|
48
|
-
|
49
49
|
If you often sweep up fields in the same way - say, squishing and nilifying blanks - it'd be nice to bundle that up in some way, so you don't have to repeat yourself. Something like this might be nice:
|
50
50
|
|
51
51
|
```ruby
|
@@ -72,7 +72,14 @@ end
|
|
72
72
|
|
73
73
|
#### Other Ways to Sweep
|
74
74
|
|
75
|
-
Rails models are clearly the natural use-case for this. So it would make sense to have an easy way to auto-clean up models in a table. We'll see.
|
75
|
+
Rails models are clearly the natural use-case for this. So it would make sense to have an easy way to auto-clean up models in a table. We'll see. Right now, this works (though it's slow):
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
MyModel.find_each do |m|
|
79
|
+
m.sweep_up!
|
80
|
+
m.save
|
81
|
+
end
|
82
|
+
```
|
76
83
|
|
77
84
|
## How Does It Work?
|
78
85
|
|
@@ -80,11 +87,7 @@ You include the `TinySweeper` module in your class, and define some sweep-up rul
|
|
80
87
|
|
81
88
|
"Why not use `after_create` or `before_save` or `before_validate` callbacks?"
|
82
89
|
|
83
|
-
That's one approach, and it's used by [nilify_blanks](https://github.com/rubiety/nilify_blanks), so it's clearly workable.
|
84
|
-
|
85
|
-
But it means your data isn't cleaned until the callback runs; TinySweeper cleans your data as soon as it arrives.
|
86
|
-
|
87
|
-
Also, it requires rails, so you can't use it outside of rails.
|
90
|
+
That's one approach, and it's used by [nilify_blanks](https://github.com/rubiety/nilify_blanks), so it's clearly workable. But it means your data isn't cleaned until the callback runs; TinySweeper cleans your data as soon as it arrives. Also, it requires rails, so you can't use it outside of rails.
|
88
91
|
|
89
92
|
## Install It
|
90
93
|
|
data/lib/tiny_sweeper.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
module TinySweeper
|
2
2
|
module ClassMethods
|
3
|
-
def sweep(
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
def sweep(*field_names, &sweeper)
|
4
|
+
Array(field_names).each do |field_name|
|
5
|
+
stop_if_we_have_seen_this_before!(field_name)
|
6
|
+
|
7
|
+
overrides_module.module_eval do
|
8
|
+
define_method("#{field_name}=") do |value|
|
9
|
+
if value
|
10
|
+
super(sweeper.call(value))
|
11
|
+
else
|
12
|
+
super(value)
|
13
|
+
end
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
@@ -51,4 +53,4 @@ module TinySweeper
|
|
51
53
|
end
|
52
54
|
|
53
55
|
# Do it on all fields, by default? Or be explicit?
|
54
|
-
# TODO: add EagerSweeper, which loops over attributes
|
56
|
+
# TODO: add EagerSweeper, which loops over attributes. But how would it sweep?
|
data/lib/tiny_sweeper/version.rb
CHANGED
data/spec/tiny_sweeper_spec.rb
CHANGED
@@ -68,6 +68,7 @@ describe 'cleaning fields' do
|
|
68
68
|
end
|
69
69
|
|
70
70
|
it "will let you sweep an inherited method" do
|
71
|
+
# This just falls out of the design, but it's nice to note.
|
71
72
|
class BaseClass
|
72
73
|
attr_accessor :name
|
73
74
|
end
|
@@ -82,3 +83,27 @@ describe 'cleaning fields' do
|
|
82
83
|
expect(child.name).to eq('Monty')
|
83
84
|
end
|
84
85
|
end
|
86
|
+
|
87
|
+
describe 'sweeping many fields at once, in the same way' do
|
88
|
+
class Address
|
89
|
+
attr_accessor :address1, :address2, :city, :state, :zip
|
90
|
+
include TinySweeper
|
91
|
+
sweep :address1, :address2, :city, :state, :zip, &:strip
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'can do it' do
|
95
|
+
address = Address.new
|
96
|
+
|
97
|
+
address.address1 = ' 12 Elm St '
|
98
|
+
address.address2 = ' Apt B '
|
99
|
+
address.city = ' New Haven '
|
100
|
+
address.state = ' CT '
|
101
|
+
address.zip = ' 06510 '
|
102
|
+
|
103
|
+
expect(address.address1).to eq('12 Elm St')
|
104
|
+
expect(address.address2).to eq('Apt B')
|
105
|
+
expect(address.city ).to eq('New Haven')
|
106
|
+
expect(address.state ).to eq('CT')
|
107
|
+
expect(address.zip ).to eq('06510')
|
108
|
+
end
|
109
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_sweeper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Bernier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|