Class: NRSER::Types::Array

Inherits:
IsA
  • Object
show all
Defined in:
lib/nrser/types/array.rb

Constant Summary collapse

SEP =
/\,\s+/

Instance Attribute Summary collapse

Attributes inherited from IsA

#klass

Instance Method Summary collapse

Methods inherited from IsA

#name

Methods inherited from Type

#check, #name, #respond_to?, short_name, #to_s

Constructor Details

#initialize(item_type = NRSER::Types.any, **options) ⇒ Array

Returns a new instance of Array.



13
14
15
16
# File 'lib/nrser/types/array.rb', line 13

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

Instance Attribute Details

#item_typeObject (readonly)

Returns the value of attribute item_type.



11
12
13
# File 'lib/nrser/types/array.rb', line 11

def item_type
  @item_type
end

Instance Method Details

#==(other) ⇒ Object



46
47
48
49
50
# File 'lib/nrser/types/array.rb', line 46

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

#default_nameObject



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

def default_name
  "#{ self.class.short_name }<#{ @item_type }>"
end

#from_s(s) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nrser/types/array.rb', line 34

def from_s s
  # does it looks like json?
  if s.start_with? '['
    begin
      return JSON.load s
    rescue
    end
  end
  
  s.split SEP
end

#has_from_s?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/nrser/types/array.rb', line 30

def has_from_s?
  @item_class.has_from_s?
end

#test(value) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/nrser/types/array.rb', line 18

def test value
  super(value) && if @item_type == NRSER::Types.any
    true
  else
    value.all? {|v| @item_type.test v}
  end
end