Class: SpecComponent

Inherits:
Object show all
Defined in:
lib/flex_array/spec_component.rb

Overview

This helper class encapsulates a single component of the flex array spec.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range, stride) ⇒ SpecComponent

Create a limits component from its constituent data.
Parameters

  • range - the range of values for this index limit.

  • stride - the number of cells separating data with adjacent indexes.



14
15
16
# File 'lib/flex_array/spec_component.rb', line 14

def initialize(range, stride)
  @range, @stride = range, stride
end

Instance Attribute Details

#rangeObject (readonly)

The range of acceptable values for this limit.



4
5
6
# File 'lib/flex_array/spec_component.rb', line 4

def range
  @range
end

#strideObject (readonly)

The stride of this array dimension. That is, for each step in this dimension, how many steps are required in the low level array?



8
9
10
# File 'lib/flex_array/spec_component.rb', line 8

def stride
  @stride
end

Instance Method Details

#==(other) ⇒ Object

Limits are equal if their ranges are equal.
Returns

  • true if the spec components have equal ranges.



26
27
28
# File 'lib/flex_array/spec_component.rb', line 26

def ==(other)
  @range == other.range
end

#===(value) ⇒ Object

Forward ‘=== value’ to the range.



19
20
21
# File 'lib/flex_array/spec_component.rb', line 19

def ===(value)
  @range === value
end

#each(&block) ⇒ Object

Forward ‘each’ and the block to the range.



41
42
43
# File 'lib/flex_array/spec_component.rb', line 41

def each(&block)
  @range.each(&block)
end

#enlarge(growth) ⇒ Object

Enlarge the range of this spec by the growth term.



57
58
59
60
61
62
63
# File 'lib/flex_array/spec_component.rb', line 57

def enlarge(growth)
  if @range.none?
    @range = 0...growth
  else
    @range = (@range.min)..(@range.max + growth)
  end
end

#index_step(index) ⇒ Object

Compute the step required for the index value.
Returns

  • The number of array cells to be skipped for this index.



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

def index_step(index)
  (index - @range.min) * @stride
end

#maxObject

Forward ‘max’ to the range.



36
37
38
# File 'lib/flex_array/spec_component.rb', line 36

def max
  @range.max
end

#minObject

Forward ‘min’ to the range.



31
32
33
# File 'lib/flex_array/spec_component.rb', line 31

def min
  @range.min
end

#spanObject

Compute the span of indexes in this limit component.
Returns

  • The span of the range or zero if there is none.



48
49
50
51
52
53
54
# File 'lib/flex_array/spec_component.rb', line 48

def span
  if @range.none?
    0
  else
    @range.max - @range.min + 1
  end
end