scrubby 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ test/*.db
data/Rakefile CHANGED
@@ -1,45 +1,53 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
3
5
 
4
6
  begin
5
7
  require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "scrubby"
8
- gem.summary = %Q{Clean up your incoming ActiveRecord model attributes}
9
- gem.description = %Q{Clean up your incoming ActiveRecord model attributes}
10
- gem.email = "steve.richert@gmail.com"
11
- gem.homepage = "http://github.com/laserlemon/scrubby"
12
- gem.authors = ["laserlemon"]
13
- gem.add_development_dependency "rspec", ">= 1.2.9"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
8
+ Jeweler::Tasks.new do |g|
9
+ g.name = 'scrubby'
10
+ g.summary = %(Clean up your incoming ActiveRecord model attributes)
11
+ g.description = %(Clean up your incoming ActiveRecord model attributes)
12
+ g.email = 'steve.richert@gmail.com'
13
+ g.homepage = 'http://github.com/laserlemon/scrubby'
14
+ g.authors = ['Steve Richert']
15
+ g.add_dependency 'activerecord'
16
+ g.add_development_dependency 'shoulda'
15
17
  end
16
18
  Jeweler::GemcutterTasks.new
17
19
  rescue LoadError
18
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
19
21
  end
20
22
 
21
- require 'spec/rake/spectask'
22
- Spec::Rake::SpecTask.new(:spec) do |spec|
23
- spec.libs << 'lib' << 'spec'
24
- spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'lib' << 'test'
25
+ t.pattern = 'test/**/test_*.rb'
26
+ t.verbose = true
25
27
  end
26
28
 
27
- Spec::Rake::SpecTask.new(:rcov) do |spec|
28
- spec.libs << 'lib' << 'spec'
29
- spec.pattern = 'spec/**/*_spec.rb'
30
- spec.rcov = true
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |t|
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/test_*.rb'
34
+ t.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort 'RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov'
39
+ end
31
40
  end
32
41
 
33
- task :spec => :check_dependencies
42
+ task :test => :check_dependencies
34
43
 
35
- task :default => :spec
44
+ task :default => :test
36
45
 
37
- require 'rake/rdoctask'
38
- Rake::RDocTask.new do |rdoc|
39
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+ Rake::RDocTask.new do |r|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ''
40
48
 
41
- rdoc.rdoc_dir = 'rdoc'
42
- rdoc.title = "scrubby #{version}"
43
- rdoc.rdoc_files.include('README*')
44
- rdoc.rdoc_files.include('lib/**/*.rb')
49
+ r.rdoc_dir = 'rdoc'
50
+ r.title = "scrubby #{version}"
51
+ r.rdoc_files.include('README*')
52
+ r.rdoc_files.include('lib/**/*.rb')
45
53
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,19 +1,18 @@
1
1
  module Scrubby
2
2
  def self.included(base)
3
3
  base.class_eval do
4
+ class_inheritable_hash :scrubbers
5
+ self.scrubbers = {}
6
+
4
7
  extend ClassMethods
8
+ include InstanceMethods
9
+
10
+ alias_method_chain :write_attribute, :scrub
5
11
  end
6
12
  end
7
13
 
8
14
  module ClassMethods
9
15
  def scrub(*attributes, &block)
10
- unless respond_to?(:scrubbers)
11
- class_inheritable_hash :scrubbers
12
-
13
- include InstanceMethods
14
- alias_method_chain :write_attribute, :scrub
15
- end
16
-
17
16
  scrubber = block_given? ? block : instance_method(:scrub)
18
17
  self.scrubbers = attributes.inject({}){|s,a| s.merge!(a.to_s => scrubber) }
19
18
  end
@@ -29,6 +28,8 @@ module Scrubby
29
28
  end
30
29
 
31
30
  def scrub(value)
31
+ value = value.dup if value.duplicable?
32
+
32
33
  case value
33
34
  when String
34
35
  value.strip!
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{scrubby}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["laserlemon"]
12
- s.date = %q{2010-01-23}
11
+ s.authors = ["Steve Richert"]
12
+ s.date = %q{2010-01-24}
13
13
  s.description = %q{Clean up your incoming ActiveRecord model attributes}
14
14
  s.email = %q{steve.richert@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,9 +26,8 @@ Gem::Specification.new do |s|
26
26
  "init.rb",
27
27
  "lib/scrubby.rb",
28
28
  "scrubby.gemspec",
29
- "spec/scrubby_spec.rb",
30
- "spec/spec.opts",
31
- "spec/spec_helper.rb"
29
+ "test/helper.rb",
30
+ "test/test_scrubby.rb"
32
31
  ]
33
32
  s.homepage = %q{http://github.com/laserlemon/scrubby}
34
33
  s.rdoc_options = ["--charset=UTF-8"]
@@ -36,8 +35,8 @@ Gem::Specification.new do |s|
36
35
  s.rubygems_version = %q{1.3.5}
37
36
  s.summary = %q{Clean up your incoming ActiveRecord model attributes}
38
37
  s.test_files = [
39
- "spec/scrubby_spec.rb",
40
- "spec/spec_helper.rb"
38
+ "test/helper.rb",
39
+ "test/test_scrubby.rb"
41
40
  ]
42
41
 
43
42
  if s.respond_to? :specification_version then
@@ -45,12 +44,15 @@ Gem::Specification.new do |s|
45
44
  s.specification_version = 3
46
45
 
47
46
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
47
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
48
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
49
49
  else
50
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
+ s.add_dependency(%q<activerecord>, [">= 0"])
51
+ s.add_dependency(%q<shoulda>, [">= 0"])
51
52
  end
52
53
  else
53
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ s.add_dependency(%q<activerecord>, [">= 0"])
55
+ s.add_dependency(%q<shoulda>, [">= 0"])
54
56
  end
55
57
  end
56
58
 
@@ -0,0 +1,36 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.dirname(__FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'active_record'
7
+ require 'shoulda'
8
+ require 'mocha'
9
+ require 'scrubby'
10
+ begin; require 'redgreen'; rescue LoadError; end
11
+
12
+ ActiveRecord::Base.establish_connection(
13
+ :adapter => 'sqlite3',
14
+ :database => File.join(File.dirname(__FILE__), 'test.db')
15
+ )
16
+
17
+ class CreateSchema < ActiveRecord::Migration
18
+ def self.up
19
+ create_table :users, :force => true do |t|
20
+ t.string :type
21
+ t.string :first_name
22
+ t.string :last_name
23
+ t.timestamps
24
+ end
25
+ end
26
+ end
27
+
28
+ CreateSchema.suppress_messages do
29
+ CreateSchema.migrate(:up)
30
+ end
31
+
32
+ class User < ActiveRecord::Base
33
+ end
34
+
35
+ class Admin < User
36
+ end
@@ -0,0 +1,166 @@
1
+ require 'helper'
2
+
3
+ class TestScrubby < Test::Unit::TestCase
4
+ context 'Scrubbing' do
5
+ context 'an attribute' do
6
+ setup do
7
+ User.scrub(:first_name)
8
+ @user = User.new
9
+ end
10
+
11
+ should 'strip string values' do
12
+ first_name = 'Steve '
13
+ @user.first_name = first_name
14
+ assert_equal first_name.strip, @user.first_name
15
+ end
16
+
17
+ should 'nuke blank string values' do
18
+ first_name = ' '
19
+ @user.first_name = first_name
20
+ assert_nil @user.first_name
21
+ end
22
+
23
+ should 'not edit the given value in place' do
24
+ first_name = 'Steve '
25
+ value = first_name.dup
26
+ @user.first_name = value
27
+ assert_equal first_name, value
28
+ end
29
+
30
+ should 'clean the underlying attribute' do
31
+ first_name = 'Steve '
32
+ @user.first_name = first_name
33
+ assert_not_equal first_name, @user.first_name
34
+ assert_equal @user.first_name, @user.attributes['first_name']
35
+ end
36
+
37
+ context 'with a block' do
38
+ setup do
39
+ @block = proc{|v| v.titleize }
40
+ User.scrub(:first_name, &@block)
41
+ @user = User.new
42
+ end
43
+
44
+ should 'not strip string values' do
45
+ first_name = 'Steve '
46
+ @user.first_name = first_name
47
+ assert_equal first_name, @user.first_name
48
+ end
49
+
50
+ should 'not nuke blank string values' do
51
+ first_name = ' '
52
+ @user.first_name = first_name
53
+ assert_equal first_name, @user.first_name
54
+ end
55
+
56
+ should 'use the the block' do
57
+ first_name = 'steve'
58
+ @user.first_name = first_name
59
+ assert_equal @block.call(first_name), @user.first_name
60
+ end
61
+
62
+ context 'having zero arity' do
63
+ setup do
64
+ User.scrub(:first_name){ nil }
65
+ @user = User.new
66
+ end
67
+
68
+ should 'work just fine' do
69
+ assert_nothing_raised do
70
+ @user.first_name = 'Steve'
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ context 'a virtual attribute' do
78
+ setup do
79
+ User.class_eval do
80
+ attr_accessor :middle_name
81
+ scrub :middle_name
82
+ end
83
+ @user = User.new
84
+ end
85
+
86
+ should 'do nothing' do
87
+ middle_name = ' Joel Michael '
88
+ @user.middle_name = middle_name
89
+ assert_equal middle_name, @user.middle_name
90
+ end
91
+ end
92
+
93
+ context 'an invalid attribute' do
94
+ setup do
95
+ User.scrub(:middle_name)
96
+ @user = User.new
97
+ end
98
+
99
+ should 'do nothing' do
100
+ assert_nothing_raised do
101
+ @user.update_attributes(:first_name => 'Steve', :last_name => 'Richert')
102
+ end
103
+ end
104
+ end
105
+
106
+ context 'multiple attributes' do
107
+ setup do
108
+ User.scrub(:first_name, :last_name)
109
+ @user = User.new
110
+ end
111
+
112
+ should 'clean all values' do
113
+ first_name, last_name = ' Steve', 'Richert '
114
+ @user.first_name, @user.last_name = first_name, last_name
115
+ assert_not_equal first_name, @user.first_name
116
+ assert_not_equal last_name, @user.last_name
117
+ end
118
+ end
119
+
120
+ context 'an inherited model' do
121
+ setup do
122
+ User.scrub(:first_name)
123
+ User.inherited(Admin) # Reinitializes the scrubber inheritance
124
+ Admin.scrub(:last_name)
125
+ end
126
+
127
+ should 'respect all scrubbers' do
128
+ assert_same_elements %w(first_name last_name), Admin.scrubbers.keys
129
+ end
130
+
131
+ should 'not add a parent scrubber' do
132
+ assert_same_elements %w(first_name), User.scrubbers.keys
133
+ end
134
+
135
+ context 'with overlapping attributes' do
136
+ setup do
137
+ @value = nil
138
+ User.scrub(:first_name)
139
+ @user = User.new
140
+ User.inherited(Admin) # Reinitializes the scrubber inheritance
141
+ Admin.scrub(:first_name){ @value }
142
+ @admin = Admin.new
143
+ end
144
+
145
+ should 'give priority to the inherited scrubber' do
146
+ first_name = 'Steve'
147
+ assert_not_equal @value, first_name
148
+ @admin.first_name = first_name
149
+ assert_equal @value, @admin.first_name
150
+ end
151
+
152
+ should 'not clobber the parent scrubber' do
153
+ first_name = 'Steve'
154
+ assert_not_equal @value, first_name
155
+ @user.first_name = first_name
156
+ assert_not_equal @value, @user.first_name
157
+ end
158
+ end
159
+ end
160
+
161
+ teardown do
162
+ User.inheritable_attributes.delete(:scrubbers)
163
+ Admin.inheritable_attributes.delete(:scrubbers)
164
+ end
165
+ end
166
+ end
metadata CHANGED
@@ -1,26 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrubby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - laserlemon
7
+ - Steve Richert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-23 00:00:00 -05:00
12
+ date: 2010-01-24 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rspec
16
+ name: activerecord
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: shoulda
17
27
  type: :development
18
28
  version_requirement:
19
29
  version_requirements: !ruby/object:Gem::Requirement
20
30
  requirements:
21
31
  - - ">="
22
32
  - !ruby/object:Gem::Version
23
- version: 1.2.9
33
+ version: "0"
24
34
  version:
25
35
  description: Clean up your incoming ActiveRecord model attributes
26
36
  email: steve.richert@gmail.com
@@ -41,9 +51,8 @@ files:
41
51
  - init.rb
42
52
  - lib/scrubby.rb
43
53
  - scrubby.gemspec
44
- - spec/scrubby_spec.rb
45
- - spec/spec.opts
46
- - spec/spec_helper.rb
54
+ - test/helper.rb
55
+ - test/test_scrubby.rb
47
56
  has_rdoc: true
48
57
  homepage: http://github.com/laserlemon/scrubby
49
58
  licenses: []
@@ -73,5 +82,5 @@ signing_key:
73
82
  specification_version: 3
74
83
  summary: Clean up your incoming ActiveRecord model attributes
75
84
  test_files:
76
- - spec/scrubby_spec.rb
77
- - spec/spec_helper.rb
85
+ - test/helper.rb
86
+ - test/test_scrubby.rb
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Scrubby" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end
@@ -1 +0,0 @@
1
- --color
@@ -1,9 +0,0 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'scrubby'
4
- require 'spec'
5
- require 'spec/autorun'
6
-
7
- Spec::Runner.configure do |config|
8
-
9
- end