Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added first version of export extension

  • Loading branch information...
commit 8fbae1b82e8b080f8dd65d485e2d61b05dfd0e5e 1 parent 3bd3a0c
@mwlang authored
View
1  README.md
@@ -19,7 +19,6 @@ This gem is released to gemcutter. Rubyforge is not utilized.
And then, in your project:
require 'sequel'
- require 'sequel_plus'
class Node < Sequel::Model
plugin :tree
View
3  Rakefile
@@ -14,8 +14,7 @@ begin
"LICENSE",
"README.md",
"Rakefile",
- "lib/sequel_plus.rb",
- "lib/**/*",
+ "lib/*",
"test/*"
]
View
2  VERSION
@@ -1 +1 @@
-0.0.4
+0.0.5
View
44 lib/extensions/export.rb
@@ -0,0 +1,44 @@
+# The export extension adds Sequel::Dataset#export and the
+# Sequel::Export class for creating plain-text data exports
+
+module Sequel
+ class Dataset
+ # Pretty prints the records in the dataset as plain-text table.
+ def export(delimiter="\t", *cols)
+ Sequel::Export.output(delimiter, naked.all, cols.empty? ? columns : cols)
+ end
+ end
+
+ module Export
+ def self.output(delimiter, records, columns = nil)
+ rows = []
+ columns ||= records.first.keys.sort_by{|x|x.to_s}
+ rows << columns.join(delimiter)
+
+ records.each {|r| rows << data_line(delimiter, columns, r)}
+ rows.join("\n")
+ end
+
+ ### Private Module Methods ###
+
+ # String for each data line
+ def self.data_line(delimiter, columns, record) # :nodoc:
+ columns.map {|c| format_cell(record[c])}.join(delimiter)
+ end
+
+ # Format the value so it takes up exactly size characters
+ def self.format_cell(v) # :nodoc:
+ case v
+ when Bignum, Fixnum
+ v.to_i.to_s
+ when Float, BigDecimal
+ v.to_f.to_s
+ else
+ v.to_s
+ end
+ end
+
+ # private_class_method :column_sizes, :data_line, :format_cell, :header_line, :separator_line
+ end
+end
+
View
1  lib/sequel_plus.rb
@@ -1 +1,2 @@
require 'sequel_tree'
+require 'extensions/export'
View
58 test/test_export.rb
@@ -0,0 +1,58 @@
+require 'helper'
+require 'sequel_plus'
+
+DB = Sequel.sqlite
+
+DB.create_table :nodes do
+ primary_key :id
+ String :name
+ Integer :parent_id
+ Integer :position
+end
+
+NODES = [
+ {:id => 1, :name => 'one', :parent_id => nil, :position => 1},
+ {:id => 2, :name => 'two', :parent_id => nil, :position => 2},
+ {:id => 3, :name => 'three', :parent_id => nil, :position => 3},
+ {:id => 4, :name => "two.one", :parent_id => 2, :position => 1},
+ {:id => 5, :name => "two.two", :parent_id => 2, :position => 2},
+ {:id => 6, :name => "two.two.one", :parent_id => 5, :position => 1},
+ {:id => 7, :name => "one.two", :parent_id => 1, :position => 2},
+ {:id => 8, :name => "one.one", :parent_id => 1, :position => 1},
+ {:id => 9, :name => "five", :parent_id => nil, :position => 5},
+ {:id => 10, :name => "four", :parent_id => nil, :position => 4},
+ {:id => 11, :name => "five.one", :parent_id => 9, :position => 1},
+ {:id => 12, :name => "two.three", :parent_id => 2, :position => 3},
+]
+
+DB.create_table :lorems do
+ primary_key :id
+ String :name
+ Integer :ipsum_id
+ Integer :neque
+end
+
+LOREMS = [
+ {:id => 1, :name => 'Lorem', :ipsum_id => nil, :neque => 4},
+ {:id => 2, :name => 'Ipsum', :ipsum_id => nil, :neque => 3},
+ {:id => 4, :name => "Neque", :ipsum_id => 2, :neque => 2},
+ {:id => 5, :name => "Porro", :ipsum_id => 2, :neque => 1},
+]
+
+NODES.each{|node| DB[:nodes].insert(node)}
+LOREMS.each{|lorem| DB[:lorems].insert(lorem)}
+
+describe Sequel::Export do
+
+ it "should instantiate" do
+ DB[:nodes].all.size.should == 12
+ end
+
+ it "should export everything" do
+ DB[:nodes].export.should == "id\tname\tparent_id\tposition\n1\tone\t\t1\n2\ttwo\t\t2\n3\tthree\t\t3\n4\ttwo.one\t2\t1\n5\ttwo.two\t2\t2\n6\ttwo.two.one\t5\t1\n7\tone.two\t1\t2\n8\tone.one\t1\t1\n9\tfive\t\t5\n10\tfour\t\t4\n11\tfive.one\t9\t1\n12\ttwo.three\t2\t3"
+ end
+
+ it "should export selected" do
+ DB[:nodes].filter(:id < 3).select(:id, :name).export.should == "id\tname\n1\tone\n2\ttwo"
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.