acts_as_stripped 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.sqlite3
7
+ *.log
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Sonnek
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+ = acts_as_stripped
2
+
3
+ Simple utility to strip whitespace from string attributes.
4
+
5
+ = Installation
6
+
7
+ Command line installation
8
+
9
+ sudo gem install wireframe-acts_as_stripped
10
+
11
+ Rails environment.rb configuration
12
+
13
+ config.gem 'wireframe-acts_as_stripped', :source => 'http://gems.github.com', :lib => 'acts_as_stripped'
14
+
15
+ = Usage
16
+
17
+ # strip whitespace from *all* string attributes
18
+ class User < ActiveRecord::Base
19
+ acts_as_stripped
20
+ end
21
+
22
+ # strip whitespace from *select* string attributes
23
+ class Post < ActiveRecord::Base
24
+ acts_as_stripped :title, :summary
25
+ end
26
+
27
+ == Copyright
28
+
29
+ Copyright (c) 2009 Ryan Sonnek. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "acts_as_stripped"
8
+ gem.summary = %Q{strip whitespace from string attributes}
9
+ gem.email = "ryan.sonnek@gmail.com"
10
+ gem.homepage = "http://github.com/wireframe/acts_as_stripped"
11
+ gem.authors = ["Ryan Sonnek"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "acts_as_stripped #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 1
@@ -0,0 +1,36 @@
1
+ module ActsAsStripped
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ def acts_as_stripped(*attrs)
8
+ class_inheritable_accessor :acts_as_stripped_attributes
9
+ self.acts_as_stripped_attributes = attrs if attrs.any?
10
+
11
+ before_validation :strip_fields
12
+
13
+ include ActsAsStripped::InstanceMethods
14
+ extend ActsAsStripped::SingletonMethods
15
+ end
16
+ end
17
+
18
+ module SingletonMethods
19
+ end
20
+
21
+ module InstanceMethods
22
+ private
23
+ def strip_fields
24
+ strippable_attributes.each do |attr|
25
+ self[attr.to_s].strip!
26
+ end
27
+ end
28
+ def strippable_attributes
29
+ self.acts_as_stripped_attributes || self.attributes.keys.select {|attr| self[attr].respond_to?(:strip!) }
30
+ end
31
+ end
32
+ end
33
+
34
+ if Object.const_defined?("ActiveRecord")
35
+ ActiveRecord::Base.send(:include, ActsAsStripped)
36
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ ActiveRecord::Schema.define(:version => 1) do
4
+ create_table :users, :force => true do |t|
5
+ t.column :first_name, :string
6
+ t.column :last_name, :string
7
+ t.column :logged_in_at, :datetime
8
+ end
9
+ create_table :posts, :force => true do |t|
10
+ t.column :title, :string
11
+ t.column :body, :string
12
+ end
13
+ end
14
+
15
+ class User < ActiveRecord::Base
16
+ acts_as_stripped
17
+ end
18
+
19
+ class Post < ActiveRecord::Base
20
+ acts_as_stripped :title
21
+ end
22
+
23
+ class ActsAsStrippedTest < Test::Unit::TestCase
24
+ context 'a basic user instance' do
25
+ setup do
26
+ @user = User.new
27
+ end
28
+ should "strips all string attributes of extra whitespace" do
29
+ @user.first_name = ' ryan '
30
+ @user.last_name = ' sonnek '
31
+ @user.logged_in_at = Time.now
32
+
33
+ @user.save!
34
+ assert_equal 'ryan', @user.first_name
35
+ assert_equal 'sonnek', @user.last_name
36
+ end
37
+ end
38
+
39
+ context 'a basic post instance' do
40
+ setup do
41
+ @post = Post.new
42
+ end
43
+ should 'strip whitespace only from title' do
44
+ @post.title = ' hello world '
45
+ @post.body = ' awesome '
46
+ @post.save!
47
+
48
+ assert_equal 'hello world', @post.title
49
+ assert_equal ' awesome ', @post.body
50
+ end
51
+ end
52
+ end
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ sqlite:
2
+ :adapter: sqlite3
3
+ :dbfile: acts_as_stripped.sqlite3
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'activerecord'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'acts_as_stripped'
9
+
10
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
11
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
12
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
13
+
14
+ class Test::Unit::TestCase
15
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_stripped
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Sonnek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-25 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ryan.sonnek@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - lib/acts_as_stripped.rb
33
+ - test/acts_as_stripped_test.rb
34
+ - test/database.yml
35
+ - test/debug.log
36
+ - test/test_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/wireframe/acts_as_stripped
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: strip whitespace from string attributes (forked and pushed by Daniel Huckstep)
65
+ test_files:
66
+ - test/acts_as_stripped_test.rb
67
+ - test/test_helper.rb