Class: BioTable::LazyValues

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bio-table/filter.rb

Overview

LazyValues fetches values on demand from the @fields array. In the [] method a field is transformed into a float when it is called.

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ LazyValues

Returns a new instance of LazyValues.



10
11
12
13
# File 'lib/bio-table/filter.rb', line 10

def initialize fields
  @fields = fields
  @values = []  # cache values
end

Instance Method Details

#[](index) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/bio-table/filter.rb', line 15

def [] index
  if not @values[index]
    field = @fields[index]
    @values[index] = (Filter::valid_number?(field) ? field.to_f : nil )
  end
  @values[index].freeze
  @values[index]
end

#compactObject



34
35
36
37
38
39
40
# File 'lib/bio-table/filter.rb', line 34

def compact
  a = []
  each do | e |
    a << e if e != nil
  end
  a
end

#each(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/bio-table/filter.rb', line 24

def each &block
  @fields.each_with_index do |field,i|
    if block_given?
      block.call self[i]
    else
      yield self[i]
    end
  end
end

#sizeObject



42
43
44
# File 'lib/bio-table/filter.rb', line 42

def size
  @fields.size
end