Class: SequencescapeExcel::Range

Inherits:
Object
  • Object
show all
Includes:
Helpers::Attributes
Defined in:
app/sequencescape_excel/sequencescape_excel/range.rb

Overview

A range of cells signified by a reference. The options are a range of text values which are used to validate a value. The first row is the only mandatory field everything else can be inferred. Each field that is not passed in the initializer is lazy loaded.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Attributes

#<=>, #to_a

Constructor Details

#initialize(attributes = {}) ⇒ Range

If the range is valid i.e. has a first row then a first cell and last cell are created these are used for references.


32
33
34
35
36
37
38
39
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 32

def initialize(attributes = {})
  super(default_attributes.merge(attributes))

  return unless valid?

  @first_cell = Cell.new(first_row, first_column)
  @last_cell = Cell.new(last_row, last_column) unless dynamic?
end

Instance Attribute Details

#first_cellObject (readonly)

Returns the value of attribute first_cell.


27
28
29
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 27

def first_cell
  @first_cell
end

Instance Method Details

#absolute_referenceObject

An absolute reference is defined as a reference preceded by the name of the worksheet to find a reference that is not in the current worksheet e.g. Sheet1!A1:A100 If the worksheet name is not present just returns the reference.


105
106
107
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 105

def absolute_reference
  worksheet_name.present? ? "#{worksheet_name}!#{fixed_reference}" : fixed_reference.to_s
end

#dynamic?Boolean

A dynamic rage uses a se of options that are calculated at runtime. Such as a SequencescapeExcel::DynamicOption Arrays are assumed to be static

Returns:

  • (Boolean)

126
127
128
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 126

def dynamic?
  @identifier.present?
end

#first_cell_referenceObject

rubocop:disable Rails/Delegate Would change this to: delegate :reference, to: :first_cell, prefix: true

The reference of the first cell.


95
96
97
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 95

def first_cell_reference
  first_cell.reference
end

#fixed_referenceObject


86
87
88
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 86

def fixed_reference
  "#{first_cell.fixed}:#{last_cell.fixed}"
end

#last_cellObject

Returns either the cached last cell, or a dynamically created one. We don't memoize this, as we dymanically recalculate the value at runtime for some ranges. For static ranges the last_cell is calculated in the initializer so will be available. Also we can't just do @last_cell || Cell.new as @last_cell can be legitimately falsey


58
59
60
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 58

def last_cell
  dynamic? ? Cell.new(last_row, last_column) : @last_cell
end

#last_columnObject

If not defined and options are empty is set to first column. If not defined and there are options is set to first column plus the the number of options minus one.


44
45
46
47
48
49
50
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 44

def last_column
  @last_column || if dynamic?
    calculate_last_column
  else
    @last_column = calculate_last_column
  end
end

#last_rowObject

If not defined is set to the first row


64
65
66
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 64

def last_row
  @last_row ||= first_row
end

#optionsObject

If not defined is set to an empty hash.


69
70
71
72
73
74
75
76
77
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 69

def options
  if static?
    @options
  elsif dynamic?
    create_dynamic_options
  else
    {}
  end
end

#referenceObject

The reference for a range is a valid Excel reference e.g. $A$1:$H$10 Defined by the fixed reference of the first cell and the fixed reference of the last cell.


82
83
84
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 82

def reference
  "#{first_cell.reference}:#{last_cell.reference}"
end

#referencesObject

Return a list of references which are generally used together in other classes of the module.


137
138
139
140
141
142
143
144
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 137

def references
  {
    first_cell_reference: first_cell_reference,
    reference: reference,
    fixed_reference: fixed_reference,
    absolute_reference: absolute_reference
  }
end

#set_worksheet_name(worksheet_name) ⇒ Object

Set the worksheet name and return the range


111
112
113
114
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 111

def set_worksheet_name(worksheet_name) # rubocop:disable Naming/AccessorMethodName
  self.worksheet_name = worksheet_name
  self
end

#static?Boolean

Returns:

  • (Boolean)

130
131
132
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 130

def static?
  @options.present?
end

#valid?Boolean

A range is only valid if the first row is present.

Returns:

  • (Boolean)

118
119
120
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 118

def valid?
  first_row.present?
end