model_schema 0.1.2 → 0.1.3

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: 71d0f2c2d2b1fb23f4b1120c7f7dd2f800b2e303
4
- data.tar.gz: d8b9fa7fd9ed35c9a682244241502b05106af5ed
3
+ metadata.gz: e819b3df6515baecd8bac8bf213f1abd288e8217
4
+ data.tar.gz: 35b2388aaa00737be74417ff2cec92ddc62adbee
5
5
  SHA512:
6
- metadata.gz: a8166541034ee1c12f7fd2d6ec39829509651548e7c44bf801912e4cca9875201a72b621b4f6065c01c6b33afe84d14455f1e74cebbfba2cc5c8bb77ce258cf2
7
- data.tar.gz: dddbdd9f473e7b518a145747fc09d772cab97ff54ca89df0d640f42efb86f7bcb905c679467514642a2252316dc09fc578fcec53b4037de489d9dd6a1d2213de
6
+ metadata.gz: 77899af8c7f4087cf5757afe87908d2b5687f638a09842f23b45e9dcdb842b51c0a3dbf95af738e17057972cf5d39c3a7d8559a32526d27a15b30a2c1cb9c9c1
7
+ data.tar.gz: 6ee5e09645ec9328ab3564586d243e157c2253be51da737ae24dcb71157c6b3bbba6ddab949dac1eef8beffad17a1c6cda867b5e9bf2bef84e1aa9fbcf9d1e31
data/README.md CHANGED
@@ -130,7 +130,7 @@ the `dump_model_schema` executable. It will automatically dump an up-to-date
130
130
  `model_schema` block in each Sequel Model class. Use it like so:
131
131
 
132
132
  ```sh
133
- $ dump_model_schema -m [model_file] -c [connection_string]
133
+ $ dump_model_schema -c [connection_string] model_file [model_file ...]
134
134
  ```
135
135
 
136
136
  where `model_file` is a path to a ruby file that contains a single Sequel
@@ -138,10 +138,10 @@ Model, and `connection_string` is the database connection string to pass to
138
138
  `Sequel.connect()`.
139
139
 
140
140
  `dump_model_schema` will insert a `model_schema` block right after the
141
- definition of the Sequel Model class. Specifically, it looks for a line of the
142
- form `class SomeClassName < Sequel::Model(:table_name)`, and inserts a valid
143
- schema for table `table_name` directly after that line. Note that
144
- `dump_model_schema` overwrites the model file.
141
+ definition of the Sequel Model class in `model_file`. Specifically, it looks
142
+ for a line of the form `class SomeClassName < Sequel::Model(:table_name)`, and
143
+ inserts a valid schema for table `table_name` directly after that line. Note
144
+ that `dump_model_schema` overwrites `model_file`.
145
145
 
146
146
  For instance, say you had a file `items.rb` that looks like this:
147
147
 
@@ -155,7 +155,7 @@ end
155
155
  If you run:
156
156
 
157
157
  ```sh
158
- $ dump_model_schema -m items.rb -c [connection_string]
158
+ $ dump_model_schema -c [connection_string] items.rb
159
159
  ```
160
160
 
161
161
  `items.rb` might now look like:
@@ -179,6 +179,13 @@ By default, `dump_model_schema` assumes a tab size of 2 spaces, but you can
179
179
  change this with the `-t` option. Pass an integer representing the number of
180
180
  spaces, or 0 if you want to use hard tabs.
181
181
 
182
+ You may specify multiple `model_file`s as distinct arguments, and each will
183
+ have its `model_schema` dumped. This can be done easily with shell expansion:
184
+
185
+ ```sh
186
+ $ dump_model_schema -c [connection_string] models/*.rb
187
+ ```
188
+
182
189
  You may see help text with `dump_model_schema -h` and view the version of
183
190
  ModelSchema with `dump_model_schema -v`.
184
191
 
@@ -11,11 +11,8 @@ module ModelSchema
11
11
  opts[:tabbing] = 2
12
12
 
13
13
  parser = OptionParser.new do |p|
14
- p.banner = "Usage: dump_model_schema [options]"
15
-
16
- p.on('-m', '--model MODEL', 'Model file to dump schema in') do |model|
17
- opts[:model] = model
18
- end
14
+ p.banner = 'Usage: dump_model_schema [options] model_file [model_file ...]'
15
+ p.separator "\nDumps a valid model_schema block in each given model_file.\n\n"
19
16
 
20
17
  p.on('-c', '--connection CONNECTION',
21
18
  'Connection string for database') do |connection|
@@ -38,24 +35,42 @@ module ModelSchema
38
35
  end
39
36
  end
40
37
 
41
- parser.parse(args)
38
+ model_files = parser.parse(args)
42
39
 
43
40
  # model and connection are required
44
- abort 'Must provide a model file with -m or --model.' if !opts[:model]
41
+ abort 'Must provide at least one model file.' if model_files.empty?
45
42
  abort 'Must provide a connection string with -c or --connection.' if !opts[:connection]
46
43
 
47
- dump_model_schema(opts)
48
- end
49
-
50
- # Dumps the model schema based on the given options (see option parsing above).
51
- def self.dump_model_schema(opts)
52
- model_info = parse_model_file(opts[:model])
53
- abort "Couldn't find class that extends Sequel::Model" if !model_info
54
-
55
44
  db = Sequel.connect(opts[:connection])
56
45
  db.extension(:schema_dumper)
57
46
 
58
- klass = Class.new(Sequel::Model(model_info[:table_name]))
47
+ if db.is_a?(Sequel::Postgres::Database)
48
+ # include all Postgres type extensions so schema dumps are accurate
49
+ db.extension(:pg_array, :pg_enum, :pg_hstore, :pg_inet, :pg_json,
50
+ :pg_range, :pg_row)
51
+ end
52
+
53
+ had_error = false
54
+ model_files.each do |path|
55
+ begin
56
+ dump_model_schema(db, path, opts)
57
+ rescue StandardError, SystemExit => error
58
+ # SystemExit error messages are already printed by abort()
59
+ $stderr.puts error.message if error.is_a?(StandardError)
60
+ had_error = true
61
+ end
62
+ end
63
+
64
+ exit 1 if had_error
65
+ end
66
+
67
+ # Dumps a valid model_schema into the given file path. Accepts options as
68
+ # per the OptionParser above.
69
+ def self.dump_model_schema(db, path, opts)
70
+ model = parse_model_file(path)
71
+ abort "In #{path}, couldn't find class that extends Sequel::Model" if !model
72
+
73
+ klass = Class.new(Sequel::Model(model[:table_name]))
59
74
  klass.db = db
60
75
  klass.plugin(ModelSchema::Plugin)
61
76
 
@@ -66,7 +81,7 @@ module ModelSchema
66
81
 
67
82
  # account for indentation
68
83
  tab = opts[:tabbing] == 0 ? "\t" : ' ' * opts[:tabbing]
69
- schema_indentation = model_info[:indentation] + tab
84
+ schema_indentation = model[:indentation] + tab
70
85
  command_indentation = schema_indentation + tab
71
86
 
72
87
  commands = commands.lines.map {|l| l == "\n" ? l : command_indentation + l}.join
@@ -76,8 +91,8 @@ module ModelSchema
76
91
  "#{commands}\n",
77
92
  "#{schema_indentation}end\n"]
78
93
 
79
- lines = model_info[:lines_before] + dump_lines + model_info[:lines_after]
80
- File.write(opts[:model], lines.join)
94
+ lines = model[:lines_before] + dump_lines + model[:lines_after]
95
+ File.write(path, lines.join)
81
96
  end
82
97
 
83
98
  # Parses the model file at the given path, returning a hash of the form:
@@ -100,7 +115,7 @@ module ModelSchema
100
115
  if table_name[0] == ':'
101
116
  table_name = table_name[1..-1].to_sym
102
117
  else
103
- abort "Can't find a symbol table name on line: #{line}"
118
+ abort "In #{path}, can't find a symbol table name in line: #{line}"
104
119
  end
105
120
 
106
121
  # indentation for model_schema block
@@ -1,3 +1,3 @@
1
1
  module ModelSchema
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karthik Viswanathan