Sha256: 03855dcaf72f2ef96d44978a7621f5e91a22c75b7581ad7daab38f6d0e07e2cf

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'plumb/composable'

module Plumb
  class TaggedHash
    include Composable

    attr_reader :key, :children

    def initialize(hash_type, key, children)
      @hash_type = hash_type
      @key = Key.wrap(key)
      @children = children

      raise ArgumentError, 'all types must be HashClass' if @children.size.zero? || @children.any? do |t|
        !t.is_a?(HashClass)
      end
      raise ArgumentError, "all types must define key #{@key}" unless @children.all? { |t| !!t.at_key(@key) }

      # types are assumed to have literal values for the index field :key
      @index = @children.each.with_object({}) do |t, memo|
        key_type = t.at_key(@key)
        raise ParseError, "key type at :#{@key} #{key_type} must be a Match type" unless key_type.is_a?(MatchClass)

        memo[key_type.children[0]] = t
      end

      freeze
    end

    def call(result)
      result = @hash_type.call(result)
      return result unless result.valid?

      child = @index[result.value[@key.to_sym]]
      return result.invalid(errors: "expected :#{@key.to_sym} to be one of #{@index.keys.join(', ')}") unless child

      child.call(result)
    end

    private

    def _inspect = "TaggedHash[#{@key.inspect}, #{@children.map(&:inspect).join(', ')}]"
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
plumb-0.0.10 lib/plumb/tagged_hash.rb
plumb-0.0.9 lib/plumb/tagged_hash.rb
plumb-0.0.8 lib/plumb/tagged_hash.rb
plumb-0.0.7 lib/plumb/tagged_hash.rb
plumb-0.0.6 lib/plumb/tagged_hash.rb
plumb-0.0.5 lib/plumb/tagged_hash.rb