Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/user_friendly_id/string.rb
Instance Method Summary collapse
-
#from_base34 ⇒ Object
Turn a base34 string representation into a base10 integer.
- #is_negative? ⇒ Boolean
- #valid_base34? ⇒ Boolean
Instance Method Details
#from_base34 ⇒ Object
Turn a base34 string representation into a base10 integer.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/user_friendly_id/string.rb', line 6 def from_base34 raise ArgumentError.new("Invalid characters for a base34 number found") unless self.valid_base34? # strip whitespace, negative sign, and upcase kleen = self.kleened.upcase total = 0 size = kleen.length for i in 0..size-1 total += UserFriendlyId::BASE34_DIGITS.index(kleen[i,1]) * 34**(size-(i+1)) end total *= -1 if is_negative? return total end |
#is_negative? ⇒ Boolean
24 25 26 |
# File 'lib/user_friendly_id/string.rb', line 24 def is_negative? self.chr == '-' end |
#valid_base34? ⇒ Boolean
19 20 21 22 |
# File 'lib/user_friendly_id/string.rb', line 19 def valid_base34? # only the alpha minus I & O is what we want... not /[^0-9a-zA-Z&&[^ioIO]]/.match(self.kleened) end |