Class: RubyIt::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_it/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f_in, output = '', path = '') ⇒ Document

Returns a new instance of Document.



12
13
14
15
16
17
18
19
20
# File 'lib/ruby_it/document.rb', line 12

def initialize(f_in, output='', path='')
  if not f_in.index('.r') then
    puts "ERROR input #{f_in} not of form y.rz were output should be y.z"
    exit 1
  end
  @input        = f_in
  @path         = set_path( path )
  @out_filename = set_out_file( output )
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



8
9
10
# File 'lib/ruby_it/document.rb', line 8

def input
  @input
end

#out_filenameObject

Returns the value of attribute out_filename.



9
10
11
# File 'lib/ruby_it/document.rb', line 9

def out_filename
  @out_filename
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/ruby_it/document.rb', line 10

def path
  @path
end

Instance Method Details

#add_parameter(param) ⇒ Object



71
72
73
74
75
76
# File 'lib/ruby_it/document.rb', line 71

def add_parameter( param )
  @erb_parameters ||= Array.new
  Array(param).each do |line|
   @erb_parameters << line
  end
end

#clean_whitespace(erb_text) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/ruby_it/document.rb', line 41

def clean_whitespace( erb_text )
  ##Remove leading white space
  text = erb_text.gsub(/[ \t]*<%(?!=)/, "<%")

  ## Remove white space and line return followed by -%> and convert to erb %>
  text = text.gsub(/-%>([ \t]*[\n\r])?/, "%>")

  return text
end

#erb_conversion(erb_text) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_it/document.rb', line 51

def erb_conversion( erb_text )
  begin
    bre = ERB.new(erb_text, 0, "%")
    ## Set filename so __FILE__ can be used in templates
    bre.filename = @input 

    return bre.result
  rescue => error
    first_line = error.backtrace[0]
    if first_line.match(/\(erb\)\W*(\d*)/)
      $stderr.puts "Error parsing #{@input} on line #{$1}"
    else
      $stderr.puts "Error parsing #{@input}"
    end
    $stderr.puts error
    fail

  end
end

#erb_textObject



23
24
25
# File 'lib/ruby_it/document.rb', line 23

def erb_text
  @erb_text ||= File.read( @input )
end

#get_parametersObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_it/document.rb', line 78

def get_parameters
  parameter_string = '<% '

  #Skip if parameters not defined
  if not @erb_parameters.nil?
    @erb_parameters.each do |param|
      parameter_string <<  param + " \n "
    end
    parameter_string << ' -%>'

  else
    #No Params have been set add comment and close erb tags
    parameter_string << ' #no parameters %>'
  end

  return parameter_string
end

#outputObject



96
97
98
# File 'lib/ruby_it/document.rb', line 96

def output
  return @path + @out_filename
end

#resultObject



31
32
33
34
35
36
37
38
39
# File 'lib/ruby_it/document.rb', line 31

def result
  #Giving The template file acces to the parent object
  $parent = self ## <-- Global bad, but works for this

  #Add params and template file text and clear white space
  text   =  clean_whitespace( get_parameters + erb_text )

  @result ||= erb_conversion( text ) 
end

#to_sObject



27
28
29
# File 'lib/ruby_it/document.rb', line 27

def to_s
  return "File: " +@input.to_s + " Output: " + @path.to_s + @output.to_s 
end

#writeObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ruby_it/document.rb', line 101

def write
  #write evaluated template to file:
  if ::File.writable? self.output or not ::File.exists? self.output
  
    result   = self.result
    file_out = File.open( self.output, 'w' )
    file_out << result
    file_out.close
    
  else
    $stderr.puts "ERROR #{self.output} is not writable"
  end
  
end