Module: DR::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/dr/base/utils.rb

Instance Method Summary collapse

Instance Method Details

#pretty_print(string, format: nil, pretty: false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dr/base/utils.rb', line 4

def pretty_print(string, format: nil, pretty: false)
	case format.to_s
	when "json"
		require 'json'
		return pretty_print(string.to_json, pretty: pretty)
	when "yaml"
		require "yaml"
		return pretty_print(string.to_yaml, pretty: pretty)
	end
	if pretty.to_s=="color"
		begin
			require 'ap'
			ap string
		rescue LoadError,NameError
			pretty_print(string,pretty:true)
		end
	elsif pretty
		require 'pp'
		pp string
	else
		puts string
	end
end

#rsplit(s, sep, num = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dr/base/utils.rb', line 40

def rsplit(s, sep, num=nil)
	if num.nil? or num==0
		s.split(sep)
	else
		components=s.split(sep)
		components+=[nil]*[(num-components.length), 0].max
		a=components[0..(components.length-num)]
		b=components[(components.length-num+1)..(components.length-1)]
		return [a.join(sep), *b]
	end
end

#to_camel_case(s) ⇒ Object

stolen from active support



29
30
31
32
33
# File 'lib/dr/base/utils.rb', line 29

def to_camel_case(s)
	s.sub(/^[a-z\d]*/) { |match| match.capitalize }.
		gsub(/(?:_|(\/))([a-z\d]*)/i) {"#{$1}#{$2.capitalize}"}.
		gsub("/", "::")
end

#to_snake_case(s) ⇒ Object



34
35
36
37
38
# File 'lib/dr/base/utils.rb', line 34

def to_snake_case(s)
	# convert from caml case to snake_case
	s.gsub(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2').
		gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
end