Class: CSVPlusPlus::Modifier::Expand

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/csv_plus_plus/modifier/expand.rb

Overview

The logic for how a row can expand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repetitions: nil, starts_at: nil) ⇒ Expand

Returns a new instance of Expand.

Parameters:

  • repetitions (Integer, nil) (defaults to: nil)

    How many times this expand repeats. If it’s nil it will expand infinitely (for the rest of the worksheet.)

  • starts_at (Integer, nil) (defaults to: nil)

    The final location where the Expand will start. It’s important to note that this can’t be derived until all rows are expanded, because each expand modifier will push down the ones below it. So typically this param will not be passed in the initializer but instead set later.



29
30
31
32
33
# File 'lib/csv_plus_plus/modifier/expand.rb', line 29

def initialize(repetitions: nil, starts_at: nil)
  @repetitions = ::T.let(repetitions, ::T.nilable(::Integer))
  @starts_at = ::T.let(starts_at, ::T.nilable(::Integer)) unless starts_at.nil?
  @ends_at = ::T.let(nil, ::T.nilable(::Integer))
end

Instance Attribute Details

#ends_atInteger? (readonly)

Once the row has been expanded, where it ends at.

Returns:

  • (Integer, nil)

    the current value of ends_at



11
12
13
# File 'lib/csv_plus_plus/modifier/expand.rb', line 11

def ends_at
  @ends_at
end

#repetitionsInteger? (readonly)

How many times the row repeats/expands.

Returns:

  • (Integer, nil)

    the current value of repetitions



11
12
13
# File 'lib/csv_plus_plus/modifier/expand.rb', line 11

def repetitions
  @repetitions
end

#starts_atInteger?

Once the row has been expanded, where it starts at.

Returns:

  • (Integer, nil)

    the current value of starts_at



11
12
13
# File 'lib/csv_plus_plus/modifier/expand.rb', line 11

def starts_at
  @starts_at
end

Instance Method Details

#expanded?boolean

Has the row been expanded?

Returns:

  • (boolean)


39
40
41
# File 'lib/csv_plus_plus/modifier/expand.rb', line 39

def expanded?
  !@starts_at.nil?
end

#infinite?boolean

Does this infinitely expand?

Returns:

  • (boolean)


47
48
49
# File 'lib/csv_plus_plus/modifier/expand.rb', line 47

def infinite?
  repetitions.nil?
end

#position_within?(position) ⇒ boolean

Does the given position fall within this expand?

Parameters:

Returns:

  • (boolean)


66
67
68
69
70
71
72
73
74
75
# File 'lib/csv_plus_plus/modifier/expand.rb', line 66

def position_within?(position)
  unless starts_at
    raise(
      ::CSVPlusPlus::Error::CompilerError,
      'Must call Template.expand_rows! before checking the scope of expands.'
    )
  end

  position.row_index >= ::T.must(starts_at) && (ends_at.nil? || position.row_index <= ::T.must(ends_at))
end