Class: Structure::Type::Array

Inherits:
Single
  • Object
show all
Defined in:
lib/structure.rb

Direct Known Subclasses

ArrayWithStruct

Instance Attribute Summary

Attributes inherited from Single

#classes

Instance Method Summary collapse

Methods inherited from Single

#class?

Constructor Details

#initialize(classes) ⇒ Array

Returns a new instance of Array.



41
42
43
44
45
# File 'lib/structure.rb', line 41

def initialize(classes)
  super(classes)
  @max = 999_999
  @min = 0
end

Instance Method Details

#at_least(number) ⇒ Object



53
54
55
# File 'lib/structure.rb', line 53

def at_least(number)
  between(number, Float::INFINITY)
end

#between(min, max) ⇒ Object



47
48
49
50
51
# File 'lib/structure.rb', line 47

def between(min, max)
  @min = min
  @max = max
  self
end

#elementsObject



62
63
64
65
66
# File 'lib/structure.rb', line 62

def elements
  @min = @number
  @max = @number
  self
end

#elements_at_leastObject



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

def elements_at_least
  raise "Wrong use of at_least" unless @number

  @min = @number
  @number = nil
  self
end

#elements_at_mostObject



72
73
74
75
76
77
78
# File 'lib/structure.rb', line 72

def elements_at_most
  raise "Wrong use of at_most" unless @number

  @max = @number
  @number = nil
  self
end

#inspectObject



99
100
101
102
103
104
105
# File 'lib/structure.rb', line 99

def inspect
  if class?
    "a_list_of(#{@classes.inspect})"
  else
    "a_list_of(\n#{@classes.pretty_inspect})"
  end
end

#itemsObject



68
69
70
# File 'lib/structure.rb', line 68

def items
  elements
end

#matches?(json) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
# File 'lib/structure.rb', line 88

def matches?(json)
  unless json.size.between?(
    @min, @max
  )
    raise SizeError,
          "Size Error: #{inspect} size (#{json.size}) is not between #{@min} and #{@max}."
  end

  json.all? { |j| classes.any? { |s| yield s, j } }
end

#with(number) ⇒ Object



57
58
59
60
# File 'lib/structure.rb', line 57

def with(number)
  @number = number
  self
end