Module: Linecook::Utils
- Included in:
- Commands::Helper
- Defined in:
- lib/linecook/utils.rb
Class Method Summary collapse
- .arrayify(obj) ⇒ Object
- .camelize(str) ⇒ Object
- .constantize(const_name) ⇒ Object
- .deep_merge(a, b) ⇒ Object
- .deep_merge?(previous, current) ⇒ Boolean
- .hashify(obj) ⇒ Object
- .load_config(path) ⇒ Object
- .underscore(str) ⇒ Object
Class Method Details
.arrayify(obj) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/linecook/utils.rb', line 11 def arrayify(obj) case obj when nil then [] when String then obj.split(':') else obj end end |
.camelize(str) ⇒ Object
48 49 50 |
# File 'lib/linecook/utils.rb', line 48 def camelize(str) str.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } end |
.constantize(const_name) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/linecook/utils.rb', line 60 def constantize(const_name) constants = camelize(const_name).split(/::/) const = Object while name = constants.shift const = const.const_get(name) end const end |
.deep_merge(a, b) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/linecook/utils.rb', line 35 def deep_merge(a, b) b.each_pair do |key, current| previous = a[key] a[key] = deep_merge?(previous, current) ? deep_merge(previous, current) : current end a end |
.deep_merge?(previous, current) ⇒ Boolean
44 45 46 |
# File 'lib/linecook/utils.rb', line 44 def deep_merge?(previous, current) current.kind_of?(Hash) && previous.kind_of?(Hash) end |
.hashify(obj) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/linecook/utils.rb', line 19 def hashify(obj) case obj when Hash then obj when nil then {} when Array hash = {} obj.each {|entry| hash[entry] = entry } hash when String hashify obj.split(':') else nil end end |
.load_config(path) ⇒ Object
7 8 9 |
# File 'lib/linecook/utils.rb', line 7 def load_config(path) (path && File.exists?(path) ? YAML.load_file(path) : nil) || {} end |
.underscore(str) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/linecook/utils.rb', line 52 def underscore(str) str.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |