Class: NRSER::Types::ArrayType

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

Direct Known Subclasses

ArrayOfType, TupleType

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

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?, #test, #test?, #to_data, #to_s, #union, #xor

Constructor Details

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

Returns a new instance of ArrayType.



26
27
28
29
# File 'lib/nrser/types/arrays.rb', line 26

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

Instance Method Details

#custom_from_s(string) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nrser/types/arrays.rb', line 51

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

#item_typeObject



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

def item_type; NRSER::Types.any; 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:



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

def items_from_strings items
  items
end