lib/ruote/reader/ruby_dsl.rb in ruote-2.2.0 vs lib/ruote/reader/ruby_dsl.rb in ruote-2.3.0
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -20,10 +20,11 @@
# THE SOFTWARE.
#
# Made in Japan.
#++
+require 'sourcify'
require 'ruote/util/ometa'
module Ruote
@@ -90,10 +91,14 @@
def self.to_tree(&block)
RubyDsl.create_branch('x', {}, &block).last.first
end
+ class << self
+ alias tree to_tree
+ end
+
# :nodoc:
#
module RubyDsl
class BranchContext < Ruote::BlankSlate
@@ -118,22 +123,22 @@
def self.create_branch(name, attributes, &block)
name = name[1..-1] while name[0, 1] == '_'
- h = attributes.inject({}) { |h1, a|
+ h = attributes.each_with_object({}) { |a, h1|
- a.is_a?(Hash) ? h1.merge!(a) : h1[a] = nil
+ if a.is_a?(Hash)
+ h1.merge!(a)
+ else
+ h1[a] = nil
+ end
- h1
+ }.remap { |(k, v), h1|
- }.inject({}) { |h1, (k, v)|
-
k = k.is_a?(Regexp) ? k.inspect : k.to_s
h1[k] = to_json(v)
-
- h1
}
c = BranchContext.new(name, h)
c.instance_eval(&block) if block
@@ -144,12 +149,42 @@
case v
when Symbol; v.to_s
when Regexp; v.inspect
when Array; v.collect { |e| to_json(e) }
- when Hash; v.inject({}) { |h, (k, v)| h[k.to_s] = to_json(v); h }
+ when Hash; v.remap { |(k, v), h| h[to_json(k)] = to_json(v) }
+ when Proc; v.to_raw_source + "\n"
else v
end
+ end
+ end
+
+ #
+ # The same .read and .understands? method as the other readers are found here.
+ #
+ module RubyReader
+
+ # Returns true if s seems to contain a Ruby process definition
+ #
+ def self.understands?(s)
+
+ s.match(
+ /\bRuote\.(process_definition|workflow_definition|define)\b/
+ ) != nil
+ end
+
+ # Evaluates the ruby string in the code, but at fist, thanks to the
+ # treechecker, makes sure it doesn't code malicious ruby code (at least
+ # tries very hard).
+ #
+ def self.read(s, treechecker)
+
+ treechecker.definition_check(s)
+ eval(s)
+
+ rescue SyntaxError => se
+ #p se
+ raise ArgumentError.new("Ruby syntax error : #{se.message}")
end
end
end