Class: Pulo::FrameColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/pulo/frames/frame_column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent_frame, hidden, &formula) ⇒ FrameColumn

Returns a new instance of FrameColumn.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#column_classObject

Returns the value of attribute column_class.


6
7
8
# File 'lib/pulo/frames/frame_column.rb', line 6

def column_class
  @column_class
end

#column_unitObject

Returns the value of attribute column_unit.


6
7
8
# File 'lib/pulo/frames/frame_column.rb', line 6

def column_unit
  @column_unit
end

#formatterObject

Returns the value of attribute formatter.


6
7
8
# File 'lib/pulo/frames/frame_column.rb', line 6

def formatter
  @formatter
end

#formulaObject (readonly)

Returns the value of attribute formula.


6
7
8
# File 'lib/pulo/frames/frame_column.rb', line 6

def formula
  @formula
end

#nameObject

Returns the value of attribute name.


6
7
8
# File 'lib/pulo/frames/frame_column.rb', line 6

def name
  @name
end

#widthObject

Returns the value of attribute width.


7
8
9
# File 'lib/pulo/frames/frame_column.rb', line 7

def width
  @width
end

Instance Method Details

#[](index) ⇒ Object

Raises:

  • (IndexError)

94
95
96
97
# 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) ⇒ Object


60
61
62
# File 'lib/pulo/frames/frame_column.rb', line 60

def append_row(cell)
  @cells<<cell
end

#column_numberObject


30
31
32
# File 'lib/pulo/frames/frame_column.rb', line 30

def column_number
  @parent_frame.column_names[@name]
end

#descriptive_statisticsObject


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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

#hidden=(value) ⇒ Object


49
50
51
# File 'lib/pulo/frames/frame_column.rb', line 49

def hidden=value
  @hidden=value
end

#hidden?Boolean

Returns:

  • (Boolean)

46
47
48
# File 'lib/pulo/frames/frame_column.rb', line 46

def hidden?
  @hidden
end

#insert_row(row_no, cell) ⇒ Object


63
64
65
# File 'lib/pulo/frames/frame_column.rb', line 63

def insert_row(row_no,cell)
  @cells.insert(row_no,cell)
end

#inspectObject


125
126
127
# File 'lib/pulo/frames/frame_column.rb', line 125

def inspect
  "Frame Column Object"
end

#lookup(value) ⇒ Object


99
100
101
# 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) ⇒ Object


103
104
105
# File 'lib/pulo/frames/frame_column.rb', line 103

def map!(&block)
  @cells.each {|cell| cell.value=block.call(cell.value)}
end

#recalc(with_timing = false) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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_widthObject


115
116
117
118
119
120
# 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) ⇒ Object


24
25
26
27
28
# File 'lib/pulo/frames/frame_column.rb', line 24

def set_formula &formula
  @formula=formula
  #@recalc_required=true
  @value_column=false
end

#to_aObject


107
108
109
# File 'lib/pulo/frames/frame_column.rb', line 107

def to_a
  @cells.map {|cell| cell.value}
end

#to_sObject


122
123
124
# File 'lib/pulo/frames/frame_column.rb', line 122

def to_s
  "#{@name}: #{(@cells.map {|c| c.to_s}).join(', ')}"
end

#value_column?Boolean

Returns:

  • (Boolean)

111
112
113
# File 'lib/pulo/frames/frame_column.rb', line 111

def value_column?
  @value_column
end

#values=(vals) ⇒ Object

Raises:

  • (ArgumentError)

67
68
69
70
71
72
# 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