Module: DR::Utils
Instance Method Summary collapse
- #pretty_print(string, format: nil, pretty: nil, **kw) ⇒ 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, **kw) ⇒ 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 36 37 38 39 |
# File 'lib/dr/base/utils.rb', line 4 def pretty_print(string, format: nil, pretty: nil, **kw) case format.to_s when "json" require 'json' return pretty_print(string.to_json, pretty: pretty, **kw) when "yaml" require "yaml" return pretty_print(string.to_yaml, pretty: pretty, **kw) end pretty = "color" if pretty == nil or pretty == true #default case pretty.to_s when "ap", "awesome_print", "amazing_print" begin require 'amazing_print' ap(string, **kw) rescue LoadError,NameError pretty_print(string,pretty: :pp_color, **kw) end when "color", "pp_color" begin require 'pry' if kw[:multiline] == false #simulate no multiline Pry::ColorPrinter.pp string, $DEFAULT_OUTPUT, 9999 else Pry::ColorPrinter.pp string end 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
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dr/base/utils.rb', line 53 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
42 43 44 45 46 |
# File 'lib/dr/base/utils.rb', line 42 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
47 48 49 50 51 |
# File 'lib/dr/base/utils.rb', line 47 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 |