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.



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

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



31
32
33
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 31

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.



24
25
26
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 24

def __real_num
  @__real_num
end

#__real_stringObject (readonly)

Returns the value of attribute __real_string.



24
25
26
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 24

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

#==(x) ⇒ Object Also known as: ===



39
40
41
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 39

def ==(x)
  return x.is_a?(Fixnum) ? x == @__real_num : x == @__real_string
end

#is_a?(obj) ⇒ Boolean Also known as: kind_of?

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 44

def is_a?(obj)
  case obj.to_s
  when "Fixnum" then true
  when "String" then true
  end
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 35

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