Class: SequencescapeExcel::Validation

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

Overview

An Excel validation Holds the validation for each column which is added to each column when the spreadsheet is created. Consists of: - A list of options which relate to options recognised by Excel e.g. errorMessage. - A range name (optional) which will be linked to a range when the spreadsheet is created.

Instance Method Summary collapse

Methods included from Helpers::Attributes

#<=>, #to_a

Constructor Details

#initialize(attributes = {}) ⇒ Validation

Returns a new instance of Validation.



15
16
17
# File 'app/sequencescape_excel/sequencescape_excel/validation.rb', line 15

def initialize(attributes = {})
  super
end

Instance Method Details

#empty?Boolean

A validation object is never empty

Returns:

  • (Boolean)


48
49
50
# File 'app/sequencescape_excel/sequencescape_excel/validation.rb', line 48

def empty?
  false
end

#formula1Object

formula1 is defined within the options, however it needs to be updated with: 1) A provided range in the case of lists 2) Proper cell names in the case of custom formulas 3) AXLSX doesn’t escape text fields for us, so we do that ourselves



74
75
76
77
78
79
# File 'app/sequencescape_excel/sequencescape_excel/validation.rb', line 74

def formula1
  return @range.absolute_reference if range_required?
  return if options[:formula1].nil?

  options[:formula1].gsub('A1', reference_start).encode(xml: :text)
end

#initialize_dup(source) ⇒ Object



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

def initialize_dup(source)
  self.options = source.options.dup
  super
end

#inspectObject



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

def inspect
  "<#{self.class}: @options=#{options}, @range_name=#{range_name}>"
end

#range_required?Boolean

If the range name is present then a range is required for the validation

Returns:

  • (Boolean)


36
37
38
# File 'app/sequencescape_excel/sequencescape_excel/validation.rb', line 36

def range_required?
  range_name.present?
end

#saved?Boolean

If the worksheet has been updated then we can assume that the validation has been saved to a worksheet.

Returns:

  • (Boolean)


55
56
57
# File 'app/sequencescape_excel/sequencescape_excel/validation.rb', line 55

def saved?
  @worksheet_validation.present?
end

#update(attributes = {}) ⇒ Object

The range is updated when the measurements of a worksheet is defined. If a range is required the the formula1 is set to the absolute raference of the range. If a worksheet is passed then the data validation is added using the reference is passed and the options for the validation.



25
26
27
28
29
30
31
32
# File 'app/sequencescape_excel/sequencescape_excel/validation.rb', line 25

def update(attributes = {})
  return if attributes[:worksheet].blank?

  @reference = attributes[:reference]
  @range = attributes[:range]

  @worksheet_validation = attributes[:worksheet].add_data_validation(attributes[:reference], **sanitized_options)
end

#valid?Boolean

Validation is only valid if there are some options

Returns:

  • (Boolean)


42
43
44
# File 'app/sequencescape_excel/sequencescape_excel/validation.rb', line 42

def valid?
  options.present?
end