Class: Code

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent = 0) ⇒ Code

Returns a new instance of Code.



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

def initialize indent=0
  @code=[]
  @indent=indent
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#indentObject

Returns the value of attribute indent.



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

def indent
  @indent
end

Instance Method Details

#<<(str) ⇒ Object



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

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

#finalize(dot = false) ⇒ Object



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

def finalize dot=false
  if dot
    return @code.join('\n')
  end
  @code.join("\n") if @code.any?
end

#newlineObject



32
33
34
# File 'lib/code.rb', line 32

def newline
  @code << " "
end

#save_as(filename, verbose = true, sep = "\n") ⇒ Object



36
37
38
39
40
41
# File 'lib/code.rb', line 36

def save_as filename,verbose=true,sep="\n"
  str=self.finalize
  File.open(filename,'w'){|f| f.puts(str)}
  puts "saved code in file #{filename}" if verbose
  return filename
end

#sizeObject



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

def size
  @code.size
end