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



17
18
19
20
21
22
23
24
25
# File 'lib/libcfenjin/cfp_code.rb', line 17

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.



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

def action
  @action
end

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#fnObject (readonly)

Returns the value of attribute fn.



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

def fn
  @fn
end

#linenumObject (readonly)

Returns the value of attribute linenum.



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

def linenum
  @linenum
end

#numObject (readonly)

Returns the value of attribute num.



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

def num
  @num
end

Instance Method Details

#classnameObject

Converts the file path into a class name that is acceptable to Ruby



73
74
75
76
77
# File 'lib/libcfenjin/cfp_code.rb', line 73

def classname
   name = @fn
   name = name.gsub(/[-]/,'_')
	name.gsub(/\W/,'').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



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/libcfenjin/cfp_code.rb', line 58

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



28
29
30
31
32
33
34
# File 'lib/libcfenjin/cfp_code.rb', line 28

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



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/libcfenjin/cfp_code.rb', line 39

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



79
80
81
# File 'lib/libcfenjin/cfp_code.rb', line 79

def methodname
	@action+@num.to_s
end