Sha256: f4a3fc7df64deb151ab812816d1fdd947ee0dc5cd0bc65d604f35ffee93dac79

Contents?: true

Size: 882 Bytes

Versions: 7

Compression:

Stored size: 882 Bytes

Contents

# frozen_string_literal: true

class Code
  class Type
    class Hash < Type
      attr_reader :hash

      def initialize(hash)
        @hash = hash.transform_keys { |key| Object::String.new(key) }
      end

      def valid?(argument)
        return false unless argument.is_a?(Object::Dictionary)

        argument = argument.raw
        (argument.keys + hash.keys).uniq.all? do |key|
          if hash[key]
            valid_for?(expected: hash[key], actual: argument[key] || Object::Nothing.new)
          else
            false
          end
        end
      end

      def min_arguments
        hash.sum { |_, value| min_arguments_of(value) }
      end

      def max_arguments
        hash.sum { |_, value| max_arguments_of(value) }
      end

      def name
        "{#{hash.map { |key, value| "#{key.inspect} => #{value.name}" }.join(", ")}}"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
code-ruby-0.7.4 lib/code/type/hash.rb
code-ruby-0.7.3 lib/code/type/hash.rb
code-ruby-0.7.2 lib/code/type/hash.rb
code-ruby-0.7.1 lib/code/type/hash.rb
code-ruby-0.7.0 lib/code/type/hash.rb
code-ruby-0.6.7 lib/code/type/hash.rb
code-ruby-0.6.6 lib/code/type/hash.rb