lib/openwfe/expressions/simplerep.rb in openwferu-0.9.13 vs lib/openwfe/expressions/simplerep.rb in openwferu-0.9.14
- old
+ new
@@ -28,12 +28,10 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#++
#
-# $Id: definitions.rb 2725 2006-06-02 13:26:32Z jmettraux $
-#
#
# "made in Japan"
#
# John Mettraux at openwfe.org
@@ -58,10 +56,11 @@
attr_accessor \
:children
def initialize (name, attributes)
+
super()
@name = name
@attributes = attributes
@children = []
end
@@ -75,23 +74,26 @@
#
# Always return the ProgRawExpression class.
#
def raw_expression_class
- return ProgRawExpression
+
+ ProgRawExpression
end
#
# Returns an XML string, containing the equivalent process definition
# in the classical OpenWFE process definition language.
#
def to_s
+
doc = REXML::Document.new()
doc << to_xml
s = ""
doc.write(s, 0)
- return s
+
+ s
end
#
# Returns this representation tree as an XML element (and its children).
#
@@ -110,11 +112,11 @@
else
elt << REXML::Text.new(child.to_s)
end
end
- return elt
+ elt
end
#
# Turns an XML tree into a simple representation
# (beware embedded XML, should do something to stop that,
@@ -153,10 +155,35 @@
rep
end
#
+ # Evals the given code (string) into a SimpleExpRepresentation.
+ #
+ def self.from_code (code)
+
+ ProcessDefinition.eval_ruby_process_definition code
+ end
+
+ #
+ # Evals the given string a return its SimpleExpRepresentation
+ # equivalent, ready for evaluation or rendering (fluo).
+ #
+ def self.from_s (s)
+
+ s = s.strip
+
+ if s[0, 1] == "<"
+
+ from_xml s
+ else
+
+ from_code s
+ end
+ end
+
+ #
# Returns a string containing the ruby code that generated this
# raw representation tree.
#
def to_code_s (indentation = 0)
@@ -174,11 +201,11 @@
s << sa[1..-1] if sa.length > 0
if @children.length > 0
s << " do\n"
@children.each do |child|
- if child.respond_to? :to_code_s
+ if child.respond_to?(:to_code_s)
s << child.to_code_s(indentation + 1)
else
s << ind
s << tab
s << "'#{child.to_s}'"
@@ -187,10 +214,30 @@
end
s << ind
s << "end"
end
- return s
+ s
+ end
+
+ #
+ # Turns this simple representation into an array
+ # (something suitable for to_json()).
+ #
+ def to_a
+
+ cs = @children.collect do |child|
+
+ if child.respond_to?(:to_a)
+
+ child.to_a
+ else
+
+ child.to_s
+ end
+ end
+
+ [ @name, @attributes, cs ]
end
end
end