normalize_attributes 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ecbab7887f7bae99d0a78cb8c72a7033396ed85
4
- data.tar.gz: 48cbb01e5b9c2a00fd45f45f9c998220c56ab7cd
3
+ metadata.gz: 72af3bc8bb1e9bb0dc8fe7372af893e57d897dfa
4
+ data.tar.gz: 6dcfeb6c02fefddf3de4cc085a56c6b67659e078
5
5
  SHA512:
6
- metadata.gz: 0bd5a60696dc64dcba815185b353a74cfed8b0f879f2c428cc33fc5d5237f337e7bfb803e9ed53c725bcba08386f95455da77ae90750c81a06f0d85d647d28f8
7
- data.tar.gz: 9e8520403275a7587674b17522fd3a3a2b19df2cee2a82bb2cb17e2f001f1c9d0c149793137b745f225e1a65d5243f1965b5b9ee61e883ed4d8feae8ca8c557f
6
+ metadata.gz: e2bcdfedf789bbff11d21b70edff37315c156365082e6b2b42a596dee4d48a8912d6703f6081842747ea0bf9896bbe235eef2f31212058f1394376063124905a
7
+ data.tar.gz: 2682671873ad1ac20eab0360ce0440a7a44157dbdd9615e3c7dfbe2f3859732fcbe9a15b6c90f1cb274681b1e30185847aa4292bbfc130f2ddb901e3f09e1b29
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ sudo: false
3
+ script: bundle exec rspec
4
+ cache: bundler
5
+ rvm:
6
+ - '2.2'
7
+ - '2.1'
8
+ - '2.0'
9
+ addons:
10
+ code_climate:
11
+ repo_token:
12
+ secure: "q+eMK+hnUjamNYDaNY7MMZSnpXeawGOZhbyDK3Fz3fQRUC9TvbuIaUh4Ue9IvyGgrabjD7TGZPN9fJueCAOBGQZ2GXKexdvhuLnl3pftWIIqK/Ea6ZhSanAIqJnC+f4VHsna7IV1LEq6ffatLTlsMic8lyokaMJbEIGhc1Jtz7w="
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Normalize Attributes
2
+
3
+ [![Build Status](https://travis-ci.org/fnando/normalize_attributes.svg)](https://travis-ci.org/fnando/normalize_attributes)
4
+ [![Code Climate](https://codeclimate.com/github/fnando/normalize_attributes/badges/gpa.svg)](https://codeclimate.com/github/fnando/normalize_attributes)
5
+ [![Test Coverage](https://codeclimate.com/github/fnando/normalize_attributes/badges/coverage.svg)](https://codeclimate.com/github/fnando/normalize_attributes)
6
+
7
+ Sometimes you want to normalize data before saving it to the database like down casing e-mails, removing spaces and so on. This Rails plugin allows you to do so in a simple way.
8
+
9
+ ## Usage
10
+
11
+ To install:
12
+
13
+ gem install normalize_attributes
14
+
15
+ Then on your model:
16
+
17
+ ```ruby
18
+ class User < ActiveRecord::Base
19
+ normalize :email, :with => :downcase
20
+ end
21
+ ```
22
+
23
+ The example above will normalize your `:email` attribute on the `before_save` callback.
24
+
25
+ You can specify multiple attributes
26
+
27
+ ```ruby
28
+ normalize :email, :username, :with => :downcase
29
+ ```
30
+
31
+ You can use a block
32
+
33
+ ```ruby
34
+ normalize :name do |value|
35
+ value.squish
36
+ end
37
+ ```
38
+
39
+ You can combine both
40
+
41
+ ```ruby
42
+ normalize :name, :with => :downcase do |value|
43
+ value.squish
44
+ end
45
+ ```
46
+
47
+ The `squish` method is the default normalizer for strings. All you need to is specify the attribute:
48
+
49
+ ```ruby
50
+ normalize :content
51
+ ```
52
+
53
+ The `compact` method is the default normalizer for arrays (when using `serialize` method):
54
+
55
+ ```ruby
56
+ class User < ActiveRecord::Base
57
+ serialize :preferences, Array
58
+ normalize :preferences
59
+ end
60
+ ```
61
+
62
+ The `normalize` method is aliased as `normalize_attributes`, `normalize_attribute`, `normalize_attr`, and `normalize_attrs`.
63
+
64
+ You can normalize the attribute before type casting; this is specially useful for normalizing
65
+ dates and numbers.
66
+
67
+ ```ruby
68
+ class Product
69
+ normalize(:price, :raw => true) {|v| Money.new(v).to_f}
70
+ end
71
+ ```
72
+
73
+ ## Maintainer
74
+
75
+ * Nando Vieira (http://nandovieira.com.br)
76
+
77
+ ## License:
78
+
79
+ (The MIT License)
80
+
81
+ Permission is hereby granted, free of charge, to any person obtaining
82
+ a copy of this software and associated documentation files (the
83
+ 'Software'), to deal in the Software without restriction, including
84
+ without limitation the rights to use, copy, modify, merge, publish,
85
+ distribute, sublicense, and/or sell copies of the Software, and to
86
+ permit persons to whom the Software is furnished to do so, subject to
87
+ the following conditions:
88
+
89
+ The above copyright notice and this permission notice shall be
90
+ included in all copies or substantial portions of the Software.
91
+
92
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
93
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
94
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
95
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
96
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
97
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
98
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -71,7 +71,11 @@ module NormalizeAttributes
71
71
  end
72
72
  end
73
73
 
74
- write_attribute name, value
74
+ begin
75
+ write_attribute name, value
76
+ rescue ActiveModel::MissingAttributeError
77
+ send :"#{name}=", value
78
+ end
75
79
  end
76
80
  end
77
81
  end
@@ -1,8 +1,8 @@
1
1
  module NormalizeAttributes
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 1
5
- PATCH = 7
4
+ MINOR = 2
5
+ PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -19,4 +19,5 @@ Gem::Specification.new do |s|
19
19
  s.add_development_dependency "rspec"
20
20
  s.add_development_dependency "sqlite3-ruby"
21
21
  s.add_development_dependency "actionpack"
22
+ s.add_development_dependency "codeclimate-test-reporter"
22
23
  end
@@ -109,4 +109,13 @@ describe "Normalize Attributes" do
109
109
  user.save
110
110
  }.to_not raise_error
111
111
  end
112
+
113
+ it "should normalize attributes that are not backed by database columns" do
114
+ User.normalize :nickname, :with => :downcase
115
+
116
+ user = User.new(:username => "john@doe.com")
117
+ user.nickname = "JOHNNY D"
118
+ expect { user.save }.to_not raise_error
119
+ expect(user.nickname).to eq("johnny d")
120
+ end
112
121
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
1
4
  require "rspec"
2
5
  require "normalize_attributes"
3
6
 
data/spec/support/user.rb CHANGED
@@ -2,6 +2,8 @@ class User < ActiveRecord::Base
2
2
  has_many :tokens
3
3
  serialize :preferences
4
4
 
5
+ attr_accessor :nickname
6
+
5
7
  def normalize_username(username)
6
8
  username.downcase
7
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: normalize_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-17 00:00:00.000000000 Z
11
+ date: 2015-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Normalize ActiveRecord attributes
70
84
  email:
71
85
  - fnando.vieira@gmail.com
@@ -75,8 +89,9 @@ extra_rdoc_files: []
75
89
  files:
76
90
  - ".gitignore"
77
91
  - ".rspec"
92
+ - ".travis.yml"
78
93
  - Gemfile
79
- - README.rdoc
94
+ - README.md
80
95
  - Rakefile
81
96
  - lib/normalize_attributes.rb
82
97
  - lib/normalize_attributes/active_record.rb
data/README.rdoc DELETED
@@ -1,80 +0,0 @@
1
- = Normalize Attributes
2
-
3
- Sometimes you want to normalize data before saving it to the database like down casing e-mails, removing spaces and so on. This Rails plugin allows you to do so in a simple way.
4
-
5
- == Usage
6
-
7
- To install:
8
-
9
- gem install normalize_attributes
10
-
11
- Then on your model:
12
-
13
- class User < ActiveRecord::Base
14
- normalize :email, :with => :downcase
15
- end
16
-
17
- The example above will normalize your <tt>:email</tt> attribute on the <tt>before_save</tt> callback.
18
-
19
- You can specify multiple attributes
20
-
21
- normalize :email, :username, :with => :downcase
22
-
23
- You can use a block
24
-
25
- normalize :name do |value|
26
- value.squish
27
- end
28
-
29
- You can combine both
30
-
31
- normalize :name, :with => :downcase do |value|
32
- value.squish
33
- end
34
-
35
- The <tt>squish</tt> method is the default normalizer for strings. All you need to is specify the attribute:
36
-
37
- normalize :content
38
-
39
- The <tt>compact</tt> method is the default normalizer for arrays (when using <tt>serialize</tt> method):
40
-
41
- class User < ActiveRecord::Base
42
- serialize :preferences, Array
43
- normalize :preferences
44
- end
45
-
46
- The <tt>normalize</tt> method is aliased as <tt>normalize_attributes</tt>, <tt>normalize_attribute</tt>, <tt>normalize_attr</tt>, and <tt>normalize_attrs</tt>.
47
-
48
- You can normalize the attribute before type casting; this is specially useful for normalizing
49
- dates and numbers.
50
-
51
- class Product
52
- normalize(:price, :raw => true) {|v| Money.new(v).to_f}
53
- end
54
-
55
- == Maintainer
56
-
57
- * Nando Vieira (http://nandovieira.com.br)
58
-
59
- == License:
60
-
61
- (The MIT License)
62
-
63
- Permission is hereby granted, free of charge, to any person obtaining
64
- a copy of this software and associated documentation files (the
65
- 'Software'), to deal in the Software without restriction, including
66
- without limitation the rights to use, copy, modify, merge, publish,
67
- distribute, sublicense, and/or sell copies of the Software, and to
68
- permit persons to whom the Software is furnished to do so, subject to
69
- the following conditions:
70
-
71
- The above copyright notice and this permission notice shall be
72
- included in all copies or substantial portions of the Software.
73
-
74
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
75
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
76
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
77
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
78
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
79
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
80
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.