Module: SequencescapeExcel::List

Extended by:
ActiveSupport::Concern
Includes:
Comparable, Enumerable
Included in:
ColumnList, ConditionalFormattingDefaultList, ConditionalFormattingList
Defined in:
app/sequencescape_excel/sequencescape_excel/list.rb

Overview

A list will create a struct of objects. Each attribute of the struct relates to an attribute of the object of a specific class. Each attribute will be a Hash This allows each object to be found by its various keys. Each item in the hash will relate to the same object. Any candidate which uses a List must respond to the valid? method. Each key must relate to a unique field. Example:

class Racket
  attr_reader :weight, :balance, :cost

  def initialize(weight, balance, cost)
   @weight, @height, @balance = weight, balance, cost
  end

  def valid?
   weight.present? && height.present? && cost.present?
  end
end

class RacketList
 include List
 list_for :racket, keys: [:weight, :balance]
end

racket_1 = Racket.new(130, “head heavy”, 75) racket_2 = Racket.new(120, “head light”, 85)

racket_list = RacketList.new racket_list.add racket_1 racket_list.add racket_2

racket_list.count => 2 racket_list.keys => [:weight, :balance] racket_list.weights => [“130”, “120”] racket_list.balances => [“head heavy”, “head light”] racket_list.find_by(:weight, “130”) => racket_1 racket_list.find_by(:balance, “head light”) => racket_2 racket_list.reset! racket_list.count => 0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object


163
164
165
166
167
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 163

def <=>(other)
  return unless other.is_a?(self.class)

  values <=> other.values
end

#add(item) ⇒ Object

Only add an item if it is valid add the item to the list of values add the item along with its attribute to each key


125
126
127
128
129
130
131
132
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 125

def add(item)
  return unless item.valid?

  values << item
  keys.each do |key|
    items.fetch(key).store(item.send(key).to_s, item)
  end
end

#add_copy(item) ⇒ Object

Uses dup It is up to the list item class to decide on the parameters for dup


137
138
139
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 137

def add_copy(item)
  add item.dup
end

#each(&block) ⇒ Object

relates to each value i.e. each object that is added.


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

def each(&block)
  values.each(&block)
end

#find(value) ⇒ Object


148
149
150
151
152
153
154
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 148

def find(value)
  keys.each do |key|
    item = find_by(key, value)
    return item if item.present?
  end
  nil
end

#find_by(key, value) ⇒ Object

Find by key and attribute. If it doesn't exist it won't blow up but will return nil


144
145
146
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 144

def find_by(key, value)
  items.fetch(key).fetch(value.to_s.squish, nil)
end

#initialize {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:


100
101
102
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 100

def initialize
  yield self if block_given?
end

#inspectObject


169
170
171
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 169

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

#itemsObject

## # If the items don't exist then create a new struct with each key being # an empty hash.


117
118
119
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 117

def items
  @items ||= create_list
end

#reset!Object

Empty the struct and start again. This is very destructive.


158
159
160
161
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 158

def reset!
  @values = []
  @items = create_list
end

#valuesObject


110
111
112
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 110

def values
  @values ||= []
end