tiny_sweeper 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -5
- data/lib/tiny_sweeper/version.rb +1 -1
- data/lib/tiny_sweeper.rb +10 -10
- data/spec/tiny_sweeper_spec.rb +0 -15
- 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: 57a9592ee6e0610b57e326c16b4acc66c4e84735
|
4
|
+
data.tar.gz: 8ac28a68c9d45d9926aeffce712aae4ac03a3f00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab8711abf77ee6cdf3ab60b1cdefc4b1ef918b4632f0ffa165c67236aa2776a029ad095c5ae789f533ec71b06c55074879492befb7fe269905ec27914b2ab9a3
|
7
|
+
data.tar.gz: 5106e0f3a1107d974f5ab4e240d94e948eccbc6010cebd1764135e22578289598076d1d59d95775f9cfd212850a6e8ed7d1f01713bbbac1537c3de2d7e49ef30
|
data/README.md
CHANGED
@@ -70,11 +70,7 @@ Rails models are clearly the natural use-case for this. So it would make sense t
|
|
70
70
|
|
71
71
|
## How Does It Work?
|
72
72
|
|
73
|
-
You include the `TinySweeper` module in your class, and define some sweep-up rules on your class' attributes. It
|
74
|
-
|
75
|
-
"Isn't it better to generate a module for the new methods, and call `super`?"
|
76
|
-
|
77
|
-
Sure, but if you do that, the module's method is called *after* the original one. We want to clean the input *before* it gets to your method.
|
73
|
+
You include the `TinySweeper` module in your class, and define some sweep-up rules on your class' attributes. It prepends an anonymous module to your class, adds to it a method with the same name that cleans its input according to the sweep-up rule, and then passes the cleaned value to `super`.
|
78
74
|
|
79
75
|
"Why not use `after_create` or `before_save` or `before_validate` callbacks?"
|
80
76
|
|
data/lib/tiny_sweeper/version.rb
CHANGED
data/lib/tiny_sweeper.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
module TinySweeper
|
2
2
|
module ClassMethods
|
3
3
|
def sweep(field_name, &sweeper)
|
4
|
-
stop_if_attribute_does_not_exist!(field_name)
|
5
4
|
stop_if_we_have_seen_this_before!(field_name)
|
6
5
|
|
7
6
|
writer_method_name = writer_method_name(field_name)
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
send("original #{writer_method_name}", clean_value)
|
8
|
+
overrides_module.module_eval do
|
9
|
+
define_method(writer_method_name) do |value|
|
10
|
+
super(sweeper.call(value))
|
11
|
+
end
|
14
12
|
end
|
15
13
|
end
|
16
14
|
|
@@ -22,10 +20,12 @@ module TinySweeper
|
|
22
20
|
|
23
21
|
private
|
24
22
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
def overrides_module
|
24
|
+
@overrides_module ||= begin
|
25
|
+
mod = Module.new
|
26
|
+
prepend mod
|
27
|
+
mod
|
28
|
+
end
|
29
29
|
end
|
30
30
|
|
31
31
|
def stop_if_we_have_seen_this_before!(field_name)
|
data/spec/tiny_sweeper_spec.rb
CHANGED
@@ -10,13 +10,6 @@ describe 'cleaning fields' do
|
|
10
10
|
sweep(:name) { |n| n.upcase }
|
11
11
|
end
|
12
12
|
|
13
|
-
it 'leaves some unfortunate method names, maybe?' do
|
14
|
-
contract = Contract.new
|
15
|
-
original_writers = contract.methods.grep(/^original /).sort
|
16
|
-
expect(original_writers).to eq([:"original name=", :"original notes="])
|
17
|
-
# NB: we're not saying this is GOOD, we're just noting it.
|
18
|
-
end
|
19
|
-
|
20
13
|
it 'strips notes' do
|
21
14
|
contract = Contract.new
|
22
15
|
contract.notes = ' needs stripping '
|
@@ -64,14 +57,6 @@ describe 'cleaning fields' do
|
|
64
57
|
}.to raise_error
|
65
58
|
end
|
66
59
|
|
67
|
-
it "will bark if you sweep a method that doesn't exist" do
|
68
|
-
some_class = Class.new
|
69
|
-
some_class.send(:include, TinySweeper)
|
70
|
-
expect {
|
71
|
-
some_class.send(:sweep, :attribute, &:whatever)
|
72
|
-
}.to raise_error("There is no method named :attribute to sweep up!")
|
73
|
-
end
|
74
|
-
|
75
60
|
it "will let you sweep an inherited method" do
|
76
61
|
class BaseClass
|
77
62
|
attr_accessor :name
|