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.



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

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)


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

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.



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

def first_cell_reference
  first_cell.reference
end

#fixed_referenceObject



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

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



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

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
51
# 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



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

def last_row
  @last_row ||= first_row
end

#optionsObject

If not defined is set to an empty hash.



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

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.



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

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.



138
139
140
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 138

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

#set_worksheet_name(worksheet_name) ⇒ Object

Set the worksheet name and return the range



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

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

#static?Boolean

Returns:

  • (Boolean)


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

def static?
  @options.present?
end

#valid?Boolean

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

Returns:

  • (Boolean)


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

def valid?
  first_row.present?
end