Module: Id

Includes:
Edit, Header
Defined in:
lib/id.rb

Constant Summary

Constants included from Operations

Operations::Operand, Operations::Operator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Edit

#edit

Methods included from Operations

#process_operation, #process_operations

Methods included from Header

#display_node, #header, #print_location

Instance Attribute Details

#DirObject

Returns the value of attribute Dir.



13
14
15
# File 'lib/id.rb', line 13

def Dir
  @Dir
end

#nodesObject

Returns the value of attribute nodes.



13
14
15
# File 'lib/id.rb', line 13

def nodes
  @nodes
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/id.rb', line 13

def path
  @path
end

Instance Method Details

#back(node) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/id.rb', line 134

def back node
  node = @path[-1]
  @path.pop 
  if node
    edit node
  else
    exit
  end
  
end

#contains?(node, line) ⇒ Boolean

Returns:

  • (Boolean)


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

def contains? node, line
  return !!@nodes[node].find_index(line) if @nodes[node]
end

#delete(node, line = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/id.rb', line 110

def delete node, line = nil
  if node == line
    @nodes[node].delete line
    save
    return
  end

  if not contains?("p", line) or node == "p"
    if @nodes[node].find_index line
      @nodes[node].delete line
      save
      if is_node?(line) and not is_line?(line) and not line == @Root
        @nodes[line].each {|i| delete line, i}
        @nodes.delete line 
        File.delete(line) if `ls -a`.split("\n").find_index line
        save
      end
    end
    
  else
    msg '"' + line + '" is protected'
  end
end

#initialize_idObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/id.rb', line 23

def initialize_id
  @prompt = "<> ".col("cyan", "black")
  @Root = "idt"
  @raw = false
  @msg = ""
  @Home = ENV["HOME"] + "/"
  @Dir = @Home + ".nodes/"
  @included_nodes = []
  Dir.mkdir @Dir if not Dir.exists? @Dir
  Dir.chdir @Dir
  system("touch ../.idt-location")
  unless File.open("../.idt-location").readlines()[0] == @Root + "\n"
    system("echo #{@Root} > ../.idt-location")
  end
  @nodes = {}
  @path = []
  load
end

#ins(node, line = nil, before = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/id.rb', line 60

def ins node, line = nil, before = nil
  if line != "" and node != ""
    @nodes[node] = [] if not @nodes[node]
    @nodes[node] << line if line
  save
  end
end

#is_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/id.rb', line 72

def is_line? line
  found = false
  @nodes.values.each do |node|
    found = true if node.find_index line
  end
  return found
end

#is_node?(line) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/id.rb', line 68

def is_node? line
  !!@nodes.keys.find_index(line)
end

#loadObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/id.rb', line 96

def load
  `ls -a`.split("\n").each do |name|
    next if Dir.exists? name
    @nodes[name] = File.open(name).readlines
    @nodes[name].collect! {|line| line.gsub "\n", ""}
  end
  system("touch ../.idt-location")
  pathfile = File.open("../.idt-location")
  location = pathfile.readlines.collect{|node| node.gsub "\n", ""}
  puts location.to_s
  @path = location[0..-2]
  @node = location[-1]
end

#msg(message) ⇒ Object



15
16
17
# File 'lib/id.rb', line 15

def msg message
  @msg = message
end

#parse_op(line, operator) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/id.rb', line 52

def parse_op line, operator
  if (operands = line.split(operator)).length > 1
    return [operands[0], operands[1]]
  else
    return nil
  end
end

#readlineObject



42
43
44
45
46
47
48
49
50
# File 'lib/id.rb', line 42

def readline
  line = Readline.readline(@prompt, true)
  unless line
    puts
    exit
  else
    line.gsub "\n", ""
  end
end

#saveObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/id.rb', line 80

def save
  pathfile = File.open("../.idt-location", "w")
  @path.each {|node| pathfile.write node + "\n"}
  pathfile.write @node
  pathfile.close
  @nodes.each do |name, contents|
    next if not name or name == ""
    next if Dir.exists? name.to_s
    file = File.open name.to_s, "w"
    contents.each do |line|
      file.write line + "\n"
    end
    file.close
  end
end