Class: NRSER::Types::HashOfType

Inherits:
HashType show all
Defined in:
lib/nrser/types/hashes.rb

Overview

Note:

Construct HashOfType types using the Hash factory.

A Hash type with typed keys and/or values.

Instance Attribute Summary collapse

Attributes inherited from IsA

#mod

Instance Method Summary collapse

Methods inherited from HashType

#default_symbolic

Methods inherited from IsA

#==, #custom_from_data, #has_from_data?, #init_from_data?

Methods inherited from Type

#===, #builtin_inspect, #check, #check!, #default_name, #default_symbolic, #from_data, #from_s, #has_from_data?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #symbolic, #test, #to_data, #to_proc, #to_s, #union, #xor

Constructor Details

#initialize(keys: NRSER::Types.any, values: NRSER::Types.any, **options) ⇒ HashOfType

Constructor



166
167
168
169
170
171
172
173
# File 'lib/nrser/types/hashes.rb', line 166

def initialize  keys: NRSER::Types.any,
                values: NRSER::Types.any,
                **options
  super **options
  
  @keys = NRSER::Types.make keys
  @values = NRSER::Types.make values
end

Instance Attribute Details

#keysNRSER::Types::Type (readonly)

The type of the hash keys.

Returns:



153
154
155
# File 'lib/nrser/types/hashes.rb', line 153

def keys
  @keys
end

#valuesNRSER::Types::Type (readonly)

The type of the hash values.

Returns:



160
161
162
# File 'lib/nrser/types/hashes.rb', line 160

def values
  @values
end

Instance Method Details

#explainString

Returns:

See Also:



206
207
208
# File 'lib/nrser/types/hashes.rb', line 206

def explain
  "Hash<#{ keys.explain }, #{ values.explain }>"
end

#has_from_s?Boolean

Overridden to check that both the #keys and #values types can load from a string.

Returns:

See Also:



184
185
186
# File 'lib/nrser/types/hashes.rb', line 184

def has_from_s?
  !@from_s.nil? || [keys, values].all?( &:has_from_s )
end

#test?(value) ⇒ Boolean

Returns:

See Also:



193
194
195
196
197
198
199
# File 'lib/nrser/types/hashes.rb', line 193

def test? value
  return false unless super( value )
  
  value.all? { |k, v|
    keys.test( k ) && values.test( v )
  }
end