Class: MoteSMS::NumberList
- Inherits:
-
Object
- Object
- MoteSMS::NumberList
- Includes:
- Enumerable
- Defined in:
- lib/mote_sms/number_list.rb
Overview
List of Number instances, which transparantly is able to add new Number instances from strings, or whatever.
Implements Enumerable, thus can be used like any regular array.
Examples:
list << '+41 79 123 12 12'
list.push '044 123 12 12', cc: '41'
list.push Number.new('0800 123 12 12')
list.normalized_numbers
# => ['41791231212', '41441231212', '08001231212']
Instance Attribute Summary collapse
-
#numbers ⇒ Object
readonly
Internal numbers array.
Instance Method Summary collapse
-
#<<(number) ⇒ Object
Public: Add number to internal list, use duck typing to detect if it appears to be a number instance or not.
-
#each(&block) ⇒ Object
Public: Yields each Number instance from this number list to the provided block.
-
#empty? ⇒ Boolean
(also: #blank?)
Public: Conform to arrayish behavior.
-
#initialize ⇒ NumberList
constructor
Public: Create a new number list instance.
-
#length ⇒ Object
(also: #size)
Public: Count of numbers in the list.
-
#normalized_numbers ⇒ Object
Public: Fetch numbers using to_number.
-
#push(*numbers) ⇒ Object
Public: Add multiple numbers, with optional options hash which can be used to set country options etc.
Constructor Details
#initialize ⇒ NumberList
Public: Create a new number list instance.
27 28 29 |
# File 'lib/mote_sms/number_list.rb', line 27 def initialize @numbers = ::Array.new end |
Instance Attribute Details
#numbers ⇒ Object (readonly)
Internal numbers array.
24 25 26 |
# File 'lib/mote_sms/number_list.rb', line 24 def numbers @numbers end |
Instance Method Details
#<<(number) ⇒ Object
Public: Add number to internal list, use duck typing to detect if it appears to be a number instance or not. So everything which does not respond to ‘to_number` is converted into a Number instance.
number - The Number or String to add.
Returns nothing.
50 51 52 |
# File 'lib/mote_sms/number_list.rb', line 50 def <<(number) self.push(number) end |
#each(&block) ⇒ Object
Public: Yields each Number instance from this number list to the provided block. This interface is also required to be implemeneted for Enumerable support.
Returns self.
72 73 74 75 |
# File 'lib/mote_sms/number_list.rb', line 72 def each(&block) numbers.each(&block) self end |
#empty? ⇒ Boolean Also known as: blank?
Public: Conform to arrayish behavior.
38 39 40 |
# File 'lib/mote_sms/number_list.rb', line 38 def empty? numbers.empty? end |
#length ⇒ Object Also known as: size
Public: Count of numbers in the list.
32 33 34 |
# File 'lib/mote_sms/number_list.rb', line 32 def length numbers.length end |
#normalized_numbers ⇒ Object
Public: Fetch numbers using to_number.
Returns Array of E164 normalized numbers.
80 81 82 |
# File 'lib/mote_sms/number_list.rb', line 80 def normalized_numbers numbers.map(&:to_number) end |
#push(*numbers) ⇒ Object
Public: Add multiple numbers, with optional options hash which can be used to set country options etc.
Returns self.
58 59 60 61 62 63 64 65 |
# File 'lib/mote_sms/number_list.rb', line 58 def push(*numbers) = numbers.last.is_a?(Hash) ? numbers.pop : {} numbers.flatten.each do |number| number = Number.new(number, ) unless number.respond_to?(:to_number) self.numbers << number end self end |