Sha256: 246a925913dd57ff94220eb79ff9c1e05c46ebb6128e6b9c54dc0b4fa5ac7ca3

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

require "singleton"

module VagrantPlugins
  module CommandServe
    class Mappers
      class ProcRegistry
        include Singleton

        def initialize
          @m = Mutex.new
          @stored = {}
        end

        def register(value)
          @m.synchronize do
            if !value.is_a?(Proc)
              raise TypeError,
                "Expected type `Proc' but received `#{value.class}'"
            end

            # If this is already stored, return existing ID
            idx = @stored.index(value)
            return idx if idx

            # Create a new ID and store
            id = SecureRandom.uuid
            @stored[id] = value
            id
          end
        end

        def fetch(idx)
          @m.synchronize do
            if !@stored.key?(idx)
              raise KeyError,
                "No `Proc' registered for given ID (#{idx})"
            end
          end
          @stored[idx]
        end
      end

      class ProcToProto < Mapper
        def initialize
          super(
            inputs: [
              Input.new(type: Proc),
            ],
            output: SDK::Args::ProcRef,
            func: method(:converter),
          )
        end

        def converter(value)
          SDK::Args::ProcRef.new(
            id: ProcRegistry.instance.register(value),
          )
        end
      end

      class ProcFromProto < Mapper
        def initialize
          super(
            inputs: [
              Input.new(type: SDK::Args::ProcRef),
            ],
            output: Proc,
            func: method(:converter)
          )
        end

        def converter(proto)
          ProcRegistry.instance.fetch(proto.id)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
vagrant-unbundled-2.3.6.0 plugins/commands/serve/mappers/proc.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/plugins/commands/serve/mappers/proc.rb
vagrant-unbundled-2.3.3.0 plugins/commands/serve/mappers/proc.rb
vagrant-unbundled-2.3.2.0 plugins/commands/serve/mappers/proc.rb