Sha256: e57f78e35dd05f90feda97d3db060f047539bccdd64674b4fda14e782cf0dd36

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

# add the underscore from rails for snake_casing strings

class String
  def underscore
    gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
  end

  def snake_case
    gsub(' ', '_').downcase
  end

  def to_bool
    return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
    return false if self == false || self =~ (/(false|f|no|n|0)$/i)
    fail "invalid value for Boolean: '#{self}'"
  end
end

def typecast_value(variable_type, value, inspect_string = false)
  out_value = nil
  case variable_type.downcase
    when 'double'
      out_value = value.to_f
    when 'integer'
      out_value = value.to_i
    when 'string'
      out_value = inspect_string ? value.inspect : value.to_s
    when 'choice'
      out_value = value.inspect
    when 'bool', 'boolean'
      if value.downcase == 'true'
        out_value = true
      elsif value.downcase == 'false'
        out_value = false
      else
        fail "Can't cast to a bool from a value of '#{value}' of class '#{value.class}'"
      end
    else
      fail "Unknown variable type of '#{@variable['type']}'"
  end

  out_value
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
openstudio-analysis-0.3.4 lib/openstudio/helpers/string.rb
openstudio-analysis-0.3.3 lib/openstudio/helpers/string.rb
openstudio-analysis-0.3.2 lib/openstudio/helpers/string.rb