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.
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_cell ⇒ Object (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_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.
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
127 128 129 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 127 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.
96 97 98 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 96 def first_cell_reference first_cell.reference end |
#fixed_reference ⇒ Object
87 88 89 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 87 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
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_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.
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_row ⇒ Object
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 |
#options ⇒ Object
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 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.
83 84 85 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 83 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.
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
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.
119 120 121 |
# File 'app/sequencescape_excel/sequencescape_excel/range.rb', line 119 def valid? first_row.present? end |