Class: CSVPlusPlus::Entities::Reference

Inherits:
Entity
  • Object
show all
Extended by:
T::Sig
Includes:
HasIdentifier
Defined in:
lib/csv_plus_plus/entities/reference.rb

Overview

A reference to something - this is typically either a cell reference (handled by A1Reference) or a reference to a variable.

One sticking point with the design of this class is that we don’t know if a reference is a variable reference unless we look at currently defined variables and see if there is one by that name. Since all cell references can be a valid variable name, we can’t be sure which is which. So we delegate that decision as late as possible

  • in #evaluate

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasIdentifier

#identifier

Constructor Details

#initialize(ref: nil, a1_ref: nil) ⇒ Reference

Either ref, cell_index or row_index must be specified.

Parameters:

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

    An A1-style cell reference (that will be parsed into it’s row/cell indexes).

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

    An A1-style cell reference (that will be parsed into it’s row/cell indexes).

Raises:

  • (::ArgumentError)


31
32
33
34
35
36
37
38
# File 'lib/csv_plus_plus/entities/reference.rb', line 31

def initialize(ref: nil, a1_ref: nil)
  super()

  raise(::ArgumentError, 'Must specify :ref or :a1_ref') unless ref || a1_ref

  @ref = ref
  @a1_ref = a1_ref
end

Instance Attribute Details

#refObject (readonly)

Returns the value of attribute ref.



21
22
23
# File 'lib/csv_plus_plus/entities/reference.rb', line 21

def ref
  @ref
end

#scoped_to_expandExpand? (readonly)

If set, the expand in which this variable is scoped to. It cannot be resolved outside of the given expand.

Returns:

  • (Expand, nil)

    the current value of scoped_to_expand



16
17
18
# File 'lib/csv_plus_plus/entities/reference.rb', line 16

def scoped_to_expand
  @scoped_to_expand
end

Instance Method Details

#==(other) ⇒ boolean

Parameters:

  • other (BasicObject)

Returns:

  • (boolean)


44
45
46
47
48
49
50
51
# File 'lib/csv_plus_plus/entities/reference.rb', line 44

def ==(other)
  case other
  when self.class
    a1_ref == other.a1_ref
  else
    false
  end
end

#a1_refA1Reference

Returns:



55
56
57
# File 'lib/csv_plus_plus/entities/reference.rb', line 55

def a1_ref
  @a1_ref ||= ::CSVPlusPlus::A1Reference.new(ref: ::T.must(ref))
end

#evaluate(position) ⇒ ::String

Get the A1-style cell reference

Parameters:

  • position (Position)

    The current position

Returns:

  • (::String)

    An A1-style reference



65
66
67
68
# File 'lib/csv_plus_plus/entities/reference.rb', line 65

def evaluate(position)
  # TODO: ugh make to_a1_ref not return a nil
  ref || a1_ref.to_a1_ref(position) || ''
end

#idSymbol

Returns:

  • (Symbol)


72
73
74
# File 'lib/csv_plus_plus/entities/reference.rb', line 72

def id
  ref && identifier(::T.must(ref).to_sym)
end