Class: Code

Inherits:
Object
  • Object
show all
Defined in:
lib/vertigo/code.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = nil) ⇒ Code

Returns a new instance of Code.



5
6
7
8
9
# File 'lib/vertigo/code.rb', line 5

def initialize str=nil
  @lines=[]
  (@lines << str) if str
  @indent=0
end

Instance Attribute Details

#indentObject

Returns the value of attribute indent.



3
4
5
# File 'lib/vertigo/code.rb', line 3

def indent
  @indent
end

#linesObject

Returns the value of attribute lines.



3
4
5
# File 'lib/vertigo/code.rb', line 3

def lines
  @lines
end

Instance Method Details

#<<(thing) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vertigo/code.rb', line 11

def <<(thing)
  if (code=thing).is_a? Code
    code.lines.each do |line|
      @lines << " "*@indent+line.to_s
    end
  elsif thing.is_a? Array
    thing.each do |kode|
      @lines << kode
    end
  elsif thing.nil?
  else
    @lines << " "*@indent+thing.to_s
  end
end

#clean(str) ⇒ Object



33
34
35
36
37
# File 'lib/vertigo/code.rb', line 33

def clean str
  str=str.gsub(/;[\s\n]*;/ ,';')
  str=str.gsub(/;[\s\n]*\)/ ,')')
  str=str.gsub(/,[\s\n]*\)/,')')
end

#finalizeObject



26
27
28
29
30
31
# File 'lib/vertigo/code.rb', line 26

def finalize
  str=@lines.join("\n") if @lines.any?
  str=clean(str)
  return str if @lines.any?
  ""
end

#lastObject



53
54
55
# File 'lib/vertigo/code.rb', line 53

def last
  @lines.last
end

#newlineObject



39
40
41
# File 'lib/vertigo/code.rb', line 39

def newline
  @lines << " "
end

#save_as(filename, verbose = true) ⇒ Object



43
44
45
46
47
# File 'lib/vertigo/code.rb', line 43

def save_as filename,verbose=true
  str=self.finalize
  File.open(filename,'w'){|f| f.puts(str)}
  return filename
end

#sizeObject



49
50
51
# File 'lib/vertigo/code.rb', line 49

def size
  @lines.size
end