attr_cleaner 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -2
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +26 -0
- data/Appraisals +5 -0
- data/Gemfile +8 -1
- data/README.md +58 -0
- data/Rakefile +1 -0
- data/attr_cleaner.gemspec +6 -6
- data/gemfiles/rails_4.2.gemfile +14 -0
- data/gemfiles/rails_5.0.gemfile +14 -0
- data/lib/attr_cleaner/model.rb +25 -0
- data/lib/attr_cleaner/railtie.rb +1 -1
- data/lib/attr_cleaner/version.rb +1 -1
- data/lib/attr_cleaner.rb +3 -3
- data/spec/attr_cleaner/model_spec.rb +52 -0
- data/spec/spec_helper.rb +13 -0
- metadata +49 -62
- data/.rvmrc +0 -7
- data/Gemfile.lock +0 -32
- data/README.rdoc +0 -46
- data/lib/attr_cleaner/module_mixin.rb +0 -36
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 13fd121c241acea325b5ab89c95aefdaabc91a22
|
4
|
+
data.tar.gz: f4dd8fca1416c874e971899ab4559a62717a1beb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 02dbd6aab1d253c9a137e03a6edeed70b0f76242b8c5af4cb737173b4da15cdb7bba2c343fcb1f9fb6c09ecade4651ad0e19de7a52f4d47b11b78d3a4faa7d75
|
7
|
+
data.tar.gz: b0206eee8379393eb280f870ea2748933483ccfe87c2bb81804d464e89b825dea011abea2ec12566cbd2f7e7a70294c1f99a575bd71f37a87128d3394be98202
|
data/.gitignore
CHANGED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
attr_cleaner
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
sudo: false
|
4
|
+
cache: bundler
|
5
|
+
|
6
|
+
env:
|
7
|
+
global:
|
8
|
+
- TRAVIS_CI=1
|
9
|
+
|
10
|
+
rvm:
|
11
|
+
- 1.9.3
|
12
|
+
- 2.3.3
|
13
|
+
|
14
|
+
gemfile:
|
15
|
+
- gemfiles/rails_4.2.gemfile
|
16
|
+
- gemfiles/rails_5.0.gemfile
|
17
|
+
|
18
|
+
matrix:
|
19
|
+
exclude:
|
20
|
+
- rvm: 1.9.3
|
21
|
+
gemfile: gemfiles/rails_5.0.gemfile
|
22
|
+
|
23
|
+
before_install: gem update bundler
|
24
|
+
bundler_args: --without tools
|
25
|
+
|
26
|
+
script: bundle exec rspec -b
|
data/Appraisals
ADDED
data/Gemfile
CHANGED
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# AttrCleaner
|
2
|
+
|
3
|
+
Cleans up your model attributes. Strips leading and trailing space, and turns
|
4
|
+
empty strings into `nil`.
|
5
|
+
|
6
|
+
Tested against Rails 4.2 and 5.0.
|
7
|
+
|
8
|
+
![Travis CI](https://travis-ci.org/thomasfedb/attr_cleaner.svg?branch=master)
|
9
|
+
|
10
|
+
## Install
|
11
|
+
|
12
|
+
If you're using Rails with ActiveRecord all you need to do is:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem "attr_cleaner"
|
16
|
+
```
|
17
|
+
|
18
|
+
AttrCleaner will also work with any ORM that writes attributes via a method
|
19
|
+
called `write_attribute`.
|
20
|
+
|
21
|
+
To get AttrCleaner working with your ORM just include it into your models:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
include AttrCleaner::Model
|
25
|
+
```
|
26
|
+
|
27
|
+
AttrCleaner can also be used outside of Rails if ActiveSupport 3 is avalable.
|
28
|
+
If you're using AttrCleaner outside of Rails, and it detects ActiveRecord is
|
29
|
+
allready loaded, it will include itself into ActiveRecod. Make sure ActiveRecord
|
30
|
+
is loaded first.
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
Simply add this to any model you want to clean:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
attr_cleaner
|
38
|
+
```
|
39
|
+
|
40
|
+
You can restrict the cleaning to specific attributes with `:only` and `:except`:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
attr_cleaner only: :name
|
44
|
+
attr_cleaner only: [:name, :age]
|
45
|
+
attr_cleaner except: :age
|
46
|
+
attr_cleaner except: [:age, :price]
|
47
|
+
```
|
48
|
+
|
49
|
+
Settings are be inherited, so sometimes you may need both `:only` and `:except`:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# Remove :age from the list, add :name
|
53
|
+
attr_cleaner only: :name, except: :age
|
54
|
+
```
|
55
|
+
|
56
|
+
## Copyright
|
57
|
+
|
58
|
+
Copyright © 2010 Thomas Drake-Brockman
|
data/Rakefile
CHANGED
data/attr_cleaner.gemspec
CHANGED
@@ -7,15 +7,15 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = AttrCleaner::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Thomas Drake-Brockman"]
|
10
|
-
s.email = ["
|
11
|
-
s.homepage = "https://
|
10
|
+
s.email = ["thom@sfedb.com"]
|
11
|
+
s.homepage = "https://github.com/thomasfedb/attr_cleaner"
|
12
12
|
s.summary = "Cleans up model attributes."
|
13
|
-
s.description = "Strips spaces from attributes, and sets empty strings to nil.
|
13
|
+
s.description = "Strips spaces from attributes, and sets empty strings to nil."
|
14
14
|
|
15
15
|
s.files = `git ls-files`.split("\n")
|
16
|
-
s.test_files = `git ls-files --
|
16
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
17
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
18
|
s.require_paths = ["lib"]
|
19
|
-
|
20
|
-
s.add_dependency('activesupport', '
|
19
|
+
|
20
|
+
s.add_dependency('activesupport', '>= 4.2')
|
21
21
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "rspec"
|
6
|
+
gem "jamjar"
|
7
|
+
gem "activerecord", "~> 4.2", :require => "active_record"
|
8
|
+
|
9
|
+
group :tools do
|
10
|
+
gem "appraisal"
|
11
|
+
gem "byebug"
|
12
|
+
end
|
13
|
+
|
14
|
+
gemspec :path => "../"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "http://rubygems.org"
|
4
|
+
|
5
|
+
gem "rspec"
|
6
|
+
gem "jamjar"
|
7
|
+
gem "activerecord", "~> 5.0", :require => "active_record"
|
8
|
+
|
9
|
+
group :tools do
|
10
|
+
gem "appraisal"
|
11
|
+
gem "byebug"
|
12
|
+
end
|
13
|
+
|
14
|
+
gemspec :path => "../"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module AttrCleaner
|
2
|
+
module Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
alias_method_chain :write_attribute, :cleaner
|
7
|
+
class_attribute :attr_cleaners
|
8
|
+
end
|
9
|
+
|
10
|
+
def write_attribute_with_cleaner(attr_name, value)
|
11
|
+
if Array(attr_cleaners).include?(attr_name.to_sym) && value.is_a?(String)
|
12
|
+
value = value.strip
|
13
|
+
value = nil if value.empty?
|
14
|
+
end
|
15
|
+
|
16
|
+
write_attribute_without_cleaner(attr_name, value)
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
def attr_cleaner(*columns)
|
21
|
+
self.attr_cleaners = columns
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/attr_cleaner/railtie.rb
CHANGED
@@ -5,7 +5,7 @@ module AttrCleaner
|
|
5
5
|
class Railtie < Rails::Railtie
|
6
6
|
initializer 'attr_cleaner.include_into_activerecord' do |app|
|
7
7
|
ActiveSupport.on_load(:active_record) do
|
8
|
-
ActiveRecord::Base.send(:include, AttrCleaner::
|
8
|
+
ActiveRecord::Base.send(:include, AttrCleaner::Model)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
data/lib/attr_cleaner/version.rb
CHANGED
data/lib/attr_cleaner.rb
CHANGED
@@ -3,7 +3,7 @@ require "active_support/core_ext/module/aliasing"
|
|
3
3
|
require "active_support/core_ext/class/attribute"
|
4
4
|
require "active_support/concern"
|
5
5
|
|
6
|
-
require "attr_cleaner/
|
6
|
+
require "attr_cleaner/model"
|
7
7
|
|
8
8
|
# If not using active_record then include AttrCleaner::ModuleMixin.
|
9
9
|
|
@@ -12,5 +12,5 @@ if defined?(Rails::Railtie)
|
|
12
12
|
require "attr_cleaner/railtie"
|
13
13
|
else
|
14
14
|
# If used outside of rails, just check for ActiveRecord::Base
|
15
|
-
ActiveRecord::Base.send(:include, AttrCleaner::
|
16
|
-
end
|
15
|
+
ActiveRecord::Base.send(:include, AttrCleaner::Model) if defined?(ActiveRecord::Base)
|
16
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe AttrCleaner::Model do
|
4
|
+
let(:model) do
|
5
|
+
JamJar.model do
|
6
|
+
column :title, :string
|
7
|
+
column :body, :text
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:instance) { model.new }
|
12
|
+
|
13
|
+
describe ".attr_cleaner" do
|
14
|
+
context "called with attributes" do
|
15
|
+
before { model.attr_cleaner :title, :body }
|
16
|
+
|
17
|
+
specify { expect(model.attr_cleaners).to eq [:title, :body] }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#write_attribute_with_cleaner" do
|
22
|
+
context "with no cleaners" do
|
23
|
+
context "a padding string is assigned" do
|
24
|
+
before { instance.title = " Padded Title " }
|
25
|
+
|
26
|
+
specify { expect(instance.title).to eq " Padded Title " }
|
27
|
+
end
|
28
|
+
|
29
|
+
context "an empy string is assigned" do
|
30
|
+
before { instance.title = " " }
|
31
|
+
|
32
|
+
specify { expect(instance.title).to eq " " }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with cleaners" do
|
37
|
+
before { model.attr_cleaner :title }
|
38
|
+
|
39
|
+
context "a padding string is assigned" do
|
40
|
+
before { instance.title = " Padded Title " }
|
41
|
+
|
42
|
+
specify { expect(instance.title).to eq "Padded Title" }
|
43
|
+
end
|
44
|
+
|
45
|
+
context "an empy string is assigned" do
|
46
|
+
before { instance.title = " " }
|
47
|
+
|
48
|
+
specify { expect(instance.title).to be_nil }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,90 +1,77 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_cleaner
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: 0.1.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
6
|
+
authors:
|
12
7
|
- Thomas Drake-Brockman
|
13
8
|
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2016-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: activesupport
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 3
|
30
|
-
- 0
|
31
|
-
- 0
|
32
|
-
version: 3.0.0
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
33
20
|
type: :runtime
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
description: Strips spaces from attributes, and sets empty strings to nil.
|
28
|
+
email:
|
29
|
+
- thom@sfedb.com
|
38
30
|
executables: []
|
39
|
-
|
40
31
|
extensions: []
|
41
|
-
|
42
32
|
extra_rdoc_files: []
|
43
|
-
|
44
|
-
|
45
|
-
- .
|
46
|
-
- .
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".ruby-gemset"
|
36
|
+
- ".ruby-version"
|
37
|
+
- ".travis.yml"
|
38
|
+
- Appraisals
|
47
39
|
- Gemfile
|
48
|
-
- Gemfile.lock
|
49
40
|
- LICENSE
|
50
|
-
- README.
|
41
|
+
- README.md
|
51
42
|
- Rakefile
|
52
43
|
- attr_cleaner.gemspec
|
44
|
+
- gemfiles/rails_4.2.gemfile
|
45
|
+
- gemfiles/rails_5.0.gemfile
|
53
46
|
- lib/attr_cleaner.rb
|
54
|
-
- lib/attr_cleaner/
|
47
|
+
- lib/attr_cleaner/model.rb
|
55
48
|
- lib/attr_cleaner/railtie.rb
|
56
49
|
- lib/attr_cleaner/version.rb
|
57
|
-
|
58
|
-
|
50
|
+
- spec/attr_cleaner/model_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
homepage: https://github.com/thomasfedb/attr_cleaner
|
59
53
|
licenses: []
|
60
|
-
|
54
|
+
metadata: {}
|
61
55
|
post_install_message:
|
62
56
|
rdoc_options: []
|
63
|
-
|
64
|
-
require_paths:
|
57
|
+
require_paths:
|
65
58
|
- lib
|
66
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
-
|
68
|
-
requirements:
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
69
61
|
- - ">="
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
-
none: false
|
76
|
-
requirements:
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
77
66
|
- - ">="
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
|
80
|
-
- 0
|
81
|
-
version: "0"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
82
69
|
requirements: []
|
83
|
-
|
84
70
|
rubyforge_project:
|
85
|
-
rubygems_version:
|
71
|
+
rubygems_version: 2.5.2
|
86
72
|
signing_key:
|
87
|
-
specification_version:
|
73
|
+
specification_version: 4
|
88
74
|
summary: Cleans up model attributes.
|
89
|
-
test_files:
|
90
|
-
|
75
|
+
test_files:
|
76
|
+
- spec/attr_cleaner/model_spec.rb
|
77
|
+
- spec/spec_helper.rb
|
data/.rvmrc
DELETED
data/Gemfile.lock
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
attr_cleaner (0.0.3)
|
5
|
-
activerecord (~> 3.0.0)
|
6
|
-
activesupport (~> 3.0.0)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: http://rubygems.org/
|
10
|
-
specs:
|
11
|
-
activemodel (3.0.3)
|
12
|
-
activesupport (= 3.0.3)
|
13
|
-
builder (~> 2.1.2)
|
14
|
-
i18n (~> 0.4)
|
15
|
-
activerecord (3.0.3)
|
16
|
-
activemodel (= 3.0.3)
|
17
|
-
activesupport (= 3.0.3)
|
18
|
-
arel (~> 2.0.2)
|
19
|
-
tzinfo (~> 0.3.23)
|
20
|
-
activesupport (3.0.3)
|
21
|
-
arel (2.0.6)
|
22
|
-
builder (2.1.2)
|
23
|
-
i18n (0.5.0)
|
24
|
-
tzinfo (0.3.23)
|
25
|
-
|
26
|
-
PLATFORMS
|
27
|
-
ruby
|
28
|
-
|
29
|
-
DEPENDENCIES
|
30
|
-
activerecord (~> 3.0.0)
|
31
|
-
activesupport (~> 3.0.0)
|
32
|
-
attr_cleaner!
|
data/README.rdoc
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
= AttrCleaner
|
2
|
-
|
3
|
-
Cleans up your model attributes. Strips leading and trailing space, and turns empty
|
4
|
-
strings into nil.
|
5
|
-
|
6
|
-
== Installing
|
7
|
-
|
8
|
-
For Rails 3 with ActiveRecord just add this to your Gemfile:
|
9
|
-
|
10
|
-
gem "attr_cleaner"
|
11
|
-
|
12
|
-
Attr cleaner will play nice with any ORM that writes attributes
|
13
|
-
via a method called `write_attribute`.
|
14
|
-
To get AttrCleaner working with your ORM add this to an initializer file:
|
15
|
-
|
16
|
-
MyOrm.send(:include, AttrCleaner::ModelMixin)
|
17
|
-
|
18
|
-
or, include it into your models:
|
19
|
-
|
20
|
-
include AttrCleaner::ModelMixin
|
21
|
-
|
22
|
-
AttrCleaner can also be used outside of Rails if ActiveSupport 3 is avalable. If you're using
|
23
|
-
AttrCleaner outside of Rails, and it detect ActiveRecord is allready loaded, it will include
|
24
|
-
itself into ActiveRecod. Make sure ActiveRecord is loaded first.
|
25
|
-
|
26
|
-
== Usage
|
27
|
-
|
28
|
-
Simply add this to any model you want to clean:
|
29
|
-
|
30
|
-
attr_cleaner
|
31
|
-
|
32
|
-
You can restrict the cleaning to specific attributes with `:only` and `:except`
|
33
|
-
|
34
|
-
attr_cleaner :only => :name
|
35
|
-
attr_cleaner :only => [:name, :age]
|
36
|
-
attr_cleaner :except => :age
|
37
|
-
attr_cleaner :except => [:age, :price]
|
38
|
-
|
39
|
-
Attributes to be cleaned can be inherited, so sometimes you may need both :only and :except
|
40
|
-
|
41
|
-
# Remove :age from the list, add :name
|
42
|
-
attr_cleaner :only => :name, :except => :age
|
43
|
-
|
44
|
-
== Copyright
|
45
|
-
|
46
|
-
Copyright (c) 2010 Thomas Drake-Brockman. See LICENSE for details.
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module AttrCleaner
|
2
|
-
module ModuleMixin
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
alias_method_chain :write_attribute, :cleaner
|
7
|
-
class_attribute :attr_cleaners
|
8
|
-
end
|
9
|
-
|
10
|
-
def write_attribute_with_cleaner(attr_name, value)
|
11
|
-
if Array(attr_cleaners).include?(attr_name.to_sym) && value.is_a?(String)
|
12
|
-
value = value.strip
|
13
|
-
value = nil if value.empty?
|
14
|
-
end
|
15
|
-
write_attribute_without_cleaner(attr_name, value)
|
16
|
-
end
|
17
|
-
|
18
|
-
module ClassMethods
|
19
|
-
# Adds attributes to the list of attributes to be cleaned
|
20
|
-
# Attributes are inherited, but can be removed from :except
|
21
|
-
#
|
22
|
-
# @param [Hash] args options hash, set :only and/or :except
|
23
|
-
# @return [Array] a array of symbols representing the attributes to be cleaned
|
24
|
-
def attr_cleaner(args = {})
|
25
|
-
all_columns = column_names.map(&:to_sym)
|
26
|
-
|
27
|
-
only = Array(args[:only])
|
28
|
-
except = Array(args[:except])
|
29
|
-
|
30
|
-
columns = only.empty? ? all_columns : (only & all_columns)
|
31
|
-
|
32
|
-
self.attr_cleaners = (Array(self.attr_cleaners) + columns) - except
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|