Module: DR::Utils
Instance Method Summary collapse
- #pretty_print(string, format: nil, pretty: nil) ⇒ Object
- #rsplit(s, sep, num = nil) ⇒ Object
-
#to_camel_case(s) ⇒ Object
stolen from active support.
- #to_snake_case(s) ⇒ Object
Instance Method Details
#pretty_print(string, format: nil, pretty: nil) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dr/base/utils.rb', line 4 def pretty_print(string, format: nil, pretty: nil) 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 pretty = "color" if pretty == nil or pretty == true #default case pretty.to_s when "ap" begin require 'ap' ap string rescue LoadError,NameError pretty_print(string,pretty: :pp) end when "color", "pp_color" begin require 'pry' Pry::ColorPrinter.pp string rescue LoadError,NameError pretty_print(string,pretty: :pp) end when "pp" require 'pp' pp string else puts string end end |
#rsplit(s, sep, num = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dr/base/utils.rb', line 49 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
38 39 40 41 42 |
# File 'lib/dr/base/utils.rb', line 38 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
43 44 45 46 47 |
# File 'lib/dr/base/utils.rb', line 43 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 |