diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-07-22 16:55:01 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-07-22 16:55:01 +0100 |
commit | fd23821950f0562a8995735105cd31fdc6d55933 (patch) | |
tree | 967df2f5647803a6c46f4d52003b2231c1de72cb /lib/object_builder.rb | |
parent | d3a3cfef9650b08f62db62bd7e86b673f9d77d0b (diff) |
* Rejigged configuration
* Added --test and --verbose flags for the server config
* Started proper test suite
* Config parsing now gives more sensible errors + backtrace
* Rejigged people and source lists
Diffstat (limited to 'lib/object_builder.rb')
-rw-r--r-- | lib/object_builder.rb | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/lib/object_builder.rb b/lib/object_builder.rb index 7cb808c..a455b3e 100644 --- a/lib/object_builder.rb +++ b/lib/object_builder.rb @@ -41,12 +41,14 @@ # TODO: finish this convoluted example, if it kills me # class ObjectBuilder - class BuildException < Exception; end + class BuildException < StandardError; end - attr_reader :result + attr_reader :result + attr_accessor :block_result def initialize(context, *args) @context = context + @result = nil builder_setup(*args) end @@ -93,11 +95,38 @@ class ObjectBuilder end def load(file) + parse(File.read(file), file) + end + + def parse(string, file="string") builder = self.new - builder.instance_eval(File.read(file), file) + begin + builder.instance_eval(string, file) + rescue NameError => ex + # + # Ugh. Catch NameError and re-raise as a BuildException + # + f,l = ex.backtrace.first.split(":").first(2) + if f == file + build_ex = BuildException.new "Unknown word `#{ex.name}' in #{file} at line #{l}" + build_ex.set_backtrace ex.backtrace + raise build_ex + else + raise ex + end + rescue SyntaxError, ArgumentError => ex + if ex.backtrace.find{|l| l =~ /^#{file}:(\d+):/} + build_ex = BuildException.new "#{ex.message} in #{file} at line #{$1}" + build_ex.set_backtrace ex.backtrace + raise build_ex + else + raise ex + end + end + builder.result end - + def inherited(*args) initialize_class end @@ -106,7 +135,7 @@ class ObjectBuilder @words = {} end end - + initialize_class end |