Class: Normalic::PhoneNumber
- Inherits:
-
Object
- Object
- Normalic::PhoneNumber
- Defined in:
- lib/normalic/phone_number.rb
Overview
only handles U.S. phone numbers
Instance Attribute Summary collapse
-
#npa ⇒ Object
Returns the value of attribute npa.
-
#nxx ⇒ Object
Returns the value of attribute nxx.
-
#slid ⇒ Object
Returns the value of attribute slid.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](field_name) ⇒ Object
- #[]=(field_name, value) ⇒ Object
-
#initialize(fields = {}) ⇒ PhoneNumber
constructor
A new instance of PhoneNumber.
- #to_s ⇒ Object
Constructor Details
#initialize(fields = {}) ⇒ PhoneNumber
Returns a new instance of PhoneNumber.
6 7 8 9 10 |
# File 'lib/normalic/phone_number.rb', line 6 def initialize(fields={}) @npa = fields[:npa] @nxx = fields[:nxx] @slid = fields[:slid] end |
Instance Attribute Details
#npa ⇒ Object
Returns the value of attribute npa.
4 5 6 |
# File 'lib/normalic/phone_number.rb', line 4 def npa @npa end |
#nxx ⇒ Object
Returns the value of attribute nxx.
4 5 6 |
# File 'lib/normalic/phone_number.rb', line 4 def nxx @nxx end |
#slid ⇒ Object
Returns the value of attribute slid.
4 5 6 |
# File 'lib/normalic/phone_number.rb', line 4 def slid @slid end |
Class Method Details
.parse(raw) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/normalic/phone_number.rb', line 12 def self.parse(raw) digs = raw.to_s.gsub(/[^\d]/,'') while digs != (trim = digs.gsub(/^[01]/,'')) digs = trim end if digs.length < 10 return nil end self.new(:npa => digs[0,3], :nxx => digs[3,3], :slid => digs[6,4]) end |
Instance Method Details
#==(other) ⇒ Object
45 46 47 |
# File 'lib/normalic/phone_number.rb', line 45 def ==(other) self.to_s == other.to_s ? true : false end |
#[](field_name) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/normalic/phone_number.rb', line 29 def [](field_name) begin self.send(field_name.to_s) rescue NoMethodError => e nil end end |
#[]=(field_name, value) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/normalic/phone_number.rb', line 37 def []=(field_name, value) begin self.send("#{field_name}=", value) rescue NoMethodError => e nil end end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/normalic/phone_number.rb', line 25 def to_s "#{npa} #{nxx} #{slid}" end |