lib/mayl/commands/cd.rb in mayl-0.1.0 vs lib/mayl/commands/cd.rb in mayl-0.2.0
- old
+ new
@@ -12,10 +12,11 @@
#
# env - the global environment
# path - the path to cd in
def initialize(env, path)
@env = env
+ path = path.split('.').reject(&:empty?).compact.join('.') if path =~ /\w/
@path = path
end
# Public: Adds the path to the namespace.
#
@@ -27,16 +28,31 @@
ns.pop
@env.namespace = ns.join('.')
when "."
@env.namespace = ""
else
+ check_namespace!
+
if @env.namespace.empty?
@env.namespace = @path
else
@env.namespace += '.' << @path
end
end
nil
+ end
+
+ # Public: Checks that you're not trying to enter a leaf.
+ #
+ # Raises an ArgumentError if you are.
+ def check_namespace!
+ namespace = [@env.namespace, @path].reject(&:empty?).join('.')
+ matches = @env.peek(namespace).compact
+
+ if matches.empty?
+ key = @path.split('.').last
+ raise ArgumentError, "Can't cd to #{key} -- it's a leaf"
+ end
end
end
end
end