Class: StructurePrint

Inherits:
Object
  • Object
show all
Includes:
ConfigReader
Defined in:
lib/print.rb

Instance Method Summary collapse

Methods included from ConfigReader

#config_dir, #config_file, #imkdir, #mk_config_dir, #mk_user_dir, #param, #user_file

Constructor Details

#initialize(sep_char) ⇒ StructurePrint

Returns a new instance of StructurePrint.



191
192
193
# File 'lib/print.rb', line 191

def initialize(sep_char)
  @sep_char = sep_char
end

Instance Method Details

#extract_printed_columns(indexes, values, index_value, add_index = false) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/print.rb', line 213

def extract_printed_columns(indexes, values, index_value, add_index = false)
  print_values = []
  print_values << index_value.to_s if add_index
  indexes.each { |index|
    print_values << values[index]
  }
  print_values
end

#get_indexes(level, key_array) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/print.rb', line 195

def get_indexes(level, key_array)
  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]
      result = []
      column_names.each { |name|
        index = key_array.index name
        result << index
      }
      return result
    end
  end
  (0...key_array.length).to_a
end

#istructure_print(struct, level, header, add_index = false) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/print.rb', line 230

def istructure_print(struct, level, header, add_index = false)
  if (struct.class == Array)
    struct.each_with_index { |elt, index|
      istructure_print(elt, level, header, struct.length > 1)
      header = false
    }
  elsif (struct.class == Hash)
    # print top level elements of hash
    indexes = get_indexes(level, struct.keys)
    if @table.nil?
      @table = TablePrint.new(@sep_char, *extract_printed_columns(indexes, struct.keys, "item", add_index))
      @rownum = 0
    end
    values = []
    struct.keys.each { |key|
      value = struct[key]
      if (value.nil?)
        value = "null"
      elsif (value.class == Array)
        value = "[" + value.length.to_s + "]"
      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.to_s + ">"
      end
      values << value
    }
    @rownum += 1
    @table.append(*extract_printed_columns(indexes, values, @rownum, add_index))
  else
    puts struct.to_s
  end
end

#structure_print(struct, level, header = true) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/print.rb', line 222

def structure_print(struct, level, header = true)
  istructure_print(struct, level, header)
  if $debug
    puts "level = #{level}"
  end
  @table.print_table(header) if @table
end