lib/surrogate/values.rb in surrogate-0.4.3 vs lib/surrogate/values.rb in surrogate-0.5.0
- old
+ new
@@ -1,63 +1,56 @@
class Surrogate
# Superclass for all types of values. Where a value is anything stored
# in an instance variable on a surrogate, intended to be returned by an api method
class Value
-
# convert raw arguments into a value
def self.factory(*args, &block)
arg = args.first
- # if arg.kind_of? Exception
- # Raiseable.new arg
- # else
- # MethodQueue.new args
- # end
if args.size > 1
ValueQueue.new args
elsif arg.kind_of? Exception
Raisable.new arg
- elsif arg.kind_of? Value
+ elsif arg.kind_of? BaseValue
arg
else
- Value.new arg
+ BaseValue.new arg
end
end
- def initialize(value)
- @value = value
- end
+ # === the current set of possible values ===
- def value(hatchling, method_name)
- @value
- end
+ class BaseValue
+ def initialize(value)
+ @value = value
+ end
- def factory(*args, &block)
- self.class.factory(*args, &block)
+ def value(method_name)
+ @value
+ end
+
+ def factory(*args, &block)
+ Value.factory(*args, &block)
+ end
end
- end
-end
-# the current set of possible values
-
-class Surrogate
- class Value
-
- class Raisable < Value
+ class Raisable < BaseValue
def value(*)
raise @value
end
end
- class ValueQueue < Value
- QueueEmpty = Class.new StandardError
+ class ValueQueue < BaseValue
+ QueueEmpty = Class.new SurrogateError
- def value(hatchling, method_name)
- factory(dequeue).value(hatchling, method_name)
- ensure
- hatchling.unset_ivar method_name if empty?
+ def value(method_name)
+ if empty?
+ raise QueueEmpty
+ else
+ factory(dequeue).value(method_name)
+ end
end
def queue
@value
end