Sha256: 86733aa94341ef13bb1e14b9457ee29a798e6ee641e38129d48ec22f5f1a6e74

Contents?: true

Size: 1.27 KB

Versions: 14

Compression:

Stored size: 1.27 KB

Contents

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 args.size > 1
        ValueQueue.new args
      elsif arg.kind_of? Exception
        Raisable.new arg
      elsif arg.kind_of? BaseValue
        arg
      else
        BaseValue.new arg
      end
    end

    # === the current set of possible values ===

    class BaseValue
      def initialize(value)
        @value = value
      end

      def value(method_name)
        @value
      end

      def factory(*args, &block)
        Value.factory(*args, &block)
      end
    end


    class Raisable < BaseValue
      def value(*)
        raise @value
      end
    end


    class ValueQueue < BaseValue
      QueueEmpty = Class.new SurrogateError

      def value(method_name)
        if empty?
          raise QueueEmpty
        else
          factory(dequeue).value(method_name)
        end
      end

      def queue
        @value
      end

      def dequeue
        raise QueueEmpty if empty?
        queue.shift
      end

      def empty?
        queue.empty?
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
surrogate-0.8.0 lib/surrogate/values.rb
surrogate-0.7.0 lib/surrogate/values.rb
surrogate-0.6.5 lib/surrogate/values.rb
surrogate-0.6.4 lib/surrogate/values.rb
surrogate-0.6.3 lib/surrogate/values.rb
surrogate-0.6.2 lib/surrogate/values.rb
surrogate-0.6.1 lib/surrogate/values.rb
surrogate-0.6.0 lib/surrogate/values.rb
surrogate-0.5.5 lib/surrogate/values.rb
surrogate-0.5.4 lib/surrogate/values.rb
surrogate-0.5.3 lib/surrogate/values.rb
surrogate-0.5.2 lib/surrogate/values.rb
surrogate-0.5.1 lib/surrogate/values.rb
surrogate-0.5.0 lib/surrogate/values.rb