Class: RVC::Namespace
- Inherits:
-
Object
- Object
- RVC::Namespace
- Defined in:
- lib/rvc/namespace.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespaces ⇒ Object
readonly
Returns the value of attribute namespaces.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
-
#slate ⇒ Object
readonly
Returns the value of attribute slate.
Instance Method Summary collapse
- #[](sym) ⇒ Object
- #child_namespace(name) ⇒ Object
-
#initialize(name, shell, parent) ⇒ Namespace
constructor
A new instance of Namespace.
- #inspect ⇒ Object
- #load_code(code, filename) ⇒ Object
- #load_module_dir(dir, verbose) ⇒ Object
- #lookup(cmdpath, accept = Command) ⇒ Object
- #method_missing(sym, *args) ⇒ Object
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
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
26 27 28 |
# File 'lib/rvc/namespace.rb', line 26 def aliases @aliases end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
26 27 28 |
# File 'lib/rvc/namespace.rb', line 26 def commands @commands end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/rvc/namespace.rb', line 26 def name @name end |
#namespaces ⇒ Object (readonly)
Returns the value of attribute namespaces.
26 27 28 |
# File 'lib/rvc/namespace.rb', line 26 def namespaces @namespaces end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
26 27 28 |
# File 'lib/rvc/namespace.rb', line 26 def parent @parent end |
#shell ⇒ Object (readonly)
Returns the value of attribute shell.
26 27 28 |
# File 'lib/rvc/namespace.rb', line 26 def shell @shell end |
#slate ⇒ Object (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 |
#inspect ⇒ Object
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}: #{$!.}" $!.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 |