ez 0.9.7 → 0.9.9

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: 46f68c1557debf7db379e3240a76ffad307d12f4
4
- data.tar.gz: 3c88b18298f9e8c7d24bc0d6ce2e800b5c32f26c
3
+ metadata.gz: c1435cdce1c508b5097a63994d5410bacc4d70b0
4
+ data.tar.gz: c45bc846995af9ff76cd7b4ae516f1bb9d9921b0
5
5
  SHA512:
6
- metadata.gz: 43c5759b7b0236b82bc8c063dcc84176cad6249d11156b18b42b466a1e8e3f18a666a9273112880aacb5917e79cc784ba88b9dfe08e5ae32fa8a47026cfc77da
7
- data.tar.gz: f60d613d29cb36d4cf12ac59c7d53003f9634aa8ce26fc20edbaf8e97e2d081381228ebfd83144daab73491001f854757fddae672a1735f8f85f45593b0c434c
6
+ metadata.gz: 4531ea674bbd3fb547aeda0ab8ed898592292a6997497aecc46637b5917693a45f56b3137c9c3057d02bc96909eb0fc3fa53034d95f2ba05d8591c75de3d5162
7
+ data.tar.gz: 9b13d85e4e0ed661244b87e7cb3a485131904da9adb7e1c967a72363cfdf4c487a681b5fc1c4048031273d372de1539ba440f06ef81d97b5843561acc80adcb6
data/README.md CHANGED
@@ -2,9 +2,11 @@
2
2
 
3
3
  **Version 0.9.8**
4
4
 
5
+ *For educational purposes only.*
6
+
5
7
  Makes domain modeling in Rails more beginner-friendly by avoiding migrations.
6
8
 
7
- For educational purposes only.
9
+ Also enhances the rails console.
8
10
 
9
11
 
10
12
  ## Usage
@@ -13,15 +15,35 @@ For educational purposes only.
13
15
  gem 'ez'
14
16
  ```
15
17
 
18
+ Then:
19
+
20
+ `rake db:migrate` to generate a skeleton `db/models.yml` that you should edit.
21
+
22
+
23
+ ## Summary of Best Practices
24
+
25
+ 1. Use `db/models.yml` to define your schema. Foreign-key indexes will be generated automatically. (This should cover 100% of what most beginners will ever need to do.)
26
+
27
+ 2. Use Rails migrations for any additional indexes, database constraints, etc.
28
+
29
+ 3. Just use `rake db:migrate` as usual. Nothing new to learn.
30
+
31
+
32
+
16
33
  ## Features
17
34
 
18
35
 
19
36
  ### 1. Domain Modeling Enhancements
20
37
 
21
- * Enables **instant domain modeling without migrations** by editing a simple file named `db/models.yml` and then running `rake db:migrate`. If things ever get messed up, use `rake ez:reset_tables` to drop the entire db first.
22
- * You can run `rake ez:tables` once to generate a file named `db/models.yml`. It will have some self-documenting comments inside of it.
23
- * In development mode, there's no need to ever run `rake db:migrate` - every action will trigger automatic table updates.
24
- * You can run `rake db:migrate` whenever you modify `db/models.yml` instead of waiting for a browser action.
38
+ * Enables **instant domain modeling without migrations** by using a file named `db/models.yml`.
39
+ * No new rake tasks to learn. This gem enhances `db:migrate` to incorporate the `db/models.yml` file automatically.
40
+ * You can run `rake db:migrate` to initially generate a file named `db/models.yml`. It will have some self-documenting comments inside of it.
41
+ * In development mode, there's no need to ever run `rake db:migrate`! Every brower request will trigger automatic table updates.
42
+ * In the rails console, 'reload!' will also trigger table updates.
43
+ * If you prefer, you can run `rake db:migrate` whenever you modify `db/models.yml`.
44
+ * `rake db:migrate` will run any pending migrations *after* following instructions in the `db/models.yml` file.
45
+
46
+
25
47
 
26
48
  **Syntax Guide for `db/models.yml`**
27
49
 
data/lib/ez.rb CHANGED
@@ -11,28 +11,47 @@ require 'ez/view_helpers.rb'
11
11
  require 'hirb' if Rails.env.development?
12
12
 
13
13
  module EZ
14
+ module Console
15
+ def reload!(print=true)
16
+ puts "Reloading code..." if print
17
+ ActionDispatch::Reloader.cleanup!
18
+ ActionDispatch::Reloader.prepare!
19
+
20
+ puts "Updating tables (if necessary) ..." if print
21
+ old_level = ActiveRecord::Base.logger.level
22
+ ActiveRecord::Base.logger.level = Logger::WARN
23
+ EZ::DomainModeler.generate_models_yml
24
+ EZ::DomainModeler.update_tables
25
+ puts "Models: #{EZ::DomainModeler.models.to_sentence}"
26
+ ActiveRecord::Base.logger.level = old_level
27
+ true
28
+ end
29
+ end
30
+ end
31
+
32
+ module EZ
33
+
14
34
  class Railtie < Rails::Railtie
15
35
  rake_tasks do
16
36
  load "tasks/ez_tasks.rake"
17
37
  Rake::Task["db:migrate"].enhance ["ez:tables"]
18
38
  end
19
39
 
20
- config.to_prepare do
21
- Rails.cache.fetch('ez-generate-yml-flag') do
22
- EZ::DomainModeler.generate_models_yml
23
- end
24
- EZ::DomainModeler.update_tables
25
- end
26
-
27
40
  console do |app|
28
- Hirb.enable(pager: false) if Rails.env.development? && defined?(Hirb)
41
+ Rails::ConsoleMethods.send :prepend, EZ::Console
42
+ Hirb.enable(pager: false) if ::Rails.env.development? && defined?(Hirb)
43
+
44
+ old_level = ActiveRecord::Base.logger.level
45
+ ActiveRecord::Base.logger.level = Logger::WARN
46
+ EZ::DomainModeler.generate_models_yml
47
+ EZ::DomainModeler.update_tables(true)
48
+ ActiveRecord::Base.logger.level = old_level
29
49
 
30
50
  I18n.enforce_available_locales = false
31
51
  puts "Welcome to the Rails Console."
32
52
  puts "-" * 60
33
53
  puts
34
- tables = ActiveRecord::Base.connection.tables - ['schema_migrations']
35
- models = tables.map { |t| t.classify }
54
+ models = EZ::DomainModeler.models
36
55
  if models.any?
37
56
  puts "Models: #{models.to_sentence}"
38
57
  puts
@@ -9,12 +9,19 @@ module EZ
9
9
  # Valid formats for default values
10
10
  DEFAULT_VALUE_REGEXES = [/\s*\((.+)?\)/, /\s+(.+)?\s*/, /,\s*default:\s*(.+)?\s*/]
11
11
 
12
+ def self.models
13
+ tables = ActiveRecord::Base.connection.tables - ['schema_migrations']
14
+ tables.map { |t| t.classify }
15
+ end
16
+
12
17
  # Get the domain model as a hash
13
18
  attr_reader :spec
14
19
 
15
20
  def initialize
21
+ @ok = false
16
22
  begin
17
23
  load_model_specs
24
+ @ok = true
18
25
  rescue => e
19
26
  puts e
20
27
  end
@@ -52,15 +59,23 @@ module EZ
52
59
  end
53
60
 
54
61
  def update_tables(silent = false)
62
+ return false unless @ok
63
+
55
64
  SchemaModifier.migrate(@spec, silent)
65
+ return true
56
66
 
57
67
  rescue => e
58
68
  puts e.message unless silent
59
69
  puts e.backtrace.first unless silent
70
+ @ok = false
71
+ return false
60
72
  end
61
73
 
62
74
  def load_model_specs_from_string(s)
63
75
 
76
+ # Ignore comments
77
+ s.gsub!(/#.*$/,'')
78
+
64
79
  # Append missing colons
65
80
  s.gsub!(/^((\s|\-)*\w[^\:]+?)$/, '\1:')
66
81
 
@@ -74,8 +89,8 @@ module EZ
74
89
  parse_model_spec
75
90
 
76
91
  # puts "@spec:"
77
- # puts @spec.inspect
78
- # puts "-" * 10
92
+ puts @spec.inspect
93
+ puts "-" * 10
79
94
  end
80
95
 
81
96
  def load_model_specs(filename = "db/models.yml")
@@ -86,10 +101,19 @@ module EZ
86
101
  @spec ||= {}
87
102
  @spec.each do |model, columns|
88
103
 
104
+ msg = nil
105
+
89
106
  if !columns.is_a?(Hash)
90
- raise "Could not understand models.yml while parsing model: #{model}"
107
+ msg = "Could not understand models.yml while parsing model '#{model}'."
108
+ end
109
+
110
+ if model !~ /^[A-Z]/ || model =~ /\s/
111
+ msg = "Could not understand models.yml while parsing model '#{model}'."
112
+ msg << " Models must begin with an uppercase letter and cannot contain spaces."
91
113
  end
92
114
 
115
+ raise msg if msg
116
+
93
117
  columns.each do |column_name, column_type|
94
118
  interpret_column_spec column_name, column_type, model
95
119
  end
@@ -112,7 +136,7 @@ module EZ
112
136
  end
113
137
  end
114
138
 
115
- default_column_value = (column_type == 'boolean' ? true : nil)
139
+ default_column_value = (column_type == 'boolean' ? false : nil)
116
140
  DEFAULT_VALUE_REGEXES.each { |r| default_column_value = $1 if column_type.sub!(r, '') }
117
141
  default_column_value = default_column_value.to_i if column_type == 'integer'
118
142
  default_column_value = default_column_value.to_f if column_type == 'float'
@@ -138,8 +138,8 @@ module EZ
138
138
  end
139
139
 
140
140
  def update_schema_version
141
- db.initialize_schema_migrations_table
142
- db.assume_migrated_upto_version(Time.now.utc.strftime("%Y%m%d%H%M%S"))
141
+ # db.initialize_schema_migrations_table
142
+ # db.assume_migrated_upto_version(Time.now.utc.strftime("%Y%m%d%H%M%S"))
143
143
  end
144
144
 
145
145
  def remove_dead_tables
@@ -1,3 +1,3 @@
1
1
  module EZ
2
- VERSION = "0.9.7"
2
+ VERSION = "0.9.9"
3
3
  end
@@ -8,19 +8,39 @@ namespace :ez do
8
8
  f.puts <<-EOS
9
9
  # Example table for a typical Book model.
10
10
  #
11
- Book
12
- title: string
13
- price: integer
14
- author: string
15
- summary: text
16
- hardcover: boolean
11
+ # Book
12
+ # title: string
13
+ # author_id: integer
14
+ # price: integer
15
+ # summary: text
16
+ # hardcover: boolean
17
17
  #
18
18
  # Indent consistently! Follow the above syntax exactly.
19
- # Typical column choices are: string, text, integer, boolean, date, and datetime.
19
+ # Typical column choices are: string, text, integer, boolean, date, time, and datetime.
20
20
  #
21
+ #
22
+ # Specifying Default Values
23
+ # ----------------------------------------------------
21
24
  # Default column values can be specified like this:
22
25
  # price: integer(0)
23
26
  #
27
+ # If not specified, Boolean columns always default to false.
28
+ #
29
+ #
30
+ # Convention-Based Defaults:
31
+ # ----------------------------------------------------
32
+ # You can omit the column type if it's a string, or if it's obviously an integer column:
33
+ #
34
+ #
35
+ # Book
36
+ # title
37
+ # author_id
38
+ # price: integer
39
+ # summary: text
40
+ # hardcover: boolean
41
+ #
42
+ # Complete details are in the README file online.
43
+ #
24
44
  # Have fun!
25
45
 
26
46
  EOS
@@ -35,7 +55,6 @@ EOS
35
55
 
36
56
  desc "Attempts to update the database schema and model files with minimal data loss."
37
57
  task :tables => [:environment] do
38
- puts "Running ez:tables..."
39
58
  if File.exists?('db/models.yml')
40
59
  if EZ::DomainModeler.update_tables
41
60
  Rake::Task["db:schema:dump"].invoke unless Rails.env.production?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ez
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Cohen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-27 00:00:00.000000000 Z
11
+ date: 2014-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hirb