Class: RVC::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/rvc/namespace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, shell, parent) ⇒ Namespace

Returns a new instance of Namespace.



32
33
34
35
36
37
38
39
40
# File 'lib/rvc/namespace.rb', line 32

def initialize name, shell, parent
  @name = name
  @shell = shell
  @parent = parent
  @slate = CommandSlate.new self
  @namespaces = {}
  @commands = {}
  @aliases = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/rvc/namespace.rb', line 80

def method_missing sym, *args
  if cmd = @commands[sym]
    cmd.invoke *args
  elsif args.empty? and x = self[sym]
    x
  else
    super
  end
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



26
27
28
# File 'lib/rvc/namespace.rb', line 26

def aliases
  @aliases
end

#commandsObject (readonly)

Returns the value of attribute commands.



26
27
28
# File 'lib/rvc/namespace.rb', line 26

def commands
  @commands
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/rvc/namespace.rb', line 26

def name
  @name
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



26
27
28
# File 'lib/rvc/namespace.rb', line 26

def namespaces
  @namespaces
end

#parentObject (readonly)

Returns the value of attribute parent.



26
27
28
# File 'lib/rvc/namespace.rb', line 26

def parent
  @parent
end

#shellObject (readonly)

Returns the value of attribute shell.



26
27
28
# File 'lib/rvc/namespace.rb', line 26

def shell
  @shell
end

#slateObject (readonly)

Returns the value of attribute slate.



26
27
28
# File 'lib/rvc/namespace.rb', line 26

def slate
  @slate
end

Instance Method Details

#[](sym) ⇒ Object



76
77
78
# File 'lib/rvc/namespace.rb', line 76

def [] sym
  @namespaces[sym]
end

#child_namespace(name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rvc/namespace.rb', line 46

def child_namespace name
  if ns = namespaces[name]
    return ns
  else
    namespaces[name] = Namespace.new(name, shell, self)
  end
end

#inspectObject



28
29
30
# File 'lib/rvc/namespace.rb', line 28

def inspect
  "#<RVC::Namespace:#{name}>"
end

#load_code(code, filename) ⇒ Object



42
43
44
# File 'lib/rvc/namespace.rb', line 42

def load_code code, filename
  @slate.instance_eval code, filename
end

#load_module_dir(dir, verbose) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rvc/namespace.rb', line 90

def load_module_dir dir, verbose
  puts "loading module directory #{dir}" if verbose
  Dir.foreach(dir) do |f|
    path = File.join(dir, f)
    if f[0..0] == '.'
      next
    elsif File.directory? path
      ns_name = f.to_sym
      child_namespace(ns_name).load_module_dir(path, verbose)
    elsif f =~ /\.rb$/
      ns_name = f[0...-3].to_sym
      puts "loading #{ns_name} from #{path}" if verbose
      begin
        code = File.read path
        child_namespace(ns_name).load_code(code, path)
      rescue
        puts "#{$!.class} while loading #{f}: #{$!.message}"
        $!.backtrace.each { |x| puts x }
      end
    end
  end
end

#lookup(cmdpath, accept = Command) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rvc/namespace.rb', line 54

def lookup cmdpath, accept=Command
  if cmdpath.empty?
    if accept == Command
      return nil
    elsif accept == Namespace
      return self
    end
  elsif cmdpath.length == 1 and accept == Command
    sym = cmdpath[0]
    if @aliases.member? sym
      @shell.cmds.lookup @aliases[sym], accept
    else
      @commands[sym]
    end
  else
    sym = cmdpath[0]
    child = @namespaces[sym]
    return nil if child == nil
    child.lookup cmdpath[1..-1], accept
  end
end