Class: SequencescapeExcel::ColumnList

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, List
Defined in:
app/sequencescape_excel/sequencescape_excel/column_list.rb

Overview

A collection of columns

Instance Method Summary collapse

Methods included from List

#<=>, #add, #add_copy, #each, #find, #find_by, #items, #reset!, #values

Constructor Details

#initialize(columns = {}, conditional_formattings = {}) {|_self| ... } ⇒ ColumnList

To create a column_list a hash with details of all columns is required. Each key is a column name, each value is options for the column including heading, validation, conditional formatting rules, etc. for each column a new Column object is created. Each conditional formatting for the column is combined with its conditional formatting in the list. If the column is not valid an error is returned.

Yields:

  • (_self)

Yield Parameters:


22
23
24
25
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 22

def initialize(columns = {}, conditional_formattings = {})
  create_columns(columns || {}, conditional_formattings)
  yield self if block_given?
end

Instance Method Details

#add_with_number(column, column_list = nil) ⇒ Object


64
65
66
67
68
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 64

def add_with_number(column, column_list = nil)
  column.number = (column_list || self).next_number
  add column
  self
end

#bad_keysObject


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

def bad_keys
  @bad_keys ||= []
end

#column_values(replacements = {}) ⇒ Object


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

def column_values(replacements = {})
  replacements.each do |k, v|
    find(k).value = v
  end
  columns.collect(&:value)
end

#except(key) ⇒ Object


50
51
52
53
54
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 50

def except(key)
  keys = names.dup
  keys.delete(key.to_s)
  extract(keys)
end

#extract(keys) ⇒ Object

Extracts columns from a column list based on names (a list of columns names). Returns a new column list that consists only of the columns named in names.


37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 37

def extract(keys)
  ColumnList.new do |column_list|
    keys.each do |key|
      column = find(key)
      if column.present?
        column_list.add_with_number(column.dup, column_list)
      else
        column_list.bad_keys << key
      end
    end
  end
end

#find_column_or_null(key, value) ⇒ Object


89
90
91
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 89

def find_column_or_null(key, value)
  find_by(key, value) || SequencescapeExcel::NullObjects::NullColumn.new
end

#initialize_dup(source) ⇒ Object


83
84
85
86
87
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 83

def initialize_dup(source)
  reset!
  create_columns(source.columns, nil)
  super
end

#inspectObject


101
102
103
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 101

def inspect
  "<#{self.class}: @columns:#{columns.map(&:name).inspect}...>"
end

#next_numberObject

Returns a number of a next column based on the number of columns that already exist in the list


73
74
75
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 73

def next_number
  columns.count + 1
end

#update(first_row, last_row, ranges, worksheet) ⇒ Object

A forwarding method - Update each column in the list of columns.


79
80
81
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 79

def update(first_row, last_row, ranges, worksheet)
  each { |column| column.update(first_row, last_row, ranges, worksheet) }
end

#with(key) ⇒ Object


56
57
58
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 56

def with(key)
  add_with_number(Column.new(name: key, heading: key.to_s))
end

#with_metadata_fieldsObject


97
98
99
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 97

def 
  select(&:metadata_field?)
end

#with_specialised_fieldsObject


93
94
95
# File 'app/sequencescape_excel/sequencescape_excel/column_list.rb', line 93

def with_specialised_fields
  select(&:specialised_field?)
end