Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/bcl/core_ext.rb

Overview

******************************************************************************* OpenStudio®, Copyright © Alliance for Sustainable Energy, LLC. See also openstudio.net/license *******************************************************************************

Instance Method Summary collapse

Instance Method Details

#titleizeObject

simple method to create titles – very custom to catch known inflections



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bcl/core_ext.rb', line 27

def titleize
  arr = ['a', 'an', 'the', 'by', 'to']
  upcase_arr = ['DX', 'EDA', 'AEDG', 'LPD', 'COP']
  r = tr('_', ' ').gsub(/\w+/) do |match|
    match_result = match
    if upcase_arr.include?(match.upcase)
      match_result = upcase_arr[upcase_arr.index(match.upcase)]
    elsif arr.include?(match)
      match_result = match
    else
      match_result = match.capitalize
    end
    match_result
  end

  r
end

#to_boolObject



19
20
21
22
23
24
# File 'lib/bcl/core_ext.rb', line 19

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

  raise "invalid value for Boolean: '#{self}'"
end

#to_underscoreObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bcl/core_ext.rb', line 7

def to_underscore
  gsub('OpenStudio', 'Openstudio')
    .gsub('EnergyPlus', 'Energyplus')
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .tr(' ', '_')
    .squeeze('_')
    .downcase
end