attr_cleaner 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/attr_cleaner.gemspec +19 -0
- data/lib/attr_cleaner.rb +2 -0
- data/lib/attr_cleaner/module_mixin.rb +31 -0
- data/lib/attr_cleaner/railtie.rb +12 -0
- data/lib/attr_cleaner/version.rb +3 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "attr_cleaner/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "attr_cleaner"
|
7
|
+
s.version = AttrCleaner::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Thomas Drake-Brockman"]
|
10
|
+
s.email = ["thomas@pixent.com.au"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = "Cleans up model attributes."
|
13
|
+
s.description = "Strips spaces from attributes, and sets empty strings to nil."
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/attr_cleaner.rb
ADDED
@@ -0,0 +1,31 @@
|
|
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 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
|
+
def attr_cleaner(args = {})
|
20
|
+
all_columns = column_names.map(&:to_sym)
|
21
|
+
|
22
|
+
only = Array(args[:only])
|
23
|
+
except = Array(args[:except])
|
24
|
+
|
25
|
+
columns = only.empty? ? all_columns : (only & all_columns)
|
26
|
+
|
27
|
+
self.attr_cleaners = (Array(self.attr_cleaners) + columns) - except
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "attr_cleaner"
|
2
|
+
require "rails"
|
3
|
+
|
4
|
+
module AttrCleaner
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "attr_cleaner.initialize" do
|
7
|
+
ActiveSupport.on_load(:active_record) do
|
8
|
+
ActiveRecord::Base.send :include, AttrCleaner::ModuleMixin
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_cleaner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Thomas Drake-Brockman
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-30 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Strips spaces from attributes, and sets empty strings to nil.
|
22
|
+
email:
|
23
|
+
- thomas@pixent.com.au
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Rakefile
|
34
|
+
- attr_cleaner.gemspec
|
35
|
+
- lib/attr_cleaner.rb
|
36
|
+
- lib/attr_cleaner/module_mixin.rb
|
37
|
+
- lib/attr_cleaner/railtie.rb
|
38
|
+
- lib/attr_cleaner/version.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: ""
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Cleans up model attributes.
|
71
|
+
test_files: []
|
72
|
+
|