Class: Rube::Generator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = $stdout, options = {}) ⇒ Generator

Returns a new instance of Generator.



86
87
88
89
90
91
92
93
94
95
# File 'lib/rube.rb', line 86

def initialize(stdout=$stdout, options={})
  @stdout = stdout
  @tasks = options[:tasks] || []
  @trim_level = nil
  self.disable_percent = options[:disable_percent]
  self.trim_level = options[:trim_level]
  @from_command_line = options[:from_command_line]
  @safety = options[:safety]
  @explicit = options[:explicit]
end

Instance Attribute Details

#disable_percentObject

Returns the value of attribute disable_percent.



84
85
86
# File 'lib/rube.rb', line 84

def disable_percent
  @disable_percent
end

#from_command_lineObject

Returns the value of attribute from_command_line.



83
84
85
# File 'lib/rube.rb', line 83

def from_command_line
  @from_command_line
end

#safetyObject

Returns the value of attribute safety.



83
84
85
# File 'lib/rube.rb', line 83

def safety
  @safety
end

#tasksObject

Returns the value of attribute tasks.



83
84
85
# File 'lib/rube.rb', line 83

def tasks
  @tasks
end

#trim_levelObject

Returns the value of attribute trim_level.



84
85
86
# File 'lib/rube.rb', line 84

def trim_level
  @trim_level
end

#trim_modeObject (readonly)

Returns the value of attribute trim_mode.



84
85
86
# File 'lib/rube.rb', line 84

def trim_mode
  @trim_mode
end

Class Method Details

.error(exception, msg) ⇒ Object

Issue an error message or exception

Raises:

  • (exception)


176
177
178
# File 'lib/rube.rb', line 176

def self.error(exception, msg)
  raise exception, msg
end

Instance Method Details

#add_task(type, task) ⇒ Object

Add a task



120
121
122
123
124
125
126
# File 'lib/rube.rb', line 120

def add_task(type, task)
  case type
  when :require, :eval, :template then  @tasks << [type, task]
  else                                  raise "Invalid task type #{type.inspect}"
  end
  self # Allow chaining
end

#error(exception, msg) ⇒ Object



180
181
182
# File 'lib/rube.rb', line 180

def error(exception, msg)
  self.class.error exception, msg
end

#execute(p) ⇒ Object

Execute a single task



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rube.rb', line 140

def execute(p)
  case p.first
  when :require
    protected_require(p.last)
  when :eval
    protected_eval(p.last)
  when :template
    protected_erb(p.last)
  else
    raise "Unexpected task #{p.inspect}"
  end
end

#generateObject

Run all the tasks



129
130
131
132
133
134
135
136
137
# File 'lib/rube.rb', line 129

def generate
  @eval_context = EvalContext.new
  @binding = @eval_context.sandbox
  saved_stdout, $stdout = $stdout, @stdout
  @tasks.each {|p| execute p }
  nil
ensure
  $stdout = saved_stdout
end

#protected_erb(template) ⇒ Object

Process an erb template



169
170
171
172
173
# File 'lib/rube.rb', line 169

def protected_erb(template)
  error MissingTemplateError, "Can't find template file #{template}" unless File.exists?(template)
  source = File.read(template)
  @stdout.puts ERB.new(source, @safety, @trim_mode).result(@binding)
end

#protected_eval(src) ⇒ Object

Evaluate inline ruby code



161
162
163
164
165
166
# File 'lib/rube.rb', line 161

def protected_eval(src)
  bind_at = @binding
  @eval_context.instance_eval{eval src, bind_at}
rescue => e
  error ScriptError, "Error executing source:\n#{src}\n\n#{e.to_s}"
end

#protected_require(r) ⇒ Object

Load a ruby file or library



154
155
156
157
158
# File 'lib/rube.rb', line 154

def protected_require(r)
  @eval_context.instance_eval {require r}
rescue LoadError
  error MissingRequireError, "Can't find require file #{r}"
end

#trim_mode_opt(trim_mode) ⇒ Object

Convert command line trim_mode to a form erb will understand



108
109
110
111
112
113
114
115
116
117
# File 'lib/rube.rb', line 108

def trim_mode_opt(trim_mode)
  mode = disable_percent ? '' : '%'
  mode += case trim_mode.to_s
  when '0','' then  ''
  when '1'    then  '>'
  when '2'    then  '<>'
  when '-'    then  '-'
  else              error BadArgumentError, "Invalid trim mode #{trim_mode}. Should be 0, 1, 2, or -"
  end
end