Class: TreeRepl::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/treerepl/repl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_class) ⇒ Repl

Returns a new instance of Repl.



13
14
15
16
17
# File 'lib/treerepl/repl.rb', line 13

def initialize(root_class)
  @root_class = root_class
  @finder = nil
  @commands = [Ls,Cd,Pwd,Use,Help,Quit,Cat].map {|cls| cls.new self}
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



6
7
8
# File 'lib/treerepl/repl.rb', line 6

def commands
  @commands
end

#curObject

Returns the value of attribute cur.



7
8
9
# File 'lib/treerepl/repl.rb', line 7

def cur
  @cur
end

#finderObject

String -> TreeNode



11
12
13
# File 'lib/treerepl/repl.rb', line 11

def finder
  @finder
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/treerepl/repl.rb', line 5

def root
  @root
end

#root_classObject

Returns the value of attribute root_class.



8
9
10
# File 'lib/treerepl/repl.rb', line 8

def root_class
  @root_class
end

Instance Method Details

#add_command(command_class) ⇒ Object



19
20
21
# File 'lib/treerepl/repl.rb', line 19

def add_command(command_class)
  @commands << command_class.new(self)
end

#cwdObject

void -> string



113
114
115
# File 'lib/treerepl/repl.rb', line 113

def cwd
  path(root,cur).map {|n| n.name}.join('/')
end

#eval_path(node, path) ⇒ Object

TreeNode string -> TreeNode



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/treerepl/repl.rb', line 78

def eval_path(node,path)
  path = '.' if not path or path == ''
  if path =~ /^\//
    args = ['/'] + path.gsub(/^\/+/,'').split(/\//)
  else
    args = path.split /\//
  end
  trav = node
  args.each do |arg|
    case arg
    when '.'
      trav = trav
    when '..'
      trav = trav.parent
    when '/'
      trav = root
    else
      found = false
      trav.children.each do |n|
        if n.name == arg
          trav = n
          found = true
          break
        end
      end
      if not found
        STDERR.puts 'Invalid path: ' + path
        return nil
      end
    end
  end
  return trav
end

#loop(strings2cmds) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/treerepl/repl.rb', line 34

def loop(strings2cmds)
  print cwd + '> '
  orig_line = STDIN.readline.strip
  line = orig_line.split(/ /)
  return false if line.empty?
  str = line.shift.downcase
  return true if str == 'quit'
  if not cur
    if str != 'use' and str != 'help'
      STDERR.puts 'You must set a user with \'use\' before anything else.'
      return
    end
  end
  args = line
  cmd = strings2cmds[str]
  if cmd
    begin
      cmd.exec args
    rescue Exception => e
      STDERR.puts 'Trouble executing: ' + orig_line
      raise e
    end
  else
    STDERR.puts 'Unknown command: ' + str
  end
  return false
end

#main(root) ⇒ Object

TreeNode -> Void



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/treerepl/repl.rb', line 28

def main(root)
  use root
  strings2cmds = {}
  commands.each do |cmd|
    strings2cmds[cmd.name] = cmd
  end
  def loop(strings2cmds)
    print cwd + '> '
    orig_line = STDIN.readline.strip
    line = orig_line.split(/ /)
    return false if line.empty?
    str = line.shift.downcase
    return true if str == 'quit'
    if not cur
      if str != 'use' and str != 'help'
        STDERR.puts 'You must set a user with \'use\' before anything else.'
        return
      end
    end
    args = line
    cmd = strings2cmds[str]
    if cmd
      begin
        cmd.exec args
      rescue Exception => e
        STDERR.puts 'Trouble executing: ' + orig_line
        raise e
      end
    else
      STDERR.puts 'Unknown command: ' + str
    end
    return false
  end
  while true
    break if loop strings2cmds
  end
end

#node_relative_to(node, name) ⇒ Object

TreeNode string -> TreeNode



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/treerepl/repl.rb', line 118

def node_relative_to(node,name)
  if name == '.'
    return node
  elsif name == '..'
    return node.parent
  elsif name == '/'
    return root
  else
    node.children.each do |n|
      if name == n.name
        return n
      end
    end
  end
  return nil
end

#path(from, dest) ⇒ Object

TreeNode TreeNode -> TreeNode[]



136
137
138
139
140
141
142
143
144
145
# File 'lib/treerepl/repl.rb', line 136

def path(from,dest)
  res = []
  trav = dest
  while trav
    res.push trav
    break if trav == from
    trav = trav.parent
  end
  return res.reverse
end

#use(user) ⇒ Object


‘Private’




70
71
72
73
74
75
# File 'lib/treerepl/repl.rb', line 70

def use(user)
  if user
    @cur = user
    @root = user
  end
end