normalize_attributes 0.1.4 → 0.1.5

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.
@@ -40,7 +40,7 @@ module NormalizeAttributes
40
40
  options = self.class.normalize_attributes_options || {}
41
41
 
42
42
  options.each do |attr_name, normalizers|
43
- value = self.send(attr_name)
43
+ value = self.send("#{attr_name}_before_type_cast")
44
44
 
45
45
  if normalizers.empty?
46
46
  case value
@@ -56,6 +56,8 @@ module NormalizeAttributes
56
56
  value = normalizer.call(value)
57
57
  elsif value.respond_to?(normalizer)
58
58
  value = value.send(normalizer)
59
+ else
60
+ value = send(normalizer, value)
59
61
  end
60
62
  end
61
63
 
@@ -2,7 +2,7 @@ module NormalizeAttributes
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 4
5
+ PATCH = 5
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{normalize_attributes}
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nando Vieira"]
@@ -33,6 +33,23 @@ describe "Normalize Attributes" do
33
33
  user.email.should == "john@doe.com"
34
34
  end
35
35
 
36
+ it "should apply instance method" do
37
+ User.normalize_attribute(:username, :with => :normalize_username)
38
+ user = User.create(:username => "JOHNDOE")
39
+
40
+ user.username.should == "johndoe"
41
+ end
42
+
43
+ it "should use value before type casting" do
44
+ User.normalize_attribute(:age) do |v|
45
+ v.should == "1.2"
46
+ v.to_f * 10
47
+ end
48
+
49
+ user = User.create(:age => "1.2")
50
+ user.age.should == 12
51
+ end
52
+
36
53
  it "should combine both method names and procs as normalization methods" do
37
54
  User.normalize_attribute(:email, :with => :downcase) {|v| v.reverse }
38
55
  user = User.create(:email => "JOHN@DOE.COM")
data/spec/schema.rb CHANGED
@@ -2,6 +2,7 @@ ActiveRecord::Schema.define(:version => 0) do
2
2
  create_table :users do |t|
3
3
  t.string :email, :username
4
4
  t.text :preferences
5
+ t.integer :age, :null => false, :default => 0
5
6
  end
6
7
 
7
8
  create_table :tokens do |t|
data/spec/support/user.rb CHANGED
@@ -1,4 +1,8 @@
1
1
  class User < ActiveRecord::Base
2
2
  has_many :tokens
3
3
  serialize :preferences
4
+
5
+ def normalize_username(username)
6
+ username.downcase
7
+ end
4
8
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nando Vieira