Browse files

Break up monster file.

  • Loading branch information...
1 parent 46e472f commit 0fcc9137100f23d7d95f084264e1bb5a7423cf79 @threedaymonk threedaymonk committed Mar 23, 2012
Showing with 167 additions and 155 deletions.
  1. +13 −155 lib/sibyl/nodes.rb
  2. +25 −0 lib/sibyl/nodes/brancher.rb
  3. +20 −0 lib/sibyl/nodes/jump.rb
  4. +9 −0 lib/sibyl/nodes/metadata.rb
  5. +23 −0 lib/sibyl/nodes/statement.rb
  6. +77 −0 lib/sibyl/nodes/step.rb
View
168 lib/sibyl/nodes.rb
@@ -15,6 +15,15 @@ def initialize(#{fields.map(&:to_s).join(", ")})
attr_accessor *fields
end
+ def def_true(*names)
+ def_boolean true, *names
+ end
+
+ def def_false(*names)
+ def_boolean false, *names
+ end
+
+ private
def def_boolean(value, *names)
names.each do |name|
class_eval <<-END
@@ -24,14 +33,6 @@ def #{name}?
END
end
end
-
- def def_true(*names)
- def_boolean true, *names
- end
-
- def def_false(*names)
- def_boolean false, *names
- end
end
def_false :metadata, :option, :statement, :step, :sink, :branch
@@ -42,152 +43,9 @@ def evaluate(context)
context.instance_eval(expression)
end
end
-
- class Metadata < Node
- construct_with :key, :value
- def_true :metadata
- end
-
- class Step < Node
- construct_with :type, :name, :body
- def_true :step
-
- def branches
- body.select(&:branch?)
- end
-
- def options
- body.select(&:option?)
- end
-
- def statements
- body.select(&:statement?)
- end
-
- def inputs
- body.select(&:option?).map(&:text)
- end
-
- def exits
- branches.map(&:branches).flatten.map(&:target).uniq
- end
-
- def compute(input, context)
- context.input = InputHandler.deserialize(type, input)
- statements.each do |s|
- s.execute(context)
- end
- branches.each do |o|
- result = o.compute(context)
- return result if result
- end
- raise InvalidInput
- end
-
- def l10n_keys
- prefix = keyify(name)
- standard = ["#{prefix}.title"]
- standard + options.map { |option| "#{prefix}.options.#{option.text}" }
- end
-
- def validate!
- if type.to_s == "multiple"
- raise InvalidNode, "multiple step '#{name}' has no options" if options.none?
- raise InvalidNode, "multiple step '#{name}' has non-option branches" if (branches - options).any?
- else
- raise InvalidNode, "non-multiple step '#{name}' has options" if options.any?
- raise InvalidNode, "step '#{name}' has no outputs" if branches.none?
- end
- end
-
- private
- def keyify(s)
- s.downcase.gsub(/^\W+|\W+$/, "").gsub(/\W+/, "_")
- end
- end
-
- class Outcome < Step
- construct_with :name
- def_true :sink
-
- def exits
- []
- end
-
- def body
- []
- end
-
- def validate!
- end
- end
-
- class Jump < Node
- construct_with :target
-
- def compute(*)
- target
- end
- end
-
- class ConditionalJump < Jump
- include Evaluable
- construct_with :expression, :target
-
- def compute(context)
- if evaluate(context)
- target
- else
- nil
- end
- end
- end
-
- class Brancher < Node
- construct_with :branches
- def_true :branch
-
- def compute(context)
- branches.each do |b|
- result = b.compute(context)
- return result if result
- end
- nil
- end
- end
-
- class OptionBrancher < Brancher
- construct_with :text, :branches
- def_true :option
-
- def compute(context)
- if context.input == text
- super(context)
- else
- nil
- end
- end
- end
-
- class AbstractStatement < Node
- include Evaluable
- def_true :statement
- end
-
- class Set < AbstractStatement
- construct_with :key, :expression
-
- def execute(context)
- context.__send__ "#{key}=", evaluate(context)
- end
- end
-
- class Reject < AbstractStatement
- construct_with :expression
-
- def execute(context)
- raise PreconditionFailed if evaluate(context)
- end
- end
end
end
+
+Dir[File.expand_path("../nodes/*.rb", __FILE__)].each do |path|
+ require "sibyl/nodes/#{File.basename(path, ".rb")}"
+end
View
25 lib/sibyl/nodes/brancher.rb
@@ -0,0 +1,25 @@
+module Sibyl
+ module Nodes
+ class Brancher < Node
+ construct_with :branches
+ def_true :branch
+
+ def compute(context)
+ branches.each do |b|
+ result = b.compute(context)
+ return result if result
+ end
+ nil
+ end
+ end
+
+ class OptionBrancher < Brancher
+ construct_with :text, :branches
+ def_true :option
+
+ def compute(context)
+ context.input == text ? super(context) : nil
+ end
+ end
+ end
+end
View
20 lib/sibyl/nodes/jump.rb
@@ -0,0 +1,20 @@
+module Sibyl
+ module Nodes
+ class Jump < Node
+ construct_with :target
+
+ def compute(*)
+ target
+ end
+ end
+
+ class ConditionalJump < Jump
+ include Evaluable
+ construct_with :expression, :target
+
+ def compute(context)
+ evaluate(context) ? super(context) : nil
+ end
+ end
+ end
+end
View
9 lib/sibyl/nodes/metadata.rb
@@ -0,0 +1,9 @@
+module Sibyl
+ module Nodes
+ class Metadata < Node
+ construct_with :key, :value
+ def_true :metadata
+ end
+ end
+end
+
View
23 lib/sibyl/nodes/statement.rb
@@ -0,0 +1,23 @@
+module Sibyl
+ module Nodes
+ class Set < Node
+ include Evaluable
+ def_true :statement
+ construct_with :key, :expression
+
+ def execute(context)
+ context.__send__ "#{key}=", evaluate(context)
+ end
+ end
+
+ class Reject < Node
+ include Evaluable
+ def_true :statement
+ construct_with :expression
+
+ def execute(context)
+ raise PreconditionFailed if evaluate(context)
+ end
+ end
+ end
+end
View
77 lib/sibyl/nodes/step.rb
@@ -0,0 +1,77 @@
+module Sibyl
+ module Nodes
+ class Step < Node
+ construct_with :type, :name, :body
+ def_true :step
+
+ def branches
+ body.select(&:branch?)
+ end
+
+ def options
+ body.select(&:option?)
+ end
+
+ def statements
+ body.select(&:statement?)
+ end
+
+ def inputs
+ body.select(&:option?).map(&:text)
+ end
+
+ def exits
+ branches.map(&:branches).flatten.map(&:target).uniq
+ end
+
+ def compute(input, context)
+ context.input = InputHandler.deserialize(type, input)
+ statements.each do |s|
+ s.execute(context)
+ end
+ branches.each do |o|
+ result = o.compute(context)
+ return result if result
+ end
+ raise InvalidInput
+ end
+
+ def l10n_keys
+ prefix = keyify(name)
+ standard = ["#{prefix}.title"]
+ standard + options.map { |option| "#{prefix}.options.#{option.text}" }
+ end
+
+ def validate!
+ if type.to_s == "multiple"
+ raise InvalidNode, "multiple step '#{name}' has no options" if options.none?
+ raise InvalidNode, "multiple step '#{name}' has non-option branches" if (branches - options).any?
+ else
+ raise InvalidNode, "non-multiple step '#{name}' has options" if options.any?
+ raise InvalidNode, "step '#{name}' has no outputs" if branches.none?
+ end
+ end
+
+ private
+ def keyify(s)
+ s.downcase.gsub(/^\W+|\W+$/, "").gsub(/\W+/, "_")
+ end
+ end
+
+ class Outcome < Step
+ construct_with :name
+ def_true :sink
+
+ def exits
+ []
+ end
+
+ def body
+ []
+ end
+
+ def validate!
+ end
+ end
+ end
+end

0 comments on commit 0fcc913

Please sign in to comment.