Class: Cfruby::Cfp_Code

Inherits:
Object
  • Object
show all
Defined in:
lib/libcfenjin/cfp_code.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn, num, action, argsp, linenum, code) ⇒ Cfp_Code

Code snipped in fn, block num num, block action starting at linenum containing code



13
14
15
16
17
18
19
20
21
# File 'lib/libcfenjin/cfp_code.rb', line 13

def initialize fn,num,action,argsp,linenum,code
	@fn      = fn
	@num     = num
	@action  = action
	@args    = argsp
	@args    = [] if !@args
	@linenum = linenum
	@code    = code
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



9
10
11
# File 'lib/libcfenjin/cfp_code.rb', line 9

def action
  @action
end

#argsObject (readonly)

Returns the value of attribute args.



9
10
11
# File 'lib/libcfenjin/cfp_code.rb', line 9

def args
  @args
end

#codeObject (readonly)

Returns the value of attribute code.



9
10
11
# File 'lib/libcfenjin/cfp_code.rb', line 9

def code
  @code
end

#fnObject (readonly)

Returns the value of attribute fn.



9
10
11
# File 'lib/libcfenjin/cfp_code.rb', line 9

def fn
  @fn
end

#linenumObject (readonly)

Returns the value of attribute linenum.



9
10
11
# File 'lib/libcfenjin/cfp_code.rb', line 9

def linenum
  @linenum
end

#numObject (readonly)

Returns the value of attribute num.



9
10
11
# File 'lib/libcfenjin/cfp_code.rb', line 9

def num
  @num
end

Instance Method Details

#classnameObject



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

def classname
	@fn.gsub(/\/|\./,'').capitalize
end

#dump(handle = nil) ⇒ Object

Dumps to source code of a snippet to stderr - when there is a problem (for example # a syntax error during the compile phase). When unit tests are running output is disabled.

Output to stderr can be overridden by passing a file handle



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/libcfenjin/cfp_code.rb', line 54

def dump handle=nil
	begin
		Test::Unit.class
		# if unit testing do nothing
	rescue NameError
		# if not unit testing
		handle = $stderr if handle == nil
		handle.print "\n==== Snippet #{@fn}-#{@action}-#{@num}(#{@args.join(',')}) ====\n"
		each do | line, num |
			handle.print "#{num.to_s.rjust(3)}: ",line,"\n"
		end
	end
end

#eachObject

Return each line with a line number



24
25
26
27
28
29
30
# File 'lib/libcfenjin/cfp_code.rb', line 24

def each
	num = 1
	@code.each do | line |
		yield line, @linenum+num
		num += 1
	end
end

#encapsulate(tlines) ⇒ Object

Return an array of (transated) source code lines encapsulated in a class and method



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/libcfenjin/cfp_code.rb', line 35

def encapsulate tlines
	a = []
	a.push "\tclass "+classname
	a.push "\tinclude CfrubyRuntime"
	a.push "\t\tdef "+methodname+"(#{@args.join(',')})"
	tlines.each do | line |
		a.push "\t\t\t"+line
	end
	a.push "\t\tend"
	a.push "\tend"
	a
end

#methodnameObject



72
73
74
# File 'lib/libcfenjin/cfp_code.rb', line 72

def methodname
	@action+@num.to_s
end