Module: Orchestra::Util

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

Instance Method Summary collapse

Instance Method Details

#deconstantize(str) ⇒ Object



64
65
66
# File 'lib/orchestra/util.rb', line 64

def deconstantize str
  split_namespaces(str).first
end

#demodulize(str) ⇒ Object



60
61
62
# File 'lib/orchestra/util.rb', line 60

def demodulize str
  split_namespaces(str).last
end

#extract_hash(ary) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/orchestra/util.rb', line 17

def extract_hash ary
  if ary.last.is_a? Hash
    hsh = ary.pop
  else
    hsh = {}
  end
  [hsh, ary]
end

#extract_key_args(hsh, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/orchestra/util.rb', line 5

def extract_key_args hsh, *args
  defaults, args = extract_hash args
  unknown_args = hsh.keys - (args + defaults.keys)
  missing_args = args - hsh.keys
  unless unknown_args.empty? and missing_args.empty?
    raise ArgumentError, key_arg_error(unknown_args, missing_args)
  end
  (args + defaults.keys).map do |arg|
    hsh.fetch arg do defaults.fetch arg end
  end
end

#to_camel_case(str) ⇒ Object



34
35
36
37
38
39
# File 'lib/orchestra/util.rb', line 34

def to_camel_case str
  str = "_#{str}"
  str.gsub!(%r{_[a-z]}) { |snake| snake.slice(1).upcase }
  str.gsub!('/', '::')
  str
end

#to_lazy_thunk(obj) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/orchestra/util.rb', line 26

def to_lazy_thunk obj
  if obj.respond_to? :to_proc and not obj.is_a? Symbol
    obj
  else
    Proc.new do obj end
  end
end

#to_snake_case(str) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/orchestra/util.rb', line 41

def to_snake_case str
  str = str.gsub '::', '/'
  # Convert FOOBar => FooBar
  str.gsub! %r{[[:upper:]]{2,}} do |uppercase|
    bit = uppercase[0]
    bit << uppercase[1...-1].downcase
    bit << uppercase[-1]
    bit
  end
  # Convert FooBar => foo_bar
  str.gsub! %r{[[:lower:]][[:upper:]]+[[:lower:]]} do |camel|
    bit = camel[0]
    bit << '_'
    bit << camel[1..-1].downcase
  end
  str.downcase!
  str
end