Class: StructurePrint
Instance Method Summary collapse
- #extract_printed_columns(indexes, values, index_value, add_index = false) ⇒ Object
- #get_indexes(level, struct) ⇒ Object
- #hash_element_array(hash) ⇒ Object
- #indexes_from_column_names(names, struct) ⇒ Object
-
#initialize(sep_char, header = true, format = nil, columns_to_print = nil) ⇒ StructurePrint
constructor
A new instance of StructurePrint.
- #istructure_print(struct, level, add_index = false) ⇒ Object
- #print_values(values) ⇒ Object
- #structure_print(struct, level) ⇒ Object
Methods included from ConfigReader
#config_dir, #config_file, #mkdir, #param, #user_file
Constructor Details
#initialize(sep_char, header = true, format = nil, columns_to_print = nil) ⇒ StructurePrint
Returns a new instance of StructurePrint.
199 200 201 202 203 204 205 206 |
# File 'lib/print.rb', line 199 def initialize(sep_char, header=true, format=nil, columns_to_print=nil) @sep_char = sep_char @header = header @format = format @rownum = 0 @columns_to_print = columns_to_print end |
Instance Method Details
#extract_printed_columns(indexes, values, index_value, add_index = false) ⇒ Object
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/print.rb', line 242 def extract_printed_columns(indexes, values, index_value, add_index = false) print_values = [] if add_index print_values << index_value.to_s end indexes.each { |index| print_values << values[index] } print_values end |
#get_indexes(level, struct) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/print.rb', line 221 def get_indexes(level, struct) result = (0...struct.keys.length).to_a if @columns_to_print if @columns_to_print == '*' return result end column_names = @columns_to_print.split(',') return indexes_from_column_names(column_names, struct) end print_config_file = config_file("print.cfg") if File.exists? print_config_file level.gsub!(/\[\d*\]/, '') level_map = JSON.parse(File.open(print_config_file).read) if level_map.has_key? level column_names = level_map[level] return indexes_from_column_names(column_names, struct) end end result end |
#hash_element_array(hash) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/print.rb', line 264 def hash_element_array(hash) values = [] hash.keys.each { |key| value = hash[key] if (value.nil?) value = "null" elsif (value.class == Array) value = "[#{value.length}]" elsif (value.class == String) elsif (value.class == Fixnum) value = value.to_s elsif (value.class == Hash) value = "..." elsif value.class == TrueClass value = "true" elsif value.class == FalseClass value = "false" else value = "<#{value.class}>" end values << value } values end |
#indexes_from_column_names(names, struct) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/print.rb', line 208 def indexes_from_column_names(names, struct) result = [ ] names.each { |name| index = struct.keys.index name if index.is_a? Integer result << index else raise "column (#{name}) not found" end } result end |
#istructure_print(struct, level, add_index = false) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/print.rb', line 293 def istructure_print(struct, level, add_index = false) if (struct.class == Array) struct.each_with_index { |elt, index| istructure_print(elt, level, (struct.length > 1) && @columns_to_print.nil?) } elsif (struct.class == Hash) # print top level elements of hash if @titles.nil? @titles = true @indexes = get_indexes(level, struct) titles = extract_printed_columns(@indexes, struct.keys, "item", add_index) if @table @table.set_titles(@sep_char, titles) elsif @format == :csv && @header puts titles.to_csv end end values = hash_element_array(struct) @rownum += 1 values_to_print = extract_printed_columns(@indexes, values, @rownum, add_index) if @table @table.append(values_to_print) elsif @format == :csv puts values_to_print.to_csv else puts values_to_print.join("|") end else puts struct.to_s end end |
#print_values(values) ⇒ Object
289 290 291 |
# File 'lib/print.rb', line 289 def print_values(values) puts values.join("|") end |
#structure_print(struct, level) ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/print.rb', line 253 def structure_print(struct, level) if @header && @format.nil? @table = TablePrint.new end if $debug > 0 puts "level = #{level}" end istructure_print(struct, level) @table.print_table if @table end |