Class: Skr::Address
- Defined in:
- lib/skr/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
-
#email ⇒ Object
The email address to use.
-
#ensure_not_blank ⇒ Boolean
Must the Address be filled out? The #blank? method must return false.
-
#validate_email ⇒ Boolean
Will the email be validated to meet the EmailValidator requirements?.
-
#validate_phone ⇒ Boolean
Should the phone field be validated to be not blank?.
Class Method Summary collapse
-
.blank ⇒ Address
A blank copy of an address.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Is any of (name line1 city state postal_code) blank?.
-
#email_with_name ⇒ String
The name and email formatted for inclusion in an email.
-
#enable_validations(options = {}) ⇒ Object
enable selected validations.
-
#fill_missing_from_zip ⇒ Object
fill in missing fields from postal_code using the ZipCode lookup table.
-
#seperated_name ⇒ Hash
split the name on space.
-
#to_s(include: []) ⇒ String
Address converted to string, formatted with line breaks in the typical US style of display.
Instance Attribute Details
#email ⇒ Object
The email address to use
|
# File 'lib/skr/address.rb', line 9
|
#ensure_not_blank ⇒ Boolean
Must the Address be filled out? The #blank? method must return false
21 22 23 |
# File 'lib/skr/address.rb', line 21 def ensure_not_blank @ensure_not_blank end |
#validate_email ⇒ Boolean
Will the email be validated to meet the EmailValidator requirements?
21 |
# File 'lib/skr/address.rb', line 21 attr_accessor :ensure_not_blank, :validate_email, :validate_phone |
#validate_phone ⇒ Boolean
Should the phone field be validated to be not blank?
21 |
# File 'lib/skr/address.rb', line 21 attr_accessor :ensure_not_blank, :validate_email, :validate_phone |
Class Method Details
.blank ⇒ Address
Returns a blank copy of an address.
28 29 30 |
# File 'lib/skr/address.rb', line 28 def self.blank Address.new({ name: '', line1: '', city: '', state: '', postal_code: '' }) end |
Instance Method Details
#blank? ⇒ Boolean
Returns is any of (name line1 city state postal_code) blank?.
49 50 51 |
# File 'lib/skr/address.rb', line 49 def blank? return !! %w{ name line1 city state postal_code }.detect{ |field| self[field].blank? } end |
#email_with_name ⇒ String
Returns the name and email formatted for inclusion in an email.
33 34 35 |
# File 'lib/skr/address.rb', line 33 def email_with_name "#{name} <#{email}>" end |
#enable_validations(options = {}) ⇒ Object
enable selected validations
64 65 66 67 68 |
# File 'lib/skr/address.rb', line 64 def enable_validations( = {} ) self.ensure_not_blank = true self.validate_email = [:include_email] self.validate_phone = [:include_phone] end |
#fill_missing_from_zip ⇒ Object
fill in missing fields from postal_code using the ZipCode lookup table
38 39 40 41 42 43 44 45 46 |
# File 'lib/skr/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 |
#seperated_name ⇒ Hash
split the name on space
57 58 59 |
# File 'lib/skr/address.rb', line 57 def seperated_name {:first=>name.to_s.split(' ').first, :last=> name.to_s.split(' ').last } end |
#to_s(include: []) ⇒ String
Returns 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 |
# File 'lib/skr/address.rb', line 80 def to_s( include: [] ) ret = "" %w{ name line1 line2 }.each{ |a| ret << self[a] + "\n" unless self[a].blank? } ret << city.to_s ret << ' ' + state unless state.blank? ret << ', ' + postal_code.to_s unless postal_code.blank? include = [ *include ] if include.any? ret << "\n" + include.map{ | field | self[ field ] }.join(' ') end ret end |