Class: NRSER::Types::EnumerableType

Inherits:
IsA show all
Defined in:
lib/nrser/types/enumerables.rb

Overview

Type class that parameterizes Enumerable values of a homogeneous type.

Examples:

array_of_int = NRSER::Types::EnumerableType.new Array, Integer
array_of_int.test? [1, 2, 3]          #=> true
array_of_int.test? [1, 2, 'three']    #=> false
array_of_int.test? 'blah'             #=> false
array_of_int.test? []                 #=> true

Instance Attribute Summary collapse

Attributes inherited from IsA

#mod

Display Instance Methods collapse

Instance Method Summary collapse

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_from_s?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #symbolic, #test, #to_data, #to_proc, #to_s, #union, #xor

Constructor Details

#initialize(enumerable_type = ::Enumerable, entry_type = self.Top, **options) ⇒ EnumerableType

Instantiate a new ‘EnumerableType`.



52
53
54
55
56
57
# File 'lib/nrser/types/enumerables.rb', line 52

def initialize  enumerable_type = ::Enumerable,
                entry_type = self.Top,
                **options
  super enumerable_type, **options
  @entry_type = Types.make entry_type
end

Instance Attribute Details

#entry_typeType (readonly)

Type all entries must satisfy.

Returns:



45
46
47
# File 'lib/nrser/types/enumerables.rb', line 45

def entry_type
  @entry_type
end

Instance Method Details

#enumerable_typeClass

Intuitive alias for IsA#mod.

Returns:

  • (Class)


77
# File 'lib/nrser/types/enumerables.rb', line 77

def enumerable_type; mod; end

#explainObject




66
67
68
# File 'lib/nrser/types/enumerables.rb', line 66

def explain
  "#{ enumerable_type.safe_name }<#{ entry_type.explain }>"
end

#test?(value) ⇒ Boolean

Returns:



80
81
82
83
84
85
86
# File 'lib/nrser/types/enumerables.rb', line 80

def test? value
  # Test that `value` is of the right container class first
  return false unless super( value )

  # If that passed test all the entries against the type
  value.all? &@entry_type.method( :test? )
end