sequel_plus 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  # sequel_plus
2
2
 
3
- This library starts the collection of plugins and extensions I have assembled for the Ruby Sequel library.
4
- The library is in very early infancy stage, so there's not much presently, but what's here is fully covered
5
- in specs and tested and used in production-level deployments already.
6
-
3
+ This library contains a growing collection of plugins and extensions I have assembled for the Ruby Sequel library.
4
+ The library is in its infancy stage with new things being added and updated semi-frequently. Even so,
5
+ what's here is fully covered in specs and tested and used in production-level deployments already.
7
6
 
8
7
  ### Currently, sequel_plus contains:
9
8
  * plugin for Trees to mimic the Rails acts_as_tree plugin.
@@ -47,12 +46,15 @@ This gem is released to gemcutter. Rubyforge is not utilized.
47
46
  # Specific rows and columns
48
47
  File.open("nodes.txt", "w"){|file| DB[:nodes].filter(:id < 5).select(:id, :name).export(file)}
49
48
 
49
+ # Using pagination extension (for very large datasets)
50
+ File.open("nodes.txt", "w"){|file| DB[:nodes].export(file, :paginate => true, :page_size => 1000)}
51
+
50
52
  ## Use Rake Tasks
51
53
 
52
- Several rake tasks are made available simply by including the "tasks/sequel" per below:
54
+ Several rake tasks are made available simply by requiring "tasks/sequel" in your Rakefile
55
+ (or loaded rake scripts) per below:
53
56
 
54
57
  require 'sequel'
55
- require 'sequel_plus'
56
58
  require 'tasks/sequel'
57
59
 
58
60
  task :environment do
@@ -68,26 +70,34 @@ Several rake tasks are made available simply by including the "tasks/sequel" per
68
70
 
69
71
  Example tasks that are available:
70
72
 
71
- * rake db:desc[table] # Displays schema of table
72
- * rake db:fields[table] # Displays simple list of fields of table in sorted order
73
- * rake db:migrate # Migrate the database through scripts in db/migrate and update db/schema.rb by in...
74
- * rake db:migrate:down[step] # Reverts to previous schema version.
75
- * rake db:migrate:new[table,verb] # Creates a new migrate script.
76
- * rake db:migrate:redo # Rollbacks the database one migration and re-migrates up.
77
- * rake db:migrate:up[version] # Runs the "up" for a given migration VERSION.
78
- * rake db:reset # Drops all tables and recreates the schema from db/schema.rb
79
- * rake db:rollback # Rolls the schema back to the previous version.
80
- * rake db:schema:drop # drops the schema, using schema.rb
81
- * rake db:schema:dump # Dumps the schema to db/schema.db
82
- * rake db:schema:load # loads the schema from db/schema.rb
83
- * rake db:schema:version # Returns current schema version
84
- * rake db:show[table] # Displays content of table or lists all tables
85
- * rake db:tables # Displays a list of tables in the database
86
-
87
- These tasks will expect migrations to be in db/migration that is based off the folder your Rakefile resides in. If you wish to change the location of the "db" folder, simply declare :environment task and set APP_ROOT folder to be something other than the folder the Rakefile is residing in.
73
+ * rake sq:desc[table] # Displays schema of table
74
+ * rake sq:fields[table] # Displays simple list of fields of table in sorted order
75
+ * rake sq:migrate # Migrate the database through scripts in db/migrate and update db/schema.rb by in...
76
+ * rake sq:migrate:down[step] # Reverts to previous schema version.
77
+ * rake sq:migrate:new[table,verb] # Creates a new migrate script.
78
+ * rake sq:migrate:redo # Rollbacks the database one migration and re-migrates up.
79
+ * rake sq:migrate:up[version] # Runs the "up" for a given migration VERSION.
80
+ * rake sq:reset # Drops all tables and recreates the schema from db/schema.rb
81
+ * rake sq:rollback # Rolls the schema back to the previous version.
82
+ * rake sq:schema:drop # drops the schema, using schema.rb
83
+ * rake sq:schema:dump # Dumps the schema to db/schema.db
84
+ * rake sq:schema:load # loads the schema from db/schema.rb
85
+ * rake sq:schema:version # Returns current schema version
86
+ * rake sq:show[table] # Displays content of table or lists all tables
87
+ * rake sq:tables # Displays a list of tables in the database
88
+
89
+ These tasks will expect migrations to be in db/migration that is based off the folder your Rakefile resides in. If you wish to change the location of the "db" folder, simply declare :environment task and set SEQUEL_PLUS_APP_ROOT constant to be something other than the folder the Rakefile is residing in.
90
+
91
+ The rake tasks were constructed to fairly independent of the project environment they're injected into. To avoid name space collision,
92
+ the "db" namespace has been deprecated and the "sq" namespace adopted as of 0.2.0.
88
93
 
89
94
  # Note on Patches/Pull Requests
90
95
 
96
+ 0.2.0
97
+ * top-level namespace changed from "db" to "sq"
98
+ * designed and tested to work seamlessly with Padrino and Ramaze projects
99
+
100
+ 0.1.5
91
101
  * This release adds rake tasks
92
102
  * last release adds an export facility to the Sequel::Dataset
93
103
 
@@ -13,9 +13,14 @@
13
13
  module Sequel
14
14
  class Dataset
15
15
  def export(fd = $stdout, options = {})
16
+
16
17
  opts[:delimiter] = options[:delimiter] || "\t"
17
18
  opts[:quote_char] = options[:quote_char] || ''
18
19
  opts[:headers] = options[:headers] != false
20
+ opts[:paginate] = options[:paginate] || false
21
+ opts[:page_size] = options[:page_size] || 5000
22
+
23
+ Sequel.extension :pagination if opts[:paginate]
19
24
 
20
25
  Sequel::Export::Writer.new(fd, self, opts).output
21
26
  end
@@ -29,25 +34,18 @@ module Sequel
29
34
  @dataset = dataset
30
35
  @options = options
31
36
  end
32
-
33
- def output
34
- first_row = @dataset.first
35
- return unless first_row
36
-
37
- quot = @options[:quote_char]
38
- @columns ||= first_row.keys.sort_by{|x|x.to_s}
39
37
 
40
- if @options[:headers] == true
41
- @file.puts @columns.map{|col| "#{quot}#{col}#{quot}"}.join(@options[:delimiter])
42
- end
43
-
44
- @dataset.each do |row|
38
+ def export_data(ds)
39
+ quot = @options[:quote_char]
40
+ ds.each do |row|
45
41
  data = @columns.map do |col|
46
42
  case row[col]
47
43
  when Date then
48
44
  "#{quot}#{row[col].strftime('%Y-%m-%d')}#{quot}"
49
- when DateTime, Time then
45
+ when DateTime then
50
46
  "#{quot}#{row[col].localtime.strftime('%Y-%m-%dT%H:%M%Z')}#{quot}"
47
+ when Time then
48
+ "#{quot}#{row[col].localtime.strftime('%H:%M%Z')}#{quot}"
51
49
  when Float, BigDecimal then
52
50
  row[col].to_f
53
51
  when BigDecimal, Bignum, Fixnum then
@@ -59,6 +57,24 @@ module Sequel
59
57
  @file.puts data.join(@options[:delimiter])
60
58
  end
61
59
  end
60
+
61
+ def output
62
+ first_row = @dataset.first
63
+ return unless first_row
64
+
65
+ quot = @options[:quote_char]
66
+ @columns ||= first_row.keys.sort_by{|x|x.to_s}
67
+
68
+ if @options[:headers] == true
69
+ @file.puts @columns.map{|col| "#{quot}#{col}#{quot}"}.join(@options[:delimiter])
70
+ end
71
+
72
+ if @options[:paginate]
73
+ @dataset.each_page(@options[:page_size]){|paged_ds| export_data paged_ds}
74
+ else
75
+ export_data @dataset
76
+ end
77
+ end
62
78
 
63
79
  def build_row(row)
64
80
  quot = @options[:quote_char]
@@ -60,8 +60,8 @@ module Sequel
60
60
  #
61
61
  # subchild1.ancestors # => [child1, root]
62
62
  def descendants
63
- nodes = self.children
64
- nodes.each{|child| nodes + child.descendants}
63
+ nodes = self.children.dup
64
+ nodes.each{|child| nodes.concat(child.descendants)}
65
65
  nodes
66
66
  end
67
67
 
@@ -40,16 +40,22 @@ namespace :sq do
40
40
 
41
41
  desc "Displays schema of table"
42
42
  task :desc, [:table] => :load_config do |t, args|
43
- def o(value, size = 25)
44
- "%#{-1*size}s" % value.to_s
45
- end
46
43
  unless args[:table]
47
44
  Rake::Task["sq:tables"].invoke
48
45
  else
49
46
  puts '==[' << args.table << ']' << '=' * (80 - args.table.size - 4)
50
47
  DB.schema(args.table.to_sym).each_with_index do |col, i|
51
- name, info = col
52
- puts "#{o i+1, -3}: #{o name}:#{o info[:type], 15}:#{o info[:db_type], 15}:#{' not null ' unless info[:allow_null]} #{' pk ' if info[:primary_key]} #{' default: ' << info[:default].to_s if info[:default]}"
48
+ name, info = col
49
+ values = [
50
+ "%3s:" % (i + 1),
51
+ (" %-12s:" % "#{info[:db_type]}#{('(' + info[:max_chars].to_s + ')') if info[:max_chars]}"),
52
+ ("%15s:" % info[:type]),
53
+ "%-25s: " % name,
54
+ (' not null ' unless info[:allow_null]),
55
+ (' pk ' if info[:primary_key]),
56
+ (" default: %s" % info[:default] if info[:default]),
57
+ ]
58
+ puts values.join
53
59
  end
54
60
  puts '-' * 80
55
61
  indexes = DB.indexes(args.table.to_sym)
@@ -58,7 +64,7 @@ namespace :sq do
58
64
  else
59
65
  indexes.each_with_index do |idx, i|
60
66
  name, attrs = idx
61
- puts ' ' << o(name, 28) << ": unique? " << o(attrs[:unique] ? 'yes' : 'no', 6) << ': ' << attrs[:columns].join(', ')
67
+ puts ' ' << "%-28s" % name << ": unique? " << "%-6s" % (attrs[:unique] ? 'yes' : 'no') << ': ' << attrs[:columns].join(', ')
62
68
  end
63
69
  end
64
70
  puts '=' * 80
@@ -116,6 +116,27 @@ id name parent_id position
116
116
  9,"five","",5
117
117
  10,"four","",4
118
118
  11,"five.one",9,1
119
+ 12,"two.three",2,3
120
+ TEXT
121
+ end
122
+
123
+ it "should export paginated data with comma delimiter" do
124
+ mem_stream = StringIO.new("", "w+")
125
+ DB[:nodes].export(mem_stream, :quote_char => '"', :delimiter => ',', :paginate => true, :page_size => 2)
126
+ mem_stream.pos = 0
127
+ mem_stream.read.should == <<-TEXT
128
+ "id","name","parent_id","position"
129
+ 1,"one","",1
130
+ 2,"two","",2
131
+ 3,"three","",3
132
+ 4,"two.one",2,1
133
+ 5,"two.two",2,2
134
+ 6,"two.two.one",5,1
135
+ 7,"one.two",1,2
136
+ 8,"one.one",1,1
137
+ 9,"five","",5
138
+ 10,"four","",4
139
+ 11,"five.one",9,1
119
140
  12,"two.three",2,3
120
141
  TEXT
121
142
  end
@@ -71,7 +71,7 @@ module SequelTreeTest
71
71
  it "should find all descendants of a node" do
72
72
  two = Node.find(:id => 2)
73
73
  two.name.should == "two"
74
- two.descendants.map{|m| m[:id]}.should == [4, 5, 12]
74
+ two.descendants.map{|m| m[:id]}.should == [4, 5, 12, 6]
75
75
  end
76
76
 
77
77
  it "should find all ancestors of a node" do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Lang
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-04 00:00:00 -04:00
17
+ date: 2010-12-21 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -61,7 +61,7 @@ files:
61
61
  - lib/extensions/export.rb
62
62
  - lib/sequel_plus.rb
63
63
  - lib/sequel_tree.rb
64
- - lib/tasks/databases.rake
64
+ - lib/tasks/sequel.rake
65
65
  - lib/tasks/sequel.rb
66
66
  - test/helper.rb
67
67
  - test/test_export.rb