lib/arql/ext/array.rb in arql-0.3.5 vs lib/arql/ext/array.rb in arql-0.3.6
- old
+ new
@@ -1,5 +1,7 @@
+require 'arql/vd'
+
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)
@@ -44,9 +46,36 @@
puts table[3..-1].join("\n#{'-' * terminal_width}\n")
else
puts table
end
end
+ end
+
+ def vd(*attrs, **options)
+ if (attrs.present? || options.present? && options[:except]) && present? && first.is_a?(ActiveRecord::Base)
+ column_names = first.attribute_names.map(&:to_sym)
+ attrs = attrs.flat_map { |e| e.is_a?(Regexp) ? column_names.grep(e) : e }.uniq
+ if options.present? && options[:except]
+ attrs = column_names if attrs.empty?
+ if options[:except].is_a?(Regexp)
+ attrs.reject! { |e| e =~ options[:except] }
+ else
+ attrs -= [options[:except]].flatten
+ end
+ end
+
+ Arql::VD.new do |vd|
+ vd << attrs
+ each do |e|
+ vd << e.attributes.values_at(*attrs.map(&:to_s))
+ end
+ end
+ else
+ Arql::VD.new do |vd|
+ v.each { |row| vd << row if row }
+ end
+ end
+ nil
end
def v
return self unless present?
t = []