cleanerupper 0.2.1 → 0.2.2

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.
@@ -25,13 +25,13 @@ A default dictionary is included with this project, but only contains some test
25
25
  It works by providing a new method to all of your ActiveRecord based objects called `clean`
26
26
 
27
27
  class Widget < ActiveRecord::Base
28
- clean :body, :with => :scramble
28
+ clean :body, :method => :scramble
29
29
  end
30
30
 
31
- This method takes an array of columns to be cleaned by cleanerupper, followed by two options:
31
+ This method takes an array of columns to be cleaned by cleanerupper, followed by three optional parameters:
32
32
 
33
- :with => Specifies which method to clean with
34
- :dictionary => Specifies which dictionary should be used for this cleaning
33
+ :method => Specifies which method to clean with
34
+ :dictionary => Specifies which dictionaries should be used for this cleaning
35
35
  :callback => Specifies a callback to call if disallowed data is found
36
36
 
37
37
  Three methods have been provided for cleaning convenience, which are:
@@ -43,13 +43,10 @@ Three methods have been provided for cleaning convenience, which are:
43
43
  If no method is defined, `:scramble` will be used. You can also define your own function, like so:
44
44
 
45
45
  class Widget < ActiveRecord::Base
46
- clean :body, :with => :custom
46
+ clean :body, :method => :remove_vowels
47
47
 
48
- def custom(found, dict)
49
- Cleaner::Data.dictionaries[dict].each do |word|
50
- found.gsub!(word, "CUSTOM") if found.include?(word)
51
- end
52
- return found
48
+ def custom(val)
49
+ return val.gsub(/(a|e|i|o|u)/, "*")
53
50
  end
54
51
  end
55
52
 
@@ -64,14 +61,20 @@ In the example above, we make use of the `word` dictionary to check our column f
64
61
  You can access these dictionaries by using the `Cleaner::Data.dictionaries[:key]` object, where `:key` is the key of your dictionary as defined by your config file. You can specify that any cleaning method use a specific dictionary by adding a `:dictionary` paramater:
65
62
 
66
63
  class Widget < ActiveRecord::Base
67
- clean :body, :with => :replace, :dictionary => :custom
64
+ clean :body, :method => :replace, :dictionary => :custom
65
+ end
66
+
67
+ You can use multiple dictionaries for any given cleaning method by passing an array to the `:dictionary` option:
68
+
69
+ class Widget < ActiveRecord::Base
70
+ clean :body, :dictionary => [:words, :custom]
68
71
  end
69
72
 
70
73
  You can also define a callback. This callback will only be called if bad data was found in any of
71
- the columns. If the callback returns falls, the save will fail (this works the same way as a `before_save`).
74
+ the columns. If the callback returns false, the save will fail (this works the same way as a `before_save`).
72
75
 
73
76
  class Widget < ActiveRecord::Base
74
- clean :body, :with => :scramble, :callback => :found_words
77
+ clean :body, :method => :scramble, :callback => :found_words
75
78
 
76
79
  def found_words
77
80
  Emailer.email_teacher("Your student used a bad word!")
@@ -83,13 +86,13 @@ the columns. If the callback returns falls, the save will fail (this works the
83
86
 
84
87
  # Clean different columns with different methods
85
88
  class Widget < ActiveRecord::Base
86
- clean :body, :title, :with => :replace
87
- clean :author_name, :with => :scramble
89
+ clean :body, :title, :method => :replace
90
+ clean :author_name, :method => :scramble
88
91
  end
89
92
 
90
93
  # Clean the body, and send an email if this is the first time a bad word has been used
91
94
  class Widget < ActiveRecord::Base
92
- clean :body, :with => :replace, :callback => :send_email
95
+ clean :body, :method => :replace, :callback => :send_email
93
96
 
94
97
  def send_email
95
98
  if self.author.infractions >= 1
@@ -102,7 +105,7 @@ the columns. If the callback returns falls, the save will fail (this works the
102
105
 
103
106
  # Custom cleaning method
104
107
  class Widget < ActiveRecord::Base
105
- clean :body, :title, :author_name, :with => :remove_vowels
108
+ clean :body, :title, :author_name, :method => :remove_vowels
106
109
 
107
110
  def remove_vowels(found)
108
111
  Cleaner::Dictionary.words.each do |word|
@@ -114,7 +117,7 @@ the columns. If the callback returns falls, the save will fail (this works the
114
117
 
115
118
  # Custom dictionary
116
119
  class Widget < ActiveRecord::Base
117
- clean :body, :title, :with => :replace, :dictionary => :animals
120
+ clean :body, :title, :method => :replace, :dictionary => :animals
118
121
  end
119
122
 
120
123
  # Disclaimer #
@@ -122,8 +125,8 @@ This code is still under development, and as such, minor revisions may break com
122
125
  the gem/plugin. Please keep this in mind when using CleanerUpper.
123
126
 
124
127
  # What's Next? #
125
- * Change the custom cleaning code to be more user friendly
126
128
  * Optimize dictionary loops
129
+ * Benchmark the impact of the CleanerUpper codebase on database activity
127
130
  * Increase test coverage
128
131
  * Remove test dependency on the rails environment
129
132
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cleanerupper}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mike Trpcic"]
12
- s.date = %q{2010-04-20}
12
+ s.date = %q{2010-04-24}
13
13
  s.email = %q{mike@fluidmedia.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.markdown"
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "./pkg/cleanerupper-0.1.0.gem",
26
26
  "./pkg/cleanerupper-0.1.1.gem",
27
27
  "./pkg/cleanerupper-0.2.0.gem",
28
+ "./pkg/cleanerupper-0.2.1.gem",
28
29
  "./rails/init.rb",
29
30
  "./tasks/cleanerupper_tasks.rake",
30
31
  "./test/cleanerupper_test.rb",
@@ -50,7 +50,8 @@ module Cleaner
50
50
  if Cleaner::Data.cleaner_methods.include?(method.to_sym)
51
51
  new_value = Cleaner.send(method.to_sym, old_value.dup, dictionary)
52
52
  else
53
- new_value = self.send(method, old_value.dup, dictionary)
53
+ new_value = Cleaner.send(:custom_clean, old_value.dup, dictionary, self.method(method))
54
+ #new_value = self.send(method, old_value.dup, dictionary)
54
55
  end
55
56
  unless new_value == old_value
56
57
  to_save = callback.nil? ? true : self.send(callback) == false ? false : true
@@ -68,7 +69,7 @@ module Cleaner
68
69
  def clean(*args)
69
70
  params = args[-1].is_a?(Hash) ? args[-1] : {}
70
71
  attributes = args[0..-1] if params
71
- with = params.has_key?(:with) ? params[:with] : :scramble
72
+ with = params.has_key?(:method) ? params[:method] : :scramble
72
73
  callback = params.has_key?(:callback) ? params[:callback] : nil
73
74
  dictionary = params.has_key?(:dictionary) ? params[:dictionary] : :words
74
75
  attributes.each do |attribute|
@@ -79,6 +80,14 @@ module Cleaner
79
80
 
80
81
  #Define all your actual manipulation methods here:
81
82
 
83
+ #This is a wrapper method for custom cleaning methods defined by a user
84
+ def custom_clean(value, dict, func)
85
+ dict.each do |word|
86
+ value.to_s.gsub!(/#{word}/, func.call(word))
87
+ end
88
+ value
89
+ end
90
+
82
91
  #This method scrambles data by rearranging the letters.
83
92
  def scramble(value, dict)
84
93
  dict.each do |word|
@@ -13,31 +13,31 @@ end
13
13
 
14
14
  class ReplaceWidget < ActiveRecord::Base
15
15
  set_table_name :widgets
16
- clean :body, :with => :replace
16
+ clean :body, :method => :replace
17
17
  end
18
18
 
19
19
  class RemoveWidget < ActiveRecord::Base
20
20
  set_table_name :widgets
21
- clean :body, :title, :with => :remove
21
+ clean :body, :title, :method => :remove
22
22
  end
23
23
 
24
24
  class ScrambleWidget < ActiveRecord::Base
25
25
  set_table_name :widgets
26
- clean :title, :with => :scramble
26
+ clean :title, :method => :scramble
27
27
  end
28
28
 
29
29
  class CustomWidget < ActiveRecord::Base
30
30
  set_table_name :widgets
31
- clean :body, :title, :with => :custom_function
31
+ clean :body, :title, :method => :remove_vowels
32
32
 
33
- def custom_function(value, dict)
34
- return "Custom Value: #{value}"
33
+ def remove_vowels(val)
34
+ val.gsub(/(a|e|i|o|u)/, "*")
35
35
  end
36
36
  end
37
37
 
38
38
  class CallbackWidget < ActiveRecord::Base
39
39
  set_table_name :widgets
40
- clean :body, :with => :scramble, :callback => :callback_method
40
+ clean :body, :method => :scramble, :callback => :callback_method
41
41
 
42
42
  def callback_method
43
43
  self.title = "CALLBACK"
@@ -47,7 +47,7 @@ end
47
47
 
48
48
  class FalseCallbackWidget < ActiveRecord::Base
49
49
  set_table_name :widgets
50
- clean :body, :with => :scramble, :callback => :callback_method
50
+ clean :body, :method => :scramble, :callback => :callback_method
51
51
 
52
52
  def callback_method
53
53
  self.title = "CALLBACK"
@@ -57,12 +57,12 @@ end
57
57
 
58
58
  class CustomDictWidget < ActiveRecord::Base
59
59
  set_table_name :widgets
60
- clean :body, :with => :scramble, :dictionary => :animals
60
+ clean :body, :method => :scramble, :dictionary => :animals
61
61
  end
62
62
 
63
63
  class MultiDictWidget < ActiveRecord::Base
64
64
  set_table_name :widgets
65
- clean :body, :with => :scramble, :dictionary => [:animals, :furniture]
65
+ clean :body, :method => :scramble, :dictionary => [:animals, :furniture]
66
66
  end
67
67
 
68
68
  class CleanerupperTest < Test::Unit::TestCase
@@ -105,10 +105,10 @@ class CleanerupperTest < Test::Unit::TestCase
105
105
  w = CustomWidget.find(w.id)
106
106
 
107
107
  assert w.title != title
108
- assert w.title == "Custom Value: cleanerupper remove_test title"
108
+ assert w.title == "cleanerupper r*m*v*_t*st title"
109
109
 
110
110
  assert w.body != body
111
- assert w.body == "Custom Value: cleanerupper scramble_test body"
111
+ assert w.body == "cleanerupper scr*mbl*_t*st body"
112
112
  end
113
113
 
114
114
  def test_cleanerupper_custom_callback
@@ -2800,3 +2800,85 @@
2800
2800
  ScrambleWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 7) 
2801
2801
  MultiDictWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'stdotge_ regular_test tb_edtse', NULL)
2802
2802
  MultiDictWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 8) 
2803
+ SQL (0.4ms) select sqlite_version(*)
2804
+ SQL (1.6ms)  SELECT name
2805
+ FROM sqlite_master
2806
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
2807
+ 
2808
+ SQL (79.7ms) DROP TABLE "widgets"
2809
+ SQL (5.0ms) CREATE TABLE "widgets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100), "body" varchar(255), "author_name" varchar(100)) 
2810
+ SQL (1.0ms)  SELECT name
2811
+ FROM sqlite_master
2812
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
2813
+ 
2814
+ SQL (0.1ms) SELECT version FROM "schema_migrations"
2815
+ RemoveWidget Create (0.3ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper test', 'cleanerupper test', NULL)
2816
+ RemoveWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 1) 
2817
+ ReplaceWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'cleanerupper %$$$%**&%*@* test', NULL)
2818
+ ReplaceWidget Load (2.0ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 2) 
2819
+ ScrambleWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper ttlmescsbrae_ test', NULL, NULL)
2820
+ ScrambleWidget Load (0.7ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 3) 
2821
+ CallbackWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('CALLBACK', 'cleanerupper _ustescttmo body', NULL)
2822
+ CallbackWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 4) 
2823
+ CustomDictWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'tdsotge_ bird_test fsiest_ht', NULL)
2824
+ CustomDictWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 5) 
2825
+ ScrambleWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper tfauedts_elt body', NULL, NULL)
2826
+ ScrambleWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 6) 
2827
+ MultiDictWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'ots_gdte regular_test debts_et', NULL)
2828
+ MultiDictWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 7) 
2829
+ SQL (0.4ms) select sqlite_version(*)
2830
+ SQL (0.7ms)  SELECT name
2831
+ FROM sqlite_master
2832
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
2833
+ 
2834
+ SQL (21.4ms) DROP TABLE "widgets"
2835
+ SQL (2.7ms) CREATE TABLE "widgets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100), "body" varchar(255), "author_name" varchar(100)) 
2836
+ SQL (0.3ms)  SELECT name
2837
+ FROM sqlite_master
2838
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
2839
+ 
2840
+ SQL (0.1ms) SELECT version FROM "schema_migrations"
2841
+ RemoveWidget Create (0.3ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper test', 'cleanerupper test', NULL)
2842
+ RemoveWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 1) 
2843
+ ReplaceWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'cleanerupper &@$%%$**%$&* test', NULL)
2844
+ ReplaceWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 2) 
2845
+ ScrambleWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper rlbtsm_eeacts test', NULL, NULL)
2846
+ ScrambleWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 3) 
2847
+ CallbackWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('CALLBACK', 'cleanerupper cut_emtotss body', NULL)
2848
+ CallbackWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 4) 
2849
+ CustomDictWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'gs_otetd bird_test _ftshseti', NULL)
2850
+ CustomDictWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 5) 
2851
+ CustomWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper r*m*v*_t*st title', 'cleanerupper scr*mbl*_t*st body', NULL)
2852
+ CustomWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 6) 
2853
+ ScrambleWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper lftstedeau_t body', NULL, NULL)
2854
+ ScrambleWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 7) 
2855
+ MultiDictWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'tstdo_ge regular_test d_btsete', NULL)
2856
+ MultiDictWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 8) 
2857
+ SQL (0.4ms) select sqlite_version(*)
2858
+ SQL (0.7ms)  SELECT name
2859
+ FROM sqlite_master
2860
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
2861
+ 
2862
+ SQL (38.7ms) DROP TABLE "widgets"
2863
+ SQL (2.8ms) CREATE TABLE "widgets" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(100), "body" varchar(255), "author_name" varchar(100)) 
2864
+ SQL (0.3ms)  SELECT name
2865
+ FROM sqlite_master
2866
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
2867
+ 
2868
+ SQL (0.1ms) SELECT version FROM "schema_migrations"
2869
+ RemoveWidget Create (0.3ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper test', 'cleanerupper test', NULL)
2870
+ RemoveWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 1) 
2871
+ ReplaceWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'cleanerupper !*%$%*@$*%$@ test', NULL)
2872
+ ReplaceWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 2) 
2873
+ ScrambleWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper scsrm_tatbeel test', NULL, NULL)
2874
+ ScrambleWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 3) 
2875
+ CallbackWidget Create (1.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('CALLBACK', 'cleanerupper eut_tocmsts body', NULL)
2876
+ CallbackWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 4) 
2877
+ CustomDictWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, '_destogt bird_test htief_tss', NULL)
2878
+ CustomDictWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 5) 
2879
+ CustomWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper r*m*v*_t*st title', 'cleanerupper scr*mbl*_t*st body', NULL)
2880
+ CustomWidget Load (0.3ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 6) 
2881
+ ScrambleWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES('cleanerupper selfuetta_dt body', NULL, NULL)
2882
+ ScrambleWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 7) 
2883
+ MultiDictWidget Create (0.2ms) INSERT INTO "widgets" ("title", "body", "author_name") VALUES(NULL, 'gd_tstoe regular_test tsedbt_e', NULL)
2884
+ MultiDictWidget Load (0.2ms) SELECT * FROM "widgets" WHERE ("widgets"."id" = 8) 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Mike Trpcic
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-20 00:00:00 -04:00
17
+ date: 2010-04-24 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -37,6 +37,7 @@ files:
37
37
  - ./pkg/cleanerupper-0.1.0.gem
38
38
  - ./pkg/cleanerupper-0.1.1.gem
39
39
  - ./pkg/cleanerupper-0.2.0.gem
40
+ - ./pkg/cleanerupper-0.2.1.gem
40
41
  - ./rails/init.rb
41
42
  - ./tasks/cleanerupper_tasks.rake
42
43
  - ./test/cleanerupper_test.rb