Module: Alki::Support

Defined in:
lib/alki/support.rb,
lib/alki/support/version.rb

Constant Summary collapse

VERSION =
'0.7.1'

Class Method Summary collapse

Class Method Details

.classify(str) ⇒ Object



32
33
34
35
36
# File 'lib/alki/support.rb', line 32

def self.classify(str)
  str.split('/').map do |c|
    c.split('_').map{|n| n.capitalize }.join('')
  end.join('::')
end

.constantize(name, parent = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/alki/support.rb', line 38

def self.constantize(name,parent = nil)
  name.split('::').inject(parent || Object) do |obj,el|
    return nil unless obj.const_defined? el, false
    obj.const_get el, false
  end
end

.create_constant(name, value, parent = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/alki/support.rb', line 19

def self.create_constant(name, value, parent=nil)
  parent ||= Object
  *ans, ln = name.to_s.split('::')
  ans.each do |a|
    unless parent.const_defined? a
      parent.const_set a, Module.new
    end
    parent = parent.const_get a
  end

  parent.const_set ln, value
end

.find_root(path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/alki/support.rb', line 52

def self.find_root(path)
  old_dir = File.absolute_path(path)
  dir = File.dirname(old_dir)
  until dir == old_dir || yield(dir)
    old_dir = dir
    dir = File.dirname(old_dir)
  end
  if dir != old_dir
    dir
  end
end

.load(name_or_obj) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/alki/support.rb', line 8

def self.load(name_or_obj)
  if name_or_obj.is_a?(String)
    name = name_or_obj
    require name
    obj = constantize classify name
  else
    obj = name_or_obj
  end
  obj
end

.path_name(path, root = File.dirname(path)) ⇒ Object



45
46
47
48
49
50
# File 'lib/alki/support.rb', line 45

def self.path_name(path,root=File.dirname(path))
  root = File.join(root,'')
  if path.start_with?(root) && path.end_with?('.rb')
    path[root.size..-4]
  end
end