Class: SequencescapeExcel::Range
- Inherits:
-
Object
- Object
- SequencescapeExcel::Range
- 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
-
#first_cell ⇒ Object
readonly
Returns the value of attribute first_cell.
Instance Method Summary collapse
-
#absolute_reference ⇒ Object
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.
-
#dynamic? ⇒ Boolean
A dynamic rage uses a se of options that are calculated at runtime.
-
#first_cell_reference ⇒ Object
rubocop:disable Rails/Delegate Would change this to: delegate :reference, to: :first_cell, prefix: true.
- #fixed_reference ⇒ Object
-
#initialize(attributes = {}) ⇒ Range
constructor
If the range is valid i.e.
-
#last_cell ⇒ Object
Returns either the cached last cell, or a dynamically created one.
-
#last_column ⇒ Object
If not defined and options are empty is set to first column.
-
#last_row ⇒ Object
If not defined is set to the first row.
-
#options ⇒ Object
If not defined is set to an empty hash.
-
#reference ⇒ Object
The reference for a range is a valid Excel reference e.g.
-
#references ⇒ Object
Return a list of references which are generally used together in other classes of the module.
-
#set_worksheet_name(worksheet_name) ⇒ Object
Set the worksheet name and return the range.
- #static? ⇒ Boolean
-
#valid? ⇒ Boolean
A range is only valid if the first row is present.
Methods included from Helpers::Attributes
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.
22 23 24 25 26 27 28 29 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 22 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_cell ⇒ Object (readonly)
Returns the value of attribute first_cell
17 18 19 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 17 def first_cell @first_cell end |
Instance Method Details
#absolute_reference ⇒ Object
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.
94 95 96 97 98 99 100 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 94 def absolute_reference if worksheet_name.present? "#{worksheet_name}!#{fixed_reference}" else fixed_reference.to_s end 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
119 120 121 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 119 def dynamic? @identifier.present? end |
#first_cell_reference ⇒ Object
rubocop:disable Rails/Delegate Would change this to: delegate :reference, to: :first_cell, prefix: true
The reference of the first cell.
85 86 87 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 85 def first_cell_reference first_cell.reference end |
#fixed_reference ⇒ Object
76 77 78 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 76 def fixed_reference "#{first_cell.fixed}:#{last_cell.fixed}" end |
#last_cell ⇒ Object
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
48 49 50 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 48 def last_cell dynamic? ? Cell.new(last_row, last_column) : @last_cell end |
#last_column ⇒ Object
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.
34 35 36 37 38 39 40 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 34 def last_column @last_column || if dynamic? calculate_last_column else @last_column = calculate_last_column end end |
#last_row ⇒ Object
If not defined is set to the first row
54 55 56 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 54 def last_row @last_row ||= first_row end |
#options ⇒ Object
If not defined is set to an empty hash.
59 60 61 62 63 64 65 66 67 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 59 def if static? @options elsif dynamic? else {} end end |
#reference ⇒ Object
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.
72 73 74 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 72 def reference "#{first_cell.reference}:#{last_cell.reference}" end |
#references ⇒ Object
Return a list of references which are generally used together in other classes of the module.
130 131 132 133 134 135 136 137 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 130 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
104 105 106 107 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 104 def set_worksheet_name(worksheet_name) # rubocop:disable Naming/AccessorMethodName self.worksheet_name = worksheet_name self end |
#static? ⇒ Boolean
123 124 125 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 123 def static? @options.present? end |
#valid? ⇒ Boolean
A range is only valid if the first row is present.
111 112 113 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 111 def valid? first_row.present? end |