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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f01b1523f85b42ff7dab6b0a3f5f45f7fc0fef1
4
- data.tar.gz: 44f3c34cd71b6b9ca16aaa8ff6f972bc49f3d4cf
3
+ metadata.gz: d882388d33abc912ce5a2053ca3875fa522de795
4
+ data.tar.gz: 9ba763bf797822519587ff6dd9083bb09ec8bac1
5
5
  SHA512:
6
- metadata.gz: f69edb529e29a9496b7d72bb5fa91fe9932ddd7600e337b1056798625633313a9af87cc4eef7abe20a6eafd8e267f12c360572bb807850a779f4ccf77f7e6507
7
- data.tar.gz: 7fa70945f24ed1f00d7365efaf7f2e1aef4eeacbc7c16ec0e3ff64916876beae8f5a62d9735f95428f787ff3efd3d32fe90d5d0fcf5f0702f6f671fa7b8ad6ac
6
+ metadata.gz: 7801be0bb60028f792f690d3027851a3d686070b2b57bce51dab5802171680ac5e75783621a537ff4ba0f526887a7369c4f49479b60db5efa4783e0b3a7e23d6
7
+ data.tar.gz: 1085c3a1a2f13dc85ccd99f6e667fb1c31c6c55ca9878f4967c886b843bfa9b061bfd03e0ec0105c7d4e9deaa6a01a41de011d70acf9a5eab5f651a8a1cae2ec
data/.travis.yml CHANGED
@@ -3,4 +3,3 @@ rvm:
3
3
  - 2.2
4
4
  - 2.1
5
5
  - 2.0
6
- - 1.9
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) { |topping| topping.strip.downcase }
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.topping #=> 'butterscotch'. Tidy!
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(field_name, &sweeper)
4
- stop_if_we_have_seen_this_before!(field_name)
5
-
6
- overrides_module.module_eval do
7
- define_method("#{field_name}=") do |value|
8
- if value
9
- super(sweeper.call(value))
10
- else
11
- super(value)
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?
@@ -1,3 +1,3 @@
1
1
  module TinySweeper
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -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
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-02-07 00:00:00.000000000 Z
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler