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
- #<=>(other) ⇒ Object
-
#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.
-
#add_copy(item) ⇒ Object
Uses dup It is up to the list item class to decide on the parameters for dup.
-
#each ⇒ Object
relates to each value i.e.
- #find(value) ⇒ Object
-
#find_by(key, value) ⇒ Object
Find by key and attribute.
- #initialize {|_self| ... } ⇒ Object
- #inspect ⇒ Object
-
#items ⇒ Object
# If the items don’t exist then create a new struct with each key being # an empty hash.
-
#reset! ⇒ Object
Empty the struct and start again.
- #values ⇒ Object
Instance Method Details
#<=>(other) ⇒ Object
162 163 164 165 166 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 162 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
126 127 128 129 130 131 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 126 def add(item) return unless item.valid? values << item keys.each { |key| items.fetch(key).store(item.send(key).to_s, item) } end |
#add_copy(item) ⇒ Object
Uses dup It is up to the list item class to decide on the parameters for dup
136 137 138 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 136 def add_copy(item) add item.dup end |
#each ⇒ Object
relates to each value i.e. each object that is added.
107 108 109 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 107 def each(&) values.each(&) end |
#find(value) ⇒ Object
147 148 149 150 151 152 153 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 147 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
143 144 145 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 143 def find_by(key, value) items.dig(key, value.to_s.squish) end |
#initialize {|_self| ... } ⇒ Object
101 102 103 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 101 def initialize yield self if block_given? end |
#inspect ⇒ Object
168 169 170 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 168 def inspect "<#{self.class}: @keys=#{keys}, @values=#{values.inspect}>" end |
#items ⇒ Object
##
If the items don’t exist then create a new struct with each key being
an empty hash.
118 119 120 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 118 def items @items ||= create_list end |
#reset! ⇒ Object
Empty the struct and start again. This is very destructive.
157 158 159 160 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 157 def reset! @values = [] @items = create_list end |
#values ⇒ Object
111 112 113 |
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 111 def values @values ||= [] end |