Module: Id

Includes:
IdEdit, IdHeader, IdSubstitutions
Defined in:
lib/id.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IdEdit

#edit

Methods included from IdOperations

#process_operations

Methods included from IdHeader

#display_node, #header, #print_location

Methods included from IdSubstitutions

#sub

Instance Attribute Details

#DirObject

Returns the value of attribute Dir.



10
11
12
# File 'lib/id.rb', line 10

def Dir
  @Dir
end

#nodesObject

Returns the value of attribute nodes.



10
11
12
# File 'lib/id.rb', line 10

def nodes
  @nodes
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/id.rb', line 10

def path
  @path
end

Instance Method Details

#back(node) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/id.rb', line 166

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

#contains?(node, line) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/id.rb', line 17

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

#delete(node, line = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/id.rb', line 141

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
  @nodes["a"] = `ls`.split("\n")
end

#initialize_idObject



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

def initialize_id
  @references = ["<", "%"]
  @promptnode = nil
  @default_prompt = "<> ".style("blue", "black")
  @prompt = @default_prompt
  @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



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

def ins node, line = nil, before = nil
  if line != "" and node != ""
    @nodes[node] = [] if not @nodes[node]
    unless contains?(node, line)
      unless before
        @nodes[node] << line if line
      else
        if @nodes[node].find_index(before)
          @nodes[node].insert(@nodes[node].find_index(before), line)
        end
      end
    end
  save
  end
end

#is_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'lib/id.rb', line 101

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

#is_node?(line) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/id.rb', line 97

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

#loadObject



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/id.rb', line 127

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



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

def msg message
  @msg = message
end

#node_changedObject



47
48
49
50
51
# File 'lib/id.rb', line 47

def node_changed
  @promptnode = nil
  @prompt = @default_prompt
  @prompt = " # ".style("blue", "black") if @raw
end

#parse_op(line, operator) ⇒ Object



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

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

#readlineObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/id.rb', line 53

def readline
  if @promptnode
    idx = @nodes[@node].length
    pn = @nodes[@promptnode]
    if pn[idx]
      @prompt = pn[idx].style("green", "black") + " > ".style("blue", "black")
      @prompt = " # ".style("blue", "black") if @raw
    else
      @prompt = @default_prompt
    end
  end
  line = Readline.readline(@prompt, true)
  unless line
    puts
    exit
  else
    line.gsub "\n", ""
  end
end

#saveObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/id.rb', line 110

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 name == "a"
    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

#strip_pfx(line) ⇒ Object



21
22
23
# File 'lib/id.rb', line 21

def strip_pfx line
  return sub(line[1..-1])
end