class Pulo::FrameColumn

Attributes

column_class[R]
column_unit[R]
formatter[R]
formula[R]
name[R]
width[RW]

Public Class Methods

new(name,parent_frame,hidden,&formula) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 9
def initialize(name,parent_frame,hidden,&formula)
  @name=name
  @parent_frame=parent_frame
  @formula=formula
  @cells=[]
  #@recalc_required=block_given?
  @value_column=!block_given?
  @standard_formatter=lambda {|v| v.to_s }
  @formatter=@standard_formatter
  @hidden=hidden
  @width=3
  @column_class=NilClass
  @column_unit=NilClass
end

Public Instance Methods

[](index) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 94
def [](index)
  raise IndexError,"No row number #{index} defined." unless @cells[index]
  @cells[index]
end
append_row(cell) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 60
def append_row(cell)
  @cells<<cell
end
column_class=(klass) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 34
def column_class= (klass)
  @column_class=klass
  if @column_class.respond_to?(:quantity_name) and @formatter==@standard_formatter
    @formatter=lambda {|q| q.to_s(nil,true)}
  end
end
column_number() click to toggle source
# File lib/pulo/frames/frame_column.rb, line 30
def column_number
  @parent_frame.column_names[@name]
end
column_unit=(klass) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 40
def column_unit=(klass)
  @column_unit=klass
end
descriptive_statistics() click to toggle source
# File lib/pulo/frames/frame_column.rb, line 129
def descriptive_statistics
  vals=self.to_a
  if @column_class.respond_to?(:quantity_name)
    vals=vals.map{|val| val.send(@column_unit.name).value}
  end
  stats=vals.descriptive_statistics
  if @column_class.respond_to?(:quantity_name)
    stats.map{|val|

      if val[0]!=:number
        [val[0],@column_class.new(val[1],@column_unit)]
      else
        [val[0],val[1]]
      end

    }.to_h
  end
end
formatter=(lamb) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 43
def formatter= (lamb)
  @formatter=lamb
end
hidden=(value) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 49
def hidden=value
  @hidden=value
end
hidden?() click to toggle source
# File lib/pulo/frames/frame_column.rb, line 46
def hidden?
  @hidden
end
insert_row(row_no,cell) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 63
def insert_row(row_no,cell)
  @cells.insert(row_no,cell)
end
inspect() click to toggle source
# File lib/pulo/frames/frame_column.rb, line 125
def inspect
  "Frame Column Object"
end
lookup(value) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 99
def lookup(value)
  (@cells.find_all {|cell| cell.value==value}).map {|cell| cell.row}
end
map!(&block) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 103
def map!(&block)
  @cells.each {|cell| cell.value=block.call(cell.value)}
end
name=(new_name) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 52
def name= new_name
  if @parent_frame.column_names[new_name]
    @name=new_name
  else
    @parent_frame.rename_column @name,new_name
  end
end
recalc(with_timing=false) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 74
def recalc with_timing=false
  t_start=Time.now

  @column_class=NilClass
  @parent_frame.rows.each do |row|
    begin
      row[column_number].value=@formula.call(row)
      row[column_number].unset_error
    rescue Exception => e
      #raise "Exception '#{e}' occured calculating column: #{@name} row: #{row.row_number}"
      warn "Warning! Exception '#{e}' occured calculating column: #{@name} row: #{row.row_number}"
      row[column_number].set_error
    end
  end

  if with_timing
    puts "Recalc column '#{@name}' in: #{((Time.now-t_start)*1000).to_i} ms."
  end
end
recalc_width() click to toggle source
# File lib/pulo/frames/frame_column.rb, line 115
def recalc_width
  @width=@cells.take(30).map {|c| c.to_s.length}.max
  @width||=0
  @width=[@width,@name.length].max
  @width=[@width,column_class.to_s.length+1].max
end
set_formula(&formula) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 24
def set_formula &formula
  @formula=formula
  #@recalc_required=true
  @value_column=false
end
to_a() click to toggle source
# File lib/pulo/frames/frame_column.rb, line 107
def to_a
  @cells.map {|cell| cell.value}
end
to_s() click to toggle source
# File lib/pulo/frames/frame_column.rb, line 122
def to_s
  "#{@name}: #{(@cells.map {|c| c.to_s}).join(', ')}"
end
value_column?() click to toggle source
# File lib/pulo/frames/frame_column.rb, line 111
def value_column?
  @value_column
end
values=(vals) click to toggle source
# File lib/pulo/frames/frame_column.rb, line 67
def values=(vals)
  raise ArgumentError,"Wrong number of values given for column - need an array of #{@cells.count}" unless vals.count==@cells.count
  vals.each_with_index do |val,index|
    @cells[index].value=val
  end
end