Class: NRSER::Types::ArrayOfType

Inherits:
ArrayType show all
Defined in:
lib/nrser/types/arrays.rb

Overview

Type for arrays where every entry satisfies a specific type.

Broken out from ArrayType so that TupleType can inherit from ArrayType and get share it’s string handling functionality without receiving the entry type stuff (which it handles differently).

Constant Summary

Constants inherited from ArrayType

NRSER::Types::ArrayType::DEFAULT_SPLIT_WITH

Instance Attribute Summary collapse

Attributes inherited from IsA

#mod

Display Instance Methods collapse

Instance Method Summary collapse

Methods inherited from ArrayType

#custom_from_s, #default_name, #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(item_type, **options) ⇒ ArrayOfType

Instantiate a new ‘ArrayOfType`.



128
129
130
131
# File 'lib/nrser/types/arrays.rb', line 128

def initialize item_type, **options
  super **options
  @item_type = NRSER::Types.make item_type
end

Instance Attribute Details

#item_typeNRSER::Types::Type (readonly)

Type that all items must satisfy for an array to be a member of this type.

Returns:



121
122
123
# File 'lib/nrser/types/arrays.rb', line 121

def item_type
  @item_type
end

Instance Method Details

#==(other) ⇒ Object

TODO:

I’m not even sure why this is implemented… was it used somewhere?

It doesn’t seems too well thought out… seems like the reality of comparing types is much more complicated?



178
179
180
181
182
# File 'lib/nrser/types/arrays.rb', line 178

def == other
  equal?(other) || (
    other.class == self.class && @item_type == other.item_type
  )
end

#explainObject




140
141
142
# File 'lib/nrser/types/arrays.rb', line 140

def explain
  "Array<#{ item_type.explain }>"
end

#has_from_s?Boolean

NRSER::Types::ArrayOfType can convert values from strings if it’s #item_type can convert values from strings.

Returns:



162
163
164
# File 'lib/nrser/types/arrays.rb', line 162

def has_from_s?
  @from_s || @item_type.has_from_s?
end

#items_from_strings(items) ⇒ Object



167
168
169
# File 'lib/nrser/types/arrays.rb', line 167

def items_from_strings items
  items.map &@item_type.method( :from_s )
end

#test?(value) ⇒ Boolean

Returns:



147
148
149
150
151
152
153
154
# File 'lib/nrser/types/arrays.rb', line 147

def test? value
  # Check the super method first, which will test if `value` is an Array
  # instance, and return `false` if it's not.
  return false unless super( value )
  
  # Otherwise test all the items
  value.all? &@item_type.method( :test? )
end