Sha256: 6f86b0191e6f393d3a44aeb65656f64cf9c04d4a065f1a786e40007a6571954b

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Ssn
  module ActiveRecordExtensions
    def self.included( base )
      base.extend ActsMethods
    end

    module ActsMethods
      def has_ssn( *args )
        unless included_modules.include? InstanceMethods
          self.class_eval { extend ClassMethods }
          include InstanceMethods
          validates_length_of :raw_ssn, :maximum => 9, :allow_blank => true
          validates_format_of :raw_ssn, :with => /^[0-9]{9}$/, :allow_blank => true
        end

        initialize_has_ssn_from_args args
      end

      alias_method :has_ssns, :has_ssn
    end

    module ClassMethods
      def initialize_has_ssn_from_args( args )
        if args.first.is_a? Symbol
          initialize_has_ssn_from_string args.first.to_s
        elsif args.first.is_a? String
          initialize_has_ssn_from_string args.first
        elsif args.first.is_a? Hash
          initialize_has_ssn_from_hash args.first
        else
          raise 'has_ssn can only accept a string, symbol or hash of strings or symbols'
        end
      end

      def initialize_has_ssn_from_string( str )
        define_method str do
          return raw_ssn.blank? ? nil : raw_ssn.gsub( /^([0-9]{3})([0-9]{2})([0-9]{4})$/,"\\1-\\2-\\3" )
        end

        define_method "#{str}=" do |value|
          unless /^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$/.match( value )
            raise 'Invalid SSN format provided for ssn'
          end
          self.raw_ssn = value.gsub( /-/, "" )
        end
      end

      def initialize_has_ssn_from_hash( args )
      end
    end

    module InstanceMethods

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ssn-0.1.0 lib/ssn/active_record_extensions.rb