Class: Adhearsion::VoIP::DSL::NumericalString

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/voip/dsl/numerical_string.rb

Overview

In Ruby, a number with a leading zero such as 023.45 or 023 does not evaluate as expected. In the case of the float 023.45, you get a syntax error. In the case of 023 the number is parsed as being in octal form and the number 19 is returned.

In Adhearsion, various strings that are entirely numeric might start with zero and that zero should be preserved when converting that string of numbers into a number. The numerical string retains the zero while still allowing it to be compared to other numbers.

[[I think this leading zero thing is only part of the reason that NumericalString exists. I’m currently writing tests for this leading zero stuff so I thought I’d dump some of my assumptions about it here in the documentation.]]

Direct Known Subclasses

PhoneNumber

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ NumericalString

Returns a new instance of NumericalString.



28
29
30
31
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 28

def initialize(str)
  @__real_string = str.to_s
  @__real_num = str.to_i if @__real_string =~ /^\d+$/
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



33
34
35
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 33

def method_missing(name, *args, &block)
  @__real_string.__send__ name, *args, &block
end

Instance Attribute Details

#__real_numObject (readonly)

Returns the value of attribute __real_num.



26
27
28
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 26

def __real_num
  @__real_num
end

#__real_stringObject (readonly)

Returns the value of attribute __real_string.



26
27
28
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 26

def __real_string
  @__real_string
end

Class Method Details

.starts_with_leading_zero?(string) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 17

def starts_with_leading_zero?(string)
  string.to_s[/^0\d+/]
end

Instance Method Details

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 37

def respond_to?(m)
  @__real_string.respond_to?(m) || m == :__real_num || m == :__real_string
end