Module: TT::Locale

Defined in:
TT_Lib2/locale.rb

Overview

Collection of Group, ComponentInstnace and Image methods.

Since:

  • 2.4.0

Class Method Summary collapse

Class Method Details

.cast_string(string, type, array_split = ',') ⇒ Mixed

Returns:

  • (Mixed)

Since:

  • 2.4.0



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'TT_Lib2/locale.rb', line 76

def self.cast_string(string, type, array_split = ',')
  type = type.new if type.class == Class
  if string == 'null'
    value = nil
  elsif type.is_a?( Integer )
    value = string.to_i 
  elsif type.is_a?( Float ) && !type.is_a?( Length )
    value = self.string_to_float(string)
  elsif type.is_a?( Length )
    value = string.to_l
  elsif type.is_a?( TrueClass ) || type.is_a?( FalseClass )
    value = ( string.downcase == 'true' ) ? true : false
  elsif type.is_a?( Array )
    value = string.split( array_split )
  else
    value = string
  end
  value
end

.decimal_separatorString

Returns:

  • (String)

Since:

  • 2.4.0



104
105
106
107
108
109
110
# File 'TT_Lib2/locale.rb', line 104

def self.decimal_separator
  # If this raises an error the decimal separator is not '.'
  '1.2'.to_l
  return '.'
rescue
  return ','
end

.float_to_string(float, precision = nil) ⇒ String

Parameters:

  • float (Float)
  • precision (Integer) (defaults to: nil)

    (Added: 2.7.0)

Returns:

  • (String)

Since:

  • 2.4.0



38
39
40
41
42
43
44
45
# File 'TT_Lib2/locale.rb', line 38

def self.float_to_string( float, precision = nil )
  @decimal_separator ||= self.decimal_separator
  if precision
    sprintf( "%.#{precision}f", float ).tr!( '.', @decimal_separator )
  else
    float.to_s.tr!( '.', @decimal_separator )
  end
end

.format_float(float, model) ⇒ String

Parameters:

  • float (Float)
  • model (Sketchup::Model)

Returns:

  • (String)

Since:

  • 2.4.0



56
57
58
59
60
61
62
63
64
65
# File 'TT_Lib2/locale.rb', line 56

def self.format_float(float, model)
  @decimal_separator ||= self.decimal_separator
  precision = model.options['UnitsOptions']['LengthPrecision']
  num = sprintf("%.#{precision}f", float)
  if num.to_f != float
    num = "~ #{num}"
  end
  num.tr!('.', @decimal_separator)
  num
end

.list_separatorObject

Since:

  • 2.6.0



118
119
120
# File 'TT_Lib2/locale.rb', line 118

def self.list_separator
  ( self.decimal_separator == '.' ) ? ',' : ';'
end

.string_to_float(string) ⇒ Float

Parameters:

  • string (String)

Returns:

  • (Float)

Since:

  • 2.4.0



22
23
24
25
26
27
# File 'TT_Lib2/locale.rb', line 22

def self.string_to_float(string)
  # Dirty hack to get the current locale's decimal separator, which is then
  # replaced in the string to a period which is what the .to_f method expects.
  @decimal_separator ||= self.decimal_separator
  string.tr(@decimal_separator, '.').to_f
end