Sha256: 71dc2eb02ca5a7a0f834a2d4f804c0f8cc8ef3fac4fb1f3a3eb9ddcc9cd5a802

Contents?: true

Size: 1.81 KB

Versions: 12

Compression:

Stored size: 1.81 KB

Contents

class Array
  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

  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

  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

  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
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
arql-0.1.33 lib/arql/ext/array.rb
arql-0.1.32 lib/arql/ext/array.rb
arql-0.1.31 lib/arql/ext/array.rb
arql-0.1.30 lib/arql/ext/array.rb
arql-0.1.29 lib/arql/ext/array.rb
arql-0.1.28 lib/arql/ext/array.rb
arql-0.1.27 lib/arql/ext/array.rb
arql-0.1.26 lib/arql/ext/array.rb
arql-0.1.25 lib/arql/ext/array.rb
arql-0.1.24 lib/arql/ext/array.rb
arql-0.1.23 lib/arql/ext/array.rb
arql-0.1.22 lib/arql/ext/array.rb