Module: EasyType::Helpers

Included in:
Generators::Base, SourceDir, Template, Entitlement::NumberedEntry
Defined in:
lib/easy_type/helpers.rb

Overview

Contains a set of helpe methods and classed that can be used throughtout EasyType

Defined Under Namespace

Classes: InstancesResults

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(parent) ⇒ Object



18
19
20
# File 'lib/easy_type/helpers.rb', line 18

def self.included(parent)
  parent.extend(Helpers)
end

Instance Method Details

#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object

Camelize a string. This code is “borrowed” from RAILS. Credits and copyrights to them.



81
82
83
84
85
86
87
88
# File 'lib/easy_type/helpers.rb', line 81

def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    lower_case_and_underscored_word.to_s.gsub(%r{\/(.?)}) { "::#{Regexp.last_match(1).upcase}" }.\
      gsub(/(?:^|_)(.)/) { Regexp.last_match(1).upcase }
  else
    lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
  end
end

#convert_csv_data_to_hash(csv_data, headers = [], options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/easy_type/helpers.rb', line 49

def convert_csv_data_to_hash(csv_data, headers = [], options = {})
  options = check_options(options)
  default_options = {
    :header_converters => lambda { |f| f ? f.strip : nil }
  }
  default_options[:headers] = if headers != []
                                headers
                              else
                                true
                              end
  options = default_options.merge(options)
  skip_lines = options.delete(:skip_lines) { HEADER_LINE_REGEX }
  #
  # This code replace all quote character to a string. After the
  # CSV parser, these values are replaced back again. This code is required
  # to allow for quotes in the data
  #
  csv_data.gsub!('"""', '|&quote|') # Translate quotes
  data = []
  ::CSV.parse(csv_data, **options) do |row|
    unless row_contains_skip_line(row, skip_lines)
      row_data = row.to_a.collect { |r| [r[0], r[1].nil? ? nil : r[1].gsub('|&quote|', '"')] }
      data << InstancesResults[row_data]
    end
  end
  data
end

#get_puppet_file(url) ⇒ Any

This allows you to use a puppet syntax for a file and return it’s content.

Examples:

get_puppet_file 'puppet:///modules/my_module_name/create_tablespace.sql.erb'

Parameters:

  • url (String)

    this is the name of the template to be used.

Returns:

  • (Any)

    Content of the file

Raises:

  • (ArgumentError)

    when the file doesn’t exist



102
103
104
105
106
107
108
109
# File 'lib/easy_type/helpers.rb', line 102

def get_puppet_file(url)
  # Somehow there is no consistent way to determine what terminus to user. So we switch to a
  # trial and error method. First we start withe the default. And if it doesn't work, we try the
  # other ones
  status = load_file_with_any_terminus(url)
  raise ArgumentError, "Previous error(s) resulted in Puppet being unable to retrieve information from environment #{Puppet['environment']} source(s) #{url}'\nMost probable cause is file not found." unless status
  status.content
end