Sha256: b706f1b3b2773122500e8bf6407917d981135b93cb13637deb4779c8582c7dba

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 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
    return gsub(/\s/, '_').squeeze('_').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
  unless value.nil?
    case variable_type.downcase
      when 'double'
        out_value = value.to_f
      when 'integer'
        out_value = value.to_i
      when 'string', 'choice'
        out_value = inspect_string ? value.inspect : value.to_s
      when 'bool', 'boolean'
        # Check if the value is already a boolean
        if !!value == value
          out_value = value
        else
          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
        end
      else
        fail "Unknown variable type of '#{@variable['type']}'"
    end
  end

  out_value
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
openstudio-analysis-1.0.0.rc6 lib/openstudio/helpers/string.rb
openstudio-analysis-1.0.0.pre.rc5 lib/openstudio/helpers/string.rb
openstudio-analysis-1.0.0.pre.rc4 lib/openstudio/helpers/string.rb
openstudio-analysis-1.0.0.pre.rc3 lib/openstudio/helpers/string.rb
openstudio-analysis-1.0.0.pre.rc2 lib/openstudio/helpers/string.rb