Class: Array

Inherits:
Object show all
Defined in:
lib/ectoplasm.rb

Instance Method Summary collapse

Instance Method Details

#obfuscate!(secure_keys = DEFAULT_SECURE_KEYS) ⇒ Object



151
152
153
# File 'lib/ectoplasm.rb', line 151

def obfuscate! secure_keys = DEFAULT_SECURE_KEYS
  self.map { |x| x.obfuscate!(secure_keys) }
end

#pretty(width: 25) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ectoplasm.rb', line 94

def pretty width: 25
  return 'empty'.grey if self.empty?
  return self.first.pretty if self.count == 1 and not self.first.is_a? Hash

  list_length = self.map { |x| x.to_s.length }.reduce(:+)
  return self.join ', ' if list_length && list_length < 30

  self
    .select { |x| x != nil && x != '' }
    .map do |x|
      ' - ' + x.pretty(width: width-3).strip.gsub(/\n/, "\n   ")
    end
    .join "\n"
end

#table(header: nil, mappings: {}, with_index: false, limit: 50) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ectoplasm.rb', line 109

def table header: nil, mappings: {}, with_index: false, limit: 50
  header = self[0].keys if header == nil
  heading = header.is_a?(Array) ? header : self[0].keys

  table_data = self.slice(0, limit).map do |row|
    heading.map do |key|
      mappings.key?(key) ? mappings[key][row, row[key]] : row[key]
    end
  end

  table_data.insert(0, heading) if header != false

  data_sizes = table_data.map do |row|
    row.map { |data| data.to_s.length }
  end

  column_sizes = data_sizes[0]
    .zip(*data_sizes[1..-1])
    .map { |row| row.max }

  table = table_data.map { |row| column_sizes.zip row }

  table_str = ''
  table.each_with_index do |row, index|
    if with_index
      if index == 0
        table_str += '     '
      else
        table_str += "[#{index}]  "
      end
    end

    row.each do |col_size, data|
      table_str += (data.to_s + ' ' * (col_size - data.to_s.length)) + '    '
    end

    table_str += "\n"
  end
  table_str += '[...]' if self.count > limit
  print table_str
end