Class: Array
Instance Method Summary collapse
- #dump(filename, batch_size = 500) ⇒ Object
- #t(*attrs, **options) ⇒ Object
- #to_insert_sql(batch_size = 500) ⇒ Object
- #to_upsert_sql(batch_size = 500) ⇒ Object
- #v(**options) ⇒ Object
- #vd(*attrs, **options) ⇒ Object
- #write_csv(filename, *fields, **options) ⇒ Object
- #write_excel(filename, *fields, **options) ⇒ Object
Instance Method Details
#dump(filename, batch_size = 500) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/arql/ext/array.rb', line 172 def dump(filename, batch_size=500) File.open(File.(filename), 'w') do |file| group_by(&:class).each do |(klass, records)| file.puts(klass.to_upsert_sql(records, batch_size)) end end {size: size, file: File.(filename)} end |
#t(*attrs, **options) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/arql/ext/array.rb', line 19 def t(*attrs, **) if (attrs.present? || .present? && [: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 .present? && [:except] attrs = column_names if attrs.empty? if [:except].is_a?(Regexp) attrs.reject! { |e| e =~ [:except] } else attrs -= [[:except]].flatten end end # if options[:compact] # attrs = attrs.select { |e| any { |r| r.attributes[e.to_s]&.present? } } # end 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
5 6 7 8 9 10 |
# File 'lib/arql/ext/array.rb', line 5 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
12 13 14 15 16 17 |
# File 'lib/arql/ext/array.rb', line 12 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 |
#v(**options) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/arql/ext/array.rb', line 84 def v(**) return self unless present? t = [] if map(&:class).uniq.size == 1 if first.is_a?(ActiveRecord::Base) attribute_names = first.attribute_names if [:compact] attribute_names = attribute_names.select { |e| any? { |r| r.attributes[e]&.present? } } end t << attribute_names t << nil each do |e| t << e.attributes.values_at(*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) keys = first.keys if [:compact] keys = keys.select { |e| any? { |r| r[e]&.present? } } end t << keys t << nil each do |e| t << e.values_at(*keys).map(&:as_json) end else return self end end t end |
#vd(*attrs, **options) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/arql/ext/array.rb', line 57 def vd(*attrs, **) if (attrs.present? || .present? && [: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 .present? && [:except] attrs = column_names if attrs.empty? if [:except].is_a?(Regexp) attrs.reject! { |e| e =~ [:except] } else attrs -= [[: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 |
#write_csv(filename, *fields, **options) ⇒ Object
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 |
# File 'lib/arql/ext/array.rb', line 117 def write_csv(filename, *fields, **) generate_csv(filename, **) do |csv| if size > 0 && first.is_a?(ActiveRecord::Base) if fields.empty? fields = first.attributes.keys else fields = fields.map(&:to_s) end csv << fields end if size > 0 && first.is_a?(Hash) if fields.empty? fields = first.keys end csv << fields end each do |row| if row.is_a?(Array) csv << row.map(&:to_s) else csv << row.slice(*fields).values.map(&:to_s) end end end end |
#write_excel(filename, *fields, **options) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/arql/ext/array.rb', line 143 def write_excel(filename, *fields, **) sheet_name = [:sheet_name] || 'Sheet1' generate_excel(filename) do |workbook| workbook.add_worksheet(name: sheet_name) do |sheet| if size > 0 && first.is_a?(ActiveRecord::Base) if fields.empty? fields = first.attributes.keys else fields = fields.map(&:to_s) end sheet.add_row(fields, types: [:string] * fields.size) end if size > 0 && first.is_a?(Hash) if fields.empty? fields = first.keys end sheet.add_row(fields, types: [:string] * fields.size) end each do |row| if row.is_a?(Array) sheet.add_row(row.map(&:to_s), types: [:string] * row.size) else sheet.add_row(row.slice(*fields).values.map(&:to_s), types: [:string] * fields.size) end end end end end |