Module: Arsenicum::Util

Extended by:
Util
Included in:
Configuration::InstanceConfiguration, Formatter, Task::ClassDispatcher, Util
Defined in:
lib/arsenicum/util.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
# File 'lib/arsenicum/util.rb', line 3

def self.included(base)
  mod = self
  base.module_eval{extend mod}
end

Instance Method Details

#camelcase(stringlike, upcase_first = true) ⇒ Object



16
17
18
19
20
21
# File 'lib/arsenicum/util.rb', line 16

def camelcase(stringlike, upcase_first = true)
  stringlike.to_s.dup.tap do |s|
    s.gsub!(/_([a-z])/){$1.upcase}
    s.gsub!(/^([a-z])/){$1.upcase} if upcase_first
  end.to_sym
end

#classify(stringlike) ⇒ Object



23
24
25
26
27
# File 'lib/arsenicum/util.rb', line 23

def classify(stringlike)
  stringlike.to_s.split(/\/+/).map do |s|
    camelcase(s)
  end.join('::')
end

#constantize(klass, inside: Kernel) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/arsenicum/util.rb', line 36

def constantize(klass, inside: Kernel)
  if klass.to_s.start_with?('::')
    klass = klass.to_s[2..-1].to_sym
    inside = Kernel
  end
  klass.to_s.split('::').inject(inside) do |parent, const|
    parent.const_get const.to_sym
  end
end

#normalize_hash(values) ⇒ Object



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

def normalize_hash(values)
  values.inject({}) do |h, kv|
    (key, value) = kv
    value = normalize_hash(value) if value.is_a? Hash
    h.tap{|i|i.merge!(key.to_sym => value)}
  end
end

#underscore(stringlike) ⇒ Object



29
30
31
32
33
34
# File 'lib/arsenicum/util.rb', line 29

def underscore(stringlike)
  stringlike.to_s.dup.tap do |s|
    s.gsub!(/^([A-Z])/){$1.tap(&:downcase!)}
    s.gsub!(/([A-Z])/){'_' << $1.tap(&:downcase!)}
  end
end