Class: Hiera::Backend::Eyaml::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/backend/eyaml/utils.rb

Class Method Summary collapse

Class Method Details

.camelcase(string) ⇒ Object


10
11
12
13
# File 'lib/hiera/backend/eyaml/utils.rb', line 10

def self.camelcase string
  return string if string !~ /_/ && string =~ /[A-Z]+.*/
  string.split('_').map{|e| e.capitalize}.join
end

.convert_to_utf_8(string) ⇒ Object


59
60
61
62
63
64
65
66
67
# File 'lib/hiera/backend/eyaml/utils.rb', line 59

def self.convert_to_utf_8 string
  orig_encoding = string.encoding
  return string if orig_encoding == Encoding::UTF_8

  return string.dup.force_encoding(Encoding::UTF_8)
rescue EncodingError => detail
  warn "Unable to encode to \"Encoding::UTF_8\" using the original \"#{orig_encoding}\""
  return string
end

.find_all_subclasses_of(args) ⇒ Object


45
46
47
48
49
50
51
52
53
# File 'lib/hiera/backend/eyaml/utils.rb', line 45

def self.find_all_subclasses_of args
  parent_class = args[ :parent_class ]
  constants = parent_class.constants
  candidates = []
  constants.each do | candidate |
    candidates << candidate.to_s.split('::').last if parent_class.const_get(candidate).class.to_s == "Class"
  end
  candidates
end

.find_closest_class(args) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hiera/backend/eyaml/utils.rb', line 20

def self.find_closest_class args
  parent_class = args[ :parent_class ]
  class_name = args[ :class_name ]
  constants = parent_class.constants
  candidates = []
  constants.each do | candidate |
    candidates << candidate.to_s if candidate.to_s.downcase == class_name.downcase
  end
  if candidates.count > 0
    parent_class.const_get candidates.first
  else
    nil
  end
end

.hiera?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'lib/hiera/backend/eyaml/utils.rb', line 55

def self.hiera?
  "hiera".eql? Eyaml::Options[:source]
end

.require_dir(classdir) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'lib/hiera/backend/eyaml/utils.rb', line 35

def self.require_dir classdir
  num_class_hierarchy_levels = self.to_s.split("::").count - 1 
  root_folder = File.dirname(__FILE__) + "/" + Array.new(num_class_hierarchy_levels).fill("..").join("/")
  class_folder = root_folder + "/" + classdir
  Dir[File.expand_path("#{class_folder}/*.rb")].uniq.each do |file|
    LoggingHelper.trace "Requiring file: #{file}"
    require file
  end
end

.snakecase(string) ⇒ Object


15
16
17
18
# File 'lib/hiera/backend/eyaml/utils.rb', line 15

def self.snakecase string
  return string if string !~ /[A-Z]/
  string.split(/(?=[A-Z])/).collect {|x| x.downcase}.join("_")
end