Module: Operations

Included in:
Edit
Defined in:
lib/operations.rb

Constant Summary collapse

Operator =
/\+|-|=|<|>|\/|\||!|\.|\*/
Operand =
/[^+-=<>\/|!.*]*/

Instance Method Summary collapse

Instance Method Details

#process_operation(l) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
65
66
67
68
69
70
71
72
73
74
75
76
77
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
# File 'lib/operations.rb', line 6

def process_operation l
  line = l
  return false if @raw
  op = nil
  op2 = nil
  op = l[Operator] 
  
  if line[0] == op 
    op1 = @node
    op2 = line[1..-1]
  elsif line[-1] == op 
    op1 = line[0..-2]
  else
    op1, op2 = parse_op line, op
  end

  if not op2 or op2 == ""
    if @nodes[@node][-1]
      op2 =  @nodes[@node][-1]
    else
      op2 = @node
    end
  end

  # process operators

  if op == "*"
    ins op1
    @nodes[op1].each do |n|
      ins n, op2
    end
    return true
  end

  if op == "+"
    ins op1
    ins op2
    @nodes[op1] += @nodes[op2]
    return true
  end

  if op == "."
    ins op2 
    if is_node? op2
      @path << @node
      edit op2
    end
    return true
  end

  if op == "-"
    ins op1
    ins op2
    @nodes[op2].each do |l|
      @nodes[op1].delete l
    end
    return true
  end

  if op == "="
    ins op1
    op2="" unless op2   
    @nodes[op1][0] = op2
    return true
  end

  if op == ">"
    ins op2, op1
    return true
  end

  if op == "<"
    ins op1, op2
    return true
  end

  if op == "/" 
    ins op1
    delete op1, op2 
    return true
  end

  if op == "|"
    ins op1
    return true unless op2
    @nodes[@node] += `cat #{@Dir+op1}|#{op2}`.split("\n")
    return true
  end

  if op == "!"
    ins op1
    return true unless op2
    @nodes[@node] = `cat #{@Dir+op1}|#{op2}`.split("\n")
    return true
  end

  return false
end

#process_operations(l) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/operations.rb', line 105

def process_operations l
  return false unless l[Operator] 
  operations = []
  while l != ""
    operations << l[/#{Operand}#{Operator}#{Operand}/]
    if l[/#{Operand}#{Operator}/]
      l[/#{Operand}#{Operator}/] = ''
    else
      l = ""
    end
  end
  return false if operations == []
  operations.compact.reverse.each {|operation| 
    unless process_operation operation
      return false
    end
  }
  return true
end