dbf 3.0.3 → 3.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fcab794d4aecd4f5e77cfd92906d883668cf7dd
4
- data.tar.gz: d5ccc39ccfd37f5b6c539b4185f52769c0b6382e
3
+ metadata.gz: 8a9691bee280ae2d7df25c7f2fb328a8ac15d9dd
4
+ data.tar.gz: c93cc164e9b0749baa2b700a4f322d4ae7c0242a
5
5
  SHA512:
6
- metadata.gz: f3cddf7e897fa59cb4560b452eddd89f196f574e827383d8470d79a9e5448356acc59764de27a6257f4f6efd5b3334f69e6a2c263b24c5ba27472e2d30baf71b
7
- data.tar.gz: 3577e03cd8f8327dd60aadcb8c124e102d26f755c82588d5020df09fa3e1bc666c3b59e88069d70309753d42c250ad60a279f114a9bf8fcf5973d618e39817c8
6
+ metadata.gz: f0870b61e88a071d6d0d5cb8cec8036fad9566f00820ae5186298507598c6f4cd1f563dcd0d56f46e501fa392b8f33225915008cd0a9a0ba5eace029e9092926
7
+ data.tar.gz: aa88664313b12814de6590430b129f4414a9b967119b21a40537ed9a1b53698f2d04c0a5083ed7c50c1ce35fc8d8995cc055c9da46523197192e35375332134e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 3.0.4
2
+ - Adds -v command-line option to print version
3
+ - Adds -r command-line option to create Sequel migration
4
+ -
5
+
1
6
  # 3.0.3
2
7
  - Uninitialized (N)umbers should return nil
3
8
 
data/README.md CHANGED
@@ -166,6 +166,32 @@ class CreateBooks < ActiveRecord::Migration
166
166
  end
167
167
  ```
168
168
 
169
+ ## Migrating to Sequel
170
+
171
+ An example of migrating a DBF book table to Sequel using a migration:
172
+
173
+ ```ruby
174
+ require 'dbf'
175
+
176
+ class Book < Sequel::Model; end
177
+
178
+ Sequel.migration do
179
+ up do
180
+ table = DBF::Table.new('db/dbf/books.dbf')
181
+ eval(table.schema(:sequel, true)) # passing true to limit output to create_table() only
182
+
183
+ Book.reset_column_information
184
+ table.each do |record|
185
+ Book.create(title: record.title, author: record.author)
186
+ end
187
+ end
188
+
189
+ down do
190
+ drop_table(:books)
191
+ end
192
+ end
193
+ ```
194
+
169
195
  ## Command-line utility
170
196
 
171
197
  A small command-line utility called dbf is installed along with the gem.
@@ -173,14 +199,20 @@ A small command-line utility called dbf is installed along with the gem.
173
199
  $ dbf -h
174
200
  usage: dbf [-h|-s|-a] filename
175
201
  -h = print this message
202
+ -v = print the version number
176
203
  -s = print summary information
177
204
  -a = create an ActiveRecord::Schema
205
+ -r = create a Sequel Migration
178
206
  -c = create a csv file
179
207
 
180
208
  Create an executable ActiveRecord schema:
181
209
 
182
210
  dbf -a books.dbf > books_schema.rb
183
211
 
212
+ Create an executable Sequel schema:
213
+
214
+ dbf -r books.dbf > migrate/001_create_books.rb
215
+
184
216
  Dump all records to a CSV file:
185
217
 
186
218
  dbf -c books.dbf > books.csv
data/bin/dbf CHANGED
@@ -3,22 +3,34 @@
3
3
  require 'dbf'
4
4
  require 'optparse'
5
5
 
6
- params = ARGV.getopts('h', 's', 'a', 'c')
6
+ params = ARGV.getopts('h', 's', 'a', 'c', 'r', 'v')
7
7
 
8
- if params['h'] then
9
- puts "usage: #{File.basename(__FILE__)} [-h|-s|-a|-c] filename"
8
+ if params['v'] then
9
+ puts "dbf version: #{DBF::VERSION}"
10
+
11
+ elsif params['h'] then
12
+ puts "usage: #{File.basename(__FILE__)} [-h|-s|-a|-c|-r] filename"
10
13
  puts " -h = print this message"
14
+ puts " -v = print the DBF gem version"
11
15
  puts " -s = print summary information"
12
16
  puts " -a = create an ActiveRecord::Schema"
13
- puts " -c = create a csv file"
17
+ puts " -r = create a Sequel migration"
18
+ puts " -c = create a CSV file"
14
19
  else
20
+
15
21
  filename = ARGV.shift
16
22
  abort "You must supply a filename on the command line" unless filename
17
23
 
18
24
  # create an ActiveRecord::Schema
19
25
  if params['a']
20
26
  table = DBF::Table.new filename
21
- puts table.schema
27
+ puts table.schema(:activerecord)
28
+ end
29
+
30
+ # create an Sequel::Migration
31
+ if params['r']
32
+ table = DBF::Table.new filename
33
+ puts table.schema(:sequel)
22
34
  end
23
35
 
24
36
  if params['s']
data/lib/dbf.rb CHANGED
@@ -3,6 +3,7 @@ require 'date'
3
3
  require 'csv'
4
4
  require 'json'
5
5
 
6
+ require 'dbf/version'
6
7
  require 'dbf/schema'
7
8
  require 'dbf/record'
8
9
  require 'dbf/column_type'
data/lib/dbf/column.rb CHANGED
@@ -63,6 +63,13 @@ module DBF
63
63
  "\"#{underscored_name}\", #{schema_data_type}\n"
64
64
  end
65
65
 
66
+ # Sequel Schema definition
67
+ #
68
+ # @return [String]
69
+ def sequel_schema_definition
70
+ ":#{underscored_name}, #{schema_data_type(:sequel)}\n"
71
+ end
72
+
66
73
  # Underscored name
67
74
  #
68
75
  # This is the column name converted to underscore format.
@@ -112,7 +119,7 @@ module DBF
112
119
  ]
113
120
  end
114
121
 
115
- def schema_data_type # nodoc
122
+ def schema_data_type(format = :activerecord) # nodoc
116
123
  case type
117
124
  when 'N', 'F'
118
125
  decimal > 0 ? ':float' : ':integer'
@@ -135,7 +142,11 @@ module DBF
135
142
  ':text'
136
143
  end
137
144
  else
138
- ":string, :limit => #{length}"
145
+ if format == :sequel
146
+ ":varchar, :size => #{length}"
147
+ else
148
+ ":string, :limit => #{length}"
149
+ end
139
150
  end
140
151
  end
141
152
 
data/lib/dbf/schema.rb CHANGED
@@ -23,16 +23,16 @@ module DBF
23
23
  #
24
24
  # @param [Symbol] format Valid options are :activerecord and :json
25
25
  # @return [String]
26
- def schema(format = :activerecord)
27
- supported_formats = [:activerecord, :json]
26
+ def schema(format = :activerecord, table_only = false)
27
+ supported_formats = [:activerecord, :json, :sequel]
28
28
  if supported_formats.include?(format)
29
- send "#{format}_schema"
29
+ send("#{format}_schema", table_only)
30
30
  else
31
31
  raise ArgumentError
32
32
  end
33
33
  end
34
34
 
35
- def activerecord_schema
35
+ def activerecord_schema(_table_only = false)
36
36
  s = "ActiveRecord::Schema.define do\n"
37
37
  s << " create_table \"#{File.basename(@data.path, '.*')}\" do |t|\n"
38
38
  columns.each do |column|
@@ -42,7 +42,21 @@ module DBF
42
42
  s
43
43
  end
44
44
 
45
- def json_schema
45
+ def sequel_schema(table_only = false)
46
+ s = ''
47
+ s << "Sequel.migration do\n" unless table_only
48
+ s << " change do\n " unless table_only
49
+ s << " create_table(:#{File.basename(@data.path, '.*')}) do\n"
50
+ columns.each do |column|
51
+ s << " column #{column.sequel_schema_definition}"
52
+ end
53
+ s << " end\n"
54
+ s << " end\n" unless table_only
55
+ s << "end\n" unless table_only
56
+ s
57
+ end
58
+
59
+ def json_schema(_table_only = false)
46
60
  columns.map(&:to_hash).to_json
47
61
  end
48
62
  end
data/lib/dbf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module DBF
2
- VERSION = '3.0.3'
2
+ VERSION = '3.0.4'
3
3
  end
@@ -56,6 +56,57 @@ RSpec.describe DBF::Table do
56
56
  end
57
57
  end
58
58
 
59
+ describe '#sequel_schema' do
60
+ it 'should return a valid Sequel migration by default' do
61
+ expect(table.sequel_schema).to eq <<-SCHEMA
62
+ Sequel.migration do
63
+ change do
64
+ create_table(:dbase_83) do
65
+ column :id, :integer
66
+ column :catcount, :integer
67
+ column :agrpcount, :integer
68
+ column :pgrpcount, :integer
69
+ column :order, :integer
70
+ column :code, :varchar, :size => 50
71
+ column :name, :varchar, :size => 100
72
+ column :thumbnail, :varchar, :size => 254
73
+ column :image, :varchar, :size => 254
74
+ column :price, :float
75
+ column :cost, :float
76
+ column :desc, :text
77
+ column :weight, :float
78
+ column :taxable, :boolean
79
+ column :active, :boolean
80
+ end
81
+ end
82
+ end
83
+ SCHEMA
84
+ end
85
+
86
+ it 'should return a limited Sequel migration when passed true' do
87
+ expect(table.sequel_schema(true)).to eq <<-SCHEMA
88
+ create_table(:dbase_83) do
89
+ column :id, :integer
90
+ column :catcount, :integer
91
+ column :agrpcount, :integer
92
+ column :pgrpcount, :integer
93
+ column :order, :integer
94
+ column :code, :varchar, :size => 50
95
+ column :name, :varchar, :size => 100
96
+ column :thumbnail, :varchar, :size => 254
97
+ column :image, :varchar, :size => 254
98
+ column :price, :float
99
+ column :cost, :float
100
+ column :desc, :text
101
+ column :weight, :float
102
+ column :taxable, :boolean
103
+ column :active, :boolean
104
+ end
105
+ SCHEMA
106
+ end
107
+
108
+ end
109
+
59
110
  describe '#json_schema' do
60
111
  it 'is valid JSON' do
61
112
  expect { JSON.parse(table.json_schema) }.to_not raise_error
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbf
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.3
4
+ version: 3.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Morrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-18 00:00:00.000000000 Z
11
+ date: 2016-01-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A small fast library for reading dBase, xBase, Clipper and FoxPro database
14
14
  files.