tiny_sweeper 0.0.2 → 0.0.3

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: bab53d4f83febe0e94f9f5c97f632b1c7a4f6074
4
- data.tar.gz: 6cb5b37b25afdbaa6599567434aae424bc52a9f1
3
+ metadata.gz: 57a9592ee6e0610b57e326c16b4acc66c4e84735
4
+ data.tar.gz: 8ac28a68c9d45d9926aeffce712aae4ac03a3f00
5
5
  SHA512:
6
- metadata.gz: a575f5ff0cba1a49c8a46af5b15159749cf53b2313cfeee4ff5bd793f020bba9eb00a6405d443a1181ab149e4725afd67457e61f93b7e34df8369dc64fda84e8
7
- data.tar.gz: 20bd0f5be58572b3931aa4ddda86503a4e477d7960a37e5ae149c158a225172afeb037a985bf12fce857d61939a428416b77361860cb36997dab5d546056046c
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 overrides your method, and defines a new method that cleans its input according to the sweep-up rule, and then calls the original method with the clean value.
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
 
@@ -1,3 +1,3 @@
1
1
  module TinySweeper
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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
- alias_method "original #{writer_method_name}", writer_method_name
10
-
11
- define_method(writer_method_name) do |value|
12
- clean_value = sweeper.call(value)
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 stop_if_attribute_does_not_exist!(field_name)
26
- unless instance_methods(true).include?(writer_method_name(field_name))
27
- raise "There is no method named #{field_name.inspect} to sweep up!"
28
- end
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)
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_sweeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Bernier