Module: Boson::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/boson/util.rb

Overview

Collection of utility methods used throughout Boson.

Instance Method Summary collapse

Instance Method Details

#any_const_get(name) ⇒ Object

Returns a constant like const_get() no matter what namespace it’s nested in. Returns nil if the constant is not found.



30
31
32
33
34
35
36
37
38
39
# File 'lib/boson/util.rb', line 30

def any_const_get(name)
  return name if name.is_a?(Module)
  klass = Object
  name.split('::').each {|e|
    klass = klass.const_get(e)
  }
  klass
rescue
   nil
end

#camelize(string) ⇒ Object

From ActiveSupport, does the reverse of underscore: ‘boson/method_inspector’ -> ‘Boson::MethodInspector’



17
18
19
20
# File 'lib/boson/util.rb', line 17

def camelize(string)
  string.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.
    gsub(/(?:^|_)(.)/) { $1.upcase }
end

#constantize(string) ⇒ Object

Converts a module/class string to the actual constant. Returns nil if not found.



24
25
26
# File 'lib/boson/util.rb', line 24

def constantize(string)
  any_const_get(camelize(string))
end

#create_module(base_module, name) ⇒ Object

Creates a module under a given base module and possible name. If the module already exists, it attempts to create one with a number appended to the name.



75
76
77
78
79
80
81
82
# File 'lib/boson/util.rb', line 75

def create_module(base_module, name)
  desired_class = camelize(name)
  possible_suffixes = [''] + %w{1 2 3 4 5 6 7 8 9 10}
  if suffix = possible_suffixes.find {|e|
    !base_module.const_defined?(desired_class+e) }
      base_module.const_set(desired_class+suffix, Module.new)
  end
end

#detect(options = {}, &block) ⇒ Object

Detects new object/kernel methods, gems and modules created within a block. Returns a hash of what’s detected. Valid options and possible returned keys are :methods, :object_methods, :modules, :gems.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/boson/util.rb', line 44

def detect(options={}, &block)
  options = {methods: true}.merge!(options)
  original_gems = defined?(Gem) ? Gem.loaded_specs.keys : []
  original_object_methods = Object.instance_methods
  original_instance_methods = Boson.main_object.singleton_class.instance_methods
  original_modules = modules if options[:modules]

  block.call

  detected = {}
  detected[:methods] = options[:methods] ?
    (Boson.main_object.singleton_class.instance_methods -
       original_instance_methods) : []
  unless options[:object_methods]
    detected[:methods] -= (Object.instance_methods - original_object_methods)
  end
  detected[:gems] = Gem.loaded_specs.keys - original_gems if defined? Gem
  detected[:modules] = modules - original_modules if options[:modules]
  detected
end

#format_table(arr_of_arr) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/boson/util.rb', line 105

def format_table(arr_of_arr)
  name_max = arr_of_arr.map {|arr| arr[0].length }.max
  desc_max = arr_of_arr.map {|arr| arr[1].length }.max

  arr_of_arr.map do |name, desc|
    ("  %-*s  %-*s" % [name_max, name, desc_max, desc]).rstrip
  end
end

#modulesObject

Returns all modules that currently exist.



66
67
68
69
70
# File 'lib/boson/util.rb', line 66

def modules
  all_modules = []
  ObjectSpace.each_object(Module) {|e| all_modules << e}
  all_modules
end

#recursive_hash_merge(hash1, hash2) ⇒ Object

Recursively merge hash1 with hash2.



85
86
87
# File 'lib/boson/util.rb', line 85

def recursive_hash_merge(hash1, hash2)
  hash1.merge(hash2) {|k,o,n| (o.is_a?(Hash)) ? recursive_hash_merge(o,n) : n}
end

#underscore(camel_cased_word) ⇒ Object

From ActiveSupport, converts a camelcased string to an underscored string: ‘Boson::MethodInspector’ -> ‘boson/method_inspector’



7
8
9
10
11
12
13
# File 'lib/boson/util.rb', line 7

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
   gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
   gsub(/([a-z\d])([A-Z])/,'\1_\2').
   tr("-", "_").
   downcase
end

#underscore_search(input, list, first_match = false) ⇒ Object

Regular expression search of a list with underscore anchoring of words. For example ‘some_dang_long_word’ can be specified as ‘s_d_l_w’.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/boson/util.rb', line 91

def underscore_search(input, list, first_match=false)
  meth = first_match ? :find : :select
  return (first_match ? input : [input]) if list.include?(input)
  input = input.to_s
  if input.include?("_")
    underscore_regex = input.split('_').map {|e|
      Regexp.escape(e) }.join("([^_]+)?_")
    list.send(meth) {|e| e.to_s =~ /^#{underscore_regex}/ }
  else
    escaped_input = Regexp.escape(input)
    list.send(meth) {|e| e.to_s =~ /^#{escaped_input}/ }
  end
end