Class: Array

Inherits:
Object show all
Defined in:
lib/arql/ext/array.rb

Instance Method Summary collapse

Instance Method Details

#t(*attrs) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/arql/ext/array.rb', line 16

def t(*attrs)
  if attrs.present? && present? && first.is_a?(ActiveRecord::Base)
    puts Terminal::Table.new { |t|
      t << attrs
      t << :separator
      each do |e|
        t << e.attributes.values_at(*attrs.map(&:to_s))
      end
    }
  else
    table = Terminal::Table.new { |t|
      v.each { |row| t << (row || :separator)}
    }.to_s

    terminal_width = `tput cols`.to_i
    if table.lines.first.size > terminal_width
      table = table.lines.map(&:chomp)
      puts table[0..2].join("\n")
      puts table[3..-1].join("\n#{'-' * terminal_width}\n")
    else
      puts table
    end
  end
end

#to_insert_sql(batch_size = 500) ⇒ Object



2
3
4
5
6
7
# File 'lib/arql/ext/array.rb', line 2

def to_insert_sql(batch_size=500)
  raise 'All element should be an ActiveRecord instance object' unless all? { |e| e.is_a?(ActiveRecord::Base) }
  group_by(&:class).map do |(klass, records)|
    klass.to_insert_sql(records, batch_size)
  end.join("\n")
end

#to_upsert_sql(batch_size = 500) ⇒ Object



9
10
11
12
13
14
# File 'lib/arql/ext/array.rb', line 9

def to_upsert_sql(batch_size=500)
  raise 'All element should be an ActiveRecord instance object' unless all? { |e| e.is_a?(ActiveRecord::Base) }
  group_by(&:class).map do |(klass, records)|
    klass.to_upsert_sql(records, batch_size)
  end.join("\n")
end

#vObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/arql/ext/array.rb', line 41

def v
  return self unless present?
  t = []
  if map(&:class).uniq.size == 1
    if first.is_a?(ActiveRecord::Base)
      t << first.attribute_names
      t << nil
      each do |e|
        t << e.attributes.values_at(*first.attribute_names).map(&:as_json)
      end
    elsif first.is_a?(Array)
      t = map { |a| a.map(&:as_json) }
    elsif first.is_a?(Hash) || first.is_a?(ActiveSupport::HashWithIndifferentAccess)
      t << first.keys
      t << nil
      each do |e|
        t << e.values_at(*first.keys).map(&:as_json)
      end
    else
      return self
    end
  end
  t
end