Class: NRSER::Types::ArrayType

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

Overview

TODO:

Just call this Array?!

Combine with arrays of a type?!

Note:

Construct ArrayType types using the Array factory.

Arrays!

Direct Known Subclasses

ArrayOfType, Tuple

Constant Summary collapse

DEFAULT_SPLIT_WITH =

Default value to split strings with in Type#from_s if the string provided does is not recognized as an encoding format (as of writing, JSON is the only format we attempt to detect).

Splits

/\,\s*/m

Instance Attribute Summary

Attributes inherited from IsA

#mod

Display Instance Methods collapse

Instance Method Summary collapse

Methods inherited from IsA

#==, #custom_from_data, #explain, #has_from_data?, #init_from_data?, #test?

Methods inherited from Type

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

Constructor Details

#initialize(split_with: DEFAULT_SPLIT_WITH, **options) ⇒ ArrayType

Returns a new instance of ArrayType.



42
43
44
45
# File 'lib/nrser/types/arrays.rb', line 42

def initialize split_with: DEFAULT_SPLIT_WITH, **options
  super ::Array, **options
  @split_with = split_with
end

Instance Method Details

#custom_from_s(string) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nrser/types/arrays.rb', line 86

def custom_from_s string
  # Does it looks like a JSON array?
  if NRSER.looks_like_json_array? string
    # It does! Load it
    begin
      return JSON.load( string )
    rescue
      # pass - if we failed to load as JSON, it may just not be JSON, and
      # we can try the split approach below.
    end
  end
  
  # Split it with the splitter and check that
  items_from_strings( string.split( @split_with ) )
end

#default_nameObject




54
55
56
57
58
59
60
# File 'lib/nrser/types/arrays.rb', line 54

def default_name
  if item_type == NRSER::Types.Top
    'Array'
  else
    "Array<#{ item_type.name }>"
  end
end

#default_symbolicObject



63
64
65
# File 'lib/nrser/types/arrays.rb', line 63

def default_symbolic
  "[#{ item_type.symbolic }]"
end

#item_typeObject



48
# File 'lib/nrser/types/arrays.rb', line 48

def item_type; NRSER::Types.Top; end

#items_from_strings(items) ⇒ Array

Called on an array of string items that have been split from a single string by Type#from_s to convert each individual item before Type#check is called on the value.

NRSER::Types::ArrayType implementation is a no-op that just returns ‘items` - this method is in place for subclasses to override.

Parameters:

Returns:



81
82
83
# File 'lib/nrser/types/arrays.rb', line 81

def items_from_strings items
  items
end