Module: Ing::Util

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

Instance Method Summary collapse

Instance Method Details

#decode_class(str, base = ::Object) ⇒ Object



5
6
7
# File 'lib/ing/util.rb', line 5

def decode_class(str, base=::Object)
  namespaced_const_get( decode_class_names(str), base )
end

#decode_class_names(str) ⇒ Object



9
10
11
# File 'lib/ing/util.rb', line 9

def decode_class_names(str)
  str.split(':').map {|c| c.gsub(/(?:\A|_+)(\w)/) {$1.upcase} }
end

#encode_class(klass) ⇒ Object



13
14
15
# File 'lib/ing/util.rb', line 13

def encode_class(klass)
  encode_class_names(klass.to_s.split('::'))
end

#encode_class_names(list) ⇒ Object



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

def encode_class_names(list)
  list.map {|c| c.to_s.gsub(/([A-Z])/) {
      ($`.empty? ? "" : "_") + $1.downcase
    } 
  }.join(':')
end

#escape_globs(path) ⇒ Object

Returns a string that has had any glob characters escaped. The glob characters are ‘* ? { } [ ]`.

Examples

Util.escape_globs('[apps]')   # => '\[apps\]'

Parameters

String

Returns

String



70
71
72
# File 'lib/ing/util.rb', line 70

def escape_globs(path)
  path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
end

#ing_commands(base, recurse = false, init = {}) ⇒ Object

search for callables under base note this does not pick up aliased constants right now



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

def ing_commands(base, recurse=false, init={})
  base.constants(false).each do |c|
    v = base.const_get(c)
    next if init.values.include?(v)
    if v.respond_to?(:constants)
      init[ encode_class(v) ] = v
      ing_commands(v,true,init) if recurse
    elsif v.respond_to?(:call)
      init[ encode_class_names(base.to_s.split('::') + [c]) ] = v
    end
  end
  init
end

#namespaced_const_get(list, base = ::Object) ⇒ Object



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

def namespaced_const_get(list, base=::Object)
  list.inject(base) {|m, klass| m.const_get(klass, false)}
end

#option?(arg) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ing/util.rb', line 44

def option?(arg)
  !!(/^-{1,2}/ =~ arg)
end

#split_method_args(args) ⇒ Object

not used



49
50
51
52
53
54
55
# File 'lib/ing/util.rb', line 49

def split_method_args(args)
  if option?(args.first)
    [nil, args]
  else
    [args.first, args[1..-1]]
  end
end