Class: Mayl::Commands::Cd

Inherits:
Object
  • Object
show all
Defined in:
lib/mayl/commands/cd.rb

Overview

Public: The Cd command navigates through YAML namespaces.

Example

command = Cd.new(env, 'models.bla')
command.execute

Instance Method Summary collapse

Constructor Details

#initialize(env, path) ⇒ Cd

Public: Initializes a new Cd command.

env - the global environment path - the path to cd in



15
16
17
18
19
# File 'lib/mayl/commands/cd.rb', line 15

def initialize(env, path)
  @env  = env
  path = path.split('.').reject(&:empty?).compact.join('.') if path =~ /\w/
  @path = path
end

Instance Method Details

#executeObject

Public: Adds the path to the namespace.

Returns nil.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mayl/commands/cd.rb', line 24

def execute
  namespace = @env.namespace

  case @path
  when ".."
    ns = namespace.split('.')
    ns.pop
    @env.namespace = ns.join('.')
  when "."
    @env.namespace = ""
  else
    check_namespace!

    if namespace.empty?
      @env.namespace = @path
    else
      @env.namespace += '.' << @path
    end
  end
  nil
end