Permalink
Browse files

Add ssn_format and supporting formatters/tests.

  • Loading branch information...
1 parent a57d76a commit a0db2cbdd2b23cb42bfae538cc5bacaa0d3979f2 @smoil committed Mar 4, 2012
View
@@ -7,10 +7,16 @@ def phone_format(attribute, opts = {})
getter = opts[:getter] ? opts[:getter] : InFormat::Formatters::Phone::DEFAULT_GETTER
in_format(attribute, setter: setter, getter: getter)
- end # end phone_format
+ end
- def in_format(attribute, opts = {})
+ def ssn_format(attribute, opts = {})
+ setter = opts[:setter] ? opts[:setter] : InFormat::Formatters::Ssn::DEFAULT_SETTER
+ getter = opts[:getter] ? opts[:getter] : InFormat::Formatters::Ssn::DEFAULT_GETTER
+
+ in_format(attribute, setter: setter, getter: getter)
+ end
+ def in_format(attribute, opts = {})
if opts[:setter]
define_method("#{attribute}=") do |value|
self[attribute.to_sym] = opts[:setter].call(value)
@@ -1 +1,2 @@
require "in_format/formatters/phone"
+require "in_format/formatters/ssn"
@@ -0,0 +1,16 @@
+module InFormat
+ module Formatters
+ module Ssn
+ DEFAULT_SETTER = lambda { |value| value.to_s.gsub(/[^\d]/, "") }
+
+ DEFAULT_GETTER = lambda do |value|
+ if value =~ /^\d{9}\z/
+ "#{value[0..2]}-#{value[3..4]}-#{value[5..8]}"
+ else
+ value
+ end
+ end
+
+ end
+ end
+end
@@ -7,4 +7,8 @@ class Beaver < ActiveRecord::Base
phone_format :phone_custom_set, setter: Proc.new {|v| v.gsub("3", "9") }
phone_format :phone_custom_get, getter: Proc.new {|v| v.gsub("9", "P") }
+ ssn_format :ssn
+ ssn_format :ssn_custom_set, setter: Proc.new {|v| v.gsub("3", "9") }
+ ssn_format :ssn_custom_get, getter: Proc.new {|v| v.gsub("9", "P") }
+
end
@@ -8,6 +8,8 @@ def change
t.string :phone_custom_set
t.string :phone_custom_get
t.string :ssn
+ t.string :ssn_custom_set
+ t.string :ssn_custom_get
t.timestamps
end
end
@@ -21,6 +21,8 @@
t.string "phone_custom_set"
t.string "phone_custom_get"
t.string "ssn"
+ t.string "ssn_custom_set"
+ t.string "ssn_custom_get"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
@@ -0,0 +1,54 @@
+require "spec_helper"
+
+describe "ssn_format" do
+
+ describe "a field using ssn_format" do
+ context "that is unitialized" do
+ let(:beaver) { Beaver.new }
+
+ it "should return nil when read through getter" do
+ beaver.ssn.should be_nil
+ end
+
+ it "should return nil when read without getter" do
+ beaver.ssn(true).should be_nil
+ end
+ end
+
+ context "initialized with '222 22 2222'" do
+ let(:beaver) { Beaver.new(ssn: "222 22 2222") }
+
+ it "should store a value processed by the default setter" do
+ beaver.ssn(true).should eq("222222222")
+ end
+
+ it "should return a value processed by the default getter" do
+ beaver.ssn.should eq("222-22-2222")
+ end
+ end
+ end
+
+ describe "a field using ssn_format with a custom setter" do
+ let(:beaver) { Beaver.new(ssn_custom_set: "333333333") }
+
+ it "should store a value processed by the custom setter" do
+ beaver.ssn_custom_set(true).should eq("999999999")
+ end
+
+ it "should return a value processed by the default getter" do
+ beaver.ssn_custom_set.should eq("999-99-9999")
+ end
+ end
+
+ describe "a field using ssn_format with a custom getter" do
+ let(:beaver) { Beaver.new(ssn_custom_get: "999 99 9999") }
+
+ it "should store a value processed by the default setter" do
+ beaver.ssn_custom_get(true).should eq("999999999")
+ end
+
+ it "should return a value processed by the custom getter" do
+ beaver.ssn_custom_get.should eq("PPPPPPPPP")
+ end
+ end
+end

0 comments on commit a0db2cb

Please sign in to comment.