Class: Skr::Address

Inherits:
Model
  • Object
show all
Defined in:
lib/skr/models/address.rb

Overview

A postal address with optional email and phone number By default all fields may be left blank.

Validations may be selectively enabled by using the #enable_validations method

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#emailObject

The email address to use



# File 'lib/skr/models/address.rb', line 9

#ensure_not_blankBoolean

Must the Address be filled out? The #blank? method must return false

Returns:

  • (Boolean)


21
22
23
# File 'lib/skr/models/address.rb', line 21

def ensure_not_blank
  @ensure_not_blank
end

#validate_emailBoolean

Will the email be validated to meet the EmailValidator requirements?

Returns:

  • (Boolean)


21
# File 'lib/skr/models/address.rb', line 21

attr_accessor :ensure_not_blank, :validate_email, :validate_phone

#validate_phoneBoolean

Should the phone field be validated to be not blank?

Returns:

  • (Boolean)


21
# File 'lib/skr/models/address.rb', line 21

attr_accessor :ensure_not_blank, :validate_email, :validate_phone

Class Method Details

.blankAddress

Returns a blank copy of an address.

Returns:

  • (Address)

    a blank copy of an address



28
29
30
# File 'lib/skr/models/address.rb', line 28

def self.blank
    Address.new({ name: '', line1: '', city: '', state: '', postal_code: '' })
end

Instance Method Details

#email_with_nameString

Returns the name and email formatted for inclusion in an email.

Returns:

  • (String)

    the name and email formatted for inclusion in an email



33
34
35
# File 'lib/skr/models/address.rb', line 33

def email_with_name
    "#{name} <#{email}>"
end

#enable_validations(options = {}) ⇒ Object

enable selected validations

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :include_email (Boolean)

    should the email be validated

  • :include_phone (Boolean)

    should the phone number be validated



64
65
66
67
68
# File 'lib/skr/models/address.rb', line 64

def enable_validations( options = {} )
    self.ensure_not_blank = true
    self.validate_email = options[:include_email]
    self.validate_phone = options[:include_phone]
end

#fill_missing_from_zipObject

fill in missing fields from postal_code using the ZipCode lookup table



38
39
40
41
42
43
44
45
46
# File 'lib/skr/models/address.rb', line 38

def fill_missing_from_zip
    if ( self.postal_code.present? &&
         ( self.city.blank? || self.state.blank? ) &&
         zc = ZipCode.find_by_code( self.postal_code )
       )
        self.city  ||= zc.city
        self.state ||= zc.state
    end
end

#incomplete?Boolean

Returns is all of (name line1 city state postal_code) blank?.

Returns:

  • (Boolean)

    is all of (name line1 city state postal_code) blank?



49
50
51
# File 'lib/skr/models/address.rb', line 49

def incomplete?
    return %w{ name line1 city state postal_code }.all?{ |field| self[field].blank? }
end

#seperated_nameHash

split the name on space

Returns:

  • (Hash)
    • :first [String] Portion of name before first space

    • :last [String] Portion of name after last space



57
58
59
# File 'lib/skr/models/address.rb', line 57

def seperated_name
    {:first=>name.to_s.split(' ').first, :last=> name.to_s.split(' ').last }
end

#to_s(include: [], without: []) ⇒ String

Returns Address converted to string, formatted with line breaks in the typical US style of display.

Examples:

address = Address.new( name: 'Bob\s Uncle',phone: '877-555-5555', line1: 'PO Box 87',
                       city: 'Nowhereville', state: 'Urgandishly' postal_code: 'ASCN 1ZZ' )
address.to_s( :include => :phone ) #=>
        Bob's Uncle
        PO Box 87
        Nowhereville, Urgandishly ASCN 1ZZ
        877-5550-5555

Parameters:

  • include (Array) (defaults to: [])

    list of extra fields to include in the address

Returns:

  • (String)

    Address converted to string, formatted with line breaks in the typical US style of display



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/skr/models/address.rb', line 80

def to_s( include: [], without: [] )
    ret = ""
    [ :name, :line1, :line2 ].each{ |a|
        ret << self[a] + "\n" unless without.include?(a) || self[a].blank?
    }
    ret << city.to_s unless without.include?(:city)
    ret << ' ' + state unless without.include?(:state) || state.blank?
    ret << ', ' + postal_code.to_s unless without.include?(:postal_code) || postal_code.blank?
    include = [ *include ]
    if include.any?
        ret << "\n" + include.map{ | field | self[ field ] }.join("\n")
    end
    ret
end