1919# for convinient Ruby script writing for KNIME
2020# See also https://tech.knime.org/javadoc-api
2121module Knime
22+
23+ private # hide methods from subclasses of Object class
24+
2225 # This module defines a container for columns for using it in
2326 # style Cell.new.StringCell('str').IntegerCell(123).DoubleCell()...
2427 module CellUtility
@@ -86,6 +89,37 @@ def to_l
8689 end
8790 end
8891
92+ # module for extending DataRow implementation classes.
93+ module CellAccessor
94+ # Get cells by index in Ruby style
95+ def []( idx )
96+ if idx >= 0
97+ getCell ( idx )
98+ else
99+ getCell ( getNumCells + idx ) # -1 - last element
100+ end
101+ end
102+
103+ # Generate dynamic methods for accessing to cells by column name
104+ # from BlobSupportDataRow.
105+ # All names are translated in low case. All symbols except :word: are
106+ # changed to underline symbol.
107+ # For input 0 only is generated simple names. For all inputs methods has
108+ # a following format: i#{input_num}_translated_column_name
109+ #
110+ $num_inputs ||= 0
111+ ( 0 ...$num_inputs) . each do |i |
112+ table = $input_datatable_arr[ i ]
113+ col_names = table . getDataTableSpec . getColumnNames . map do |str |
114+ str . downcase . gsub ( /[^[[:word:]]]/ , '_' ) . gsub ( /\_ +/ , '_' ) . chomp ( '_' )
115+ end
116+ col_names . each_with_index do |name , num |
117+ define_method ( name ) { getCell ( num ) } if num == 0
118+ define_method ( "i#{ num } _#{ name } " ) { getCell ( num ) }
119+ end
120+ end
121+ end
122+
89123 def snippet_runner
90124 count , step = $inData0. length , 0x2FF
91125 coef = step / count . to_f
@@ -105,6 +139,7 @@ def snippet_runner
105139# Extended knime class
106140class BlobSupportDataRow
107141 include CellUtility
142+ include CellAccessor
108143
109144 # Append new columns by previously added chain of cells
110145 def append
@@ -115,15 +150,6 @@ def append
115150 def <<( cells )
116151 AppendedColumnRow . new ( self , *cells . cells )
117152 end
118-
119- # Get cells by index in Ruby style
120- def []( idx )
121- if idx >= 0
122- getCell ( idx )
123- else
124- getCell ( getNumCells + idx ) # -1 - last element
125- end
126- end
127153end
128154
129155# Extended knime class
0 commit comments