normalize 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.rvmrc +3 -0
- data/Gemfile +6 -0
- data/README.md +47 -0
- data/lib/normalize.rb +39 -0
- data/normalize.gemspec +17 -0
- data/spec/lib/normalize_spec.rb +62 -0
- data/spec/spec_helper.rb +32 -0
- metadata +66 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Normalize
|
2
|
+
|
3
|
+
Normalize your attributes with ease.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
Add normalize to your `Gemfile` like this:
|
8
|
+
|
9
|
+
gem 'normalize'
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Create a directory `app/normalizers` in your Rails project. This directory will
|
14
|
+
contain your normalizers. A normalizer is a class that responds to `call` with
|
15
|
+
the value that has to be normalized and returns the normalized value.
|
16
|
+
|
17
|
+
Example URL normalizer:
|
18
|
+
|
19
|
+
class UrlNormalizer
|
20
|
+
def self.call(input)
|
21
|
+
url = input.dup
|
22
|
+
url.prepend('http://') unless url =~ %r{^[a-zA-Z]+://}
|
23
|
+
|
24
|
+
begin
|
25
|
+
uri = URI.parse(url)
|
26
|
+
uri.scheme = uri.scheme.downcase
|
27
|
+
uri.host = uri.host.downcase
|
28
|
+
rescue
|
29
|
+
return input
|
30
|
+
end
|
31
|
+
|
32
|
+
uri.to_s
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Now in your ActiveRecord model, simply use the `normalize` method to normalize
|
37
|
+
input:
|
38
|
+
|
39
|
+
class Website < ActiveRecord::Base
|
40
|
+
normalize :url, with: UrlNormalizer
|
41
|
+
end
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
Copyright 2011 Tom-Eric Gerritsen.
|
46
|
+
You may use this work without restrictions, as long as this notice is included.
|
47
|
+
The work is provided "as is" without warranty of any kind, neither express nor implied.
|
data/lib/normalize.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Normalize
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def normalize_value(value, normalizer)
|
7
|
+
normalizer.call(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def normalize(attribute, options)
|
12
|
+
normalizer = extract_normalizer(options)
|
13
|
+
|
14
|
+
class_eval do
|
15
|
+
if method_defined? "#{attribute}="
|
16
|
+
define_method "#{attribute}_with_normalizer=" do |value|
|
17
|
+
send("#{attribute}_without_normalizer=", normalize_value(value, normalizer))
|
18
|
+
end
|
19
|
+
alias_method_chain "#{attribute}=", :normalizer
|
20
|
+
else
|
21
|
+
define_method "#{attribute}=" do |value|
|
22
|
+
super(normalize_value(value, normalizer))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def extract_normalizer(options_or_normalizer)
|
29
|
+
if options_or_normalizer.respond_to?(:[])
|
30
|
+
options_or_normalizer[:with]
|
31
|
+
else
|
32
|
+
options_or_normalizer
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'active_record'
|
39
|
+
ActiveRecord::Base.send(:include, Normalize)
|
data/normalize.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'normalize'
|
5
|
+
s.version = '0.0.1'
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ['Tom-Eric Gerritsen']
|
8
|
+
s.email = 'ik@tom-eric.info'
|
9
|
+
s.homepage = 'https://github.com/tomeric/normalize'
|
10
|
+
s.summary = 'The attribute normalizer.'
|
11
|
+
s.description = 'The attribute normalizer.'
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
15
|
+
s.require_paths = ['lib']
|
16
|
+
s.add_dependency 'activerecord', '~> 3.0'
|
17
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Normalize do
|
4
|
+
it 'is included in ActiveRecord::Base' do
|
5
|
+
ActiveRecord::Base.should include(Normalize)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'instance methods' do
|
9
|
+
class NormalizedObject
|
10
|
+
include Normalize
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:normalizer) { NormalizedObject.new }
|
14
|
+
|
15
|
+
describe '#normalize_value' do
|
16
|
+
it 'normalizes the given value with the given normalizer' do
|
17
|
+
normalizer.normalize_value("string", -> input { input.upcase }).should == "STRING"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'ActiveRecord Model' do
|
23
|
+
class Book < ActiveRecord::Base
|
24
|
+
normalize :title, TitleNormalizer
|
25
|
+
normalize :author, with: AuthorNormalizer
|
26
|
+
normalize :isbn, with: -> isbn { isbn.gsub(/[^0-9]/, '') }
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:object) { Book.new }
|
30
|
+
|
31
|
+
it 'normalizes values with a normalizer class' do
|
32
|
+
object.title = 'A Bugs Life'
|
33
|
+
object.title.should == 'A BUGS LIFE'
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'normalizes values with a normalizer class specified with an options hash' do
|
37
|
+
object.author = 'jack jones'
|
38
|
+
object.author.should == 'Jack Jones'
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'normalizes values with a lambda specified with an options hash' do
|
42
|
+
object.isbn = '123-456-7890'
|
43
|
+
object.isbn.should == '1234567890'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'Ruby Object' do
|
48
|
+
class Magazine
|
49
|
+
include Normalize
|
50
|
+
|
51
|
+
attr_accessor :title
|
52
|
+
normalize :title, TitleNormalizer
|
53
|
+
end
|
54
|
+
|
55
|
+
let(:object) { Magazine.new }
|
56
|
+
|
57
|
+
it 'normalizes values with a normalizer class' do
|
58
|
+
object.title = 'A Bugs Life'
|
59
|
+
object.title.should == 'A BUGS LIFE'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'normalize'
|
4
|
+
|
5
|
+
ActiveRecord::Base.silence do
|
6
|
+
ActiveRecord::Migration.verbose = false
|
7
|
+
ActiveRecord::Base.configurations = { 'test' => { adapter: 'sqlite3', database: ':memory:' } }
|
8
|
+
ActiveRecord::Base.establish_connection :test
|
9
|
+
|
10
|
+
ActiveRecord::Schema.define version: 0 do
|
11
|
+
create_table 'books', force: true do |t|
|
12
|
+
t.string :title
|
13
|
+
t.string :author
|
14
|
+
t.string :isbn
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class TitleNormalizer
|
20
|
+
def self.call(title)
|
21
|
+
title.upcase
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class AuthorNormalizer
|
26
|
+
def self.call(author)
|
27
|
+
author.split(' ').map do |name|
|
28
|
+
name[0] = name[0].upcase
|
29
|
+
name
|
30
|
+
end.join(' ')
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: normalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tom-Eric Gerritsen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70140813674080 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70140813674080
|
25
|
+
description: The attribute normalizer.
|
26
|
+
email: ik@tom-eric.info
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- .rspec
|
33
|
+
- .rvmrc
|
34
|
+
- Gemfile
|
35
|
+
- README.md
|
36
|
+
- lib/normalize.rb
|
37
|
+
- normalize.gemspec
|
38
|
+
- spec/lib/normalize_spec.rb
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
homepage: https://github.com/tomeric/normalize
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.10
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: The attribute normalizer.
|
64
|
+
test_files:
|
65
|
+
- spec/lib/normalize_spec.rb
|
66
|
+
- spec/spec_helper.rb
|