ez 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/ez.gemspec +23 -0
- data/lib/ez.rb +12 -0
- data/lib/ez/apis.rb +13 -0
- data/lib/ez/dispatcher.rb +28 -0
- data/lib/ez/mapper.rb +22 -0
- data/lib/ez/version.rb +3 -0
- data/lib/tasks/db_modifier.rb +149 -0
- data/lib/tasks/ez_tasks.rake +35 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 27de4129826c5db63af2490801c3fa81261eeb36
|
4
|
+
data.tar.gz: 7a54089e8550dea5e52d53c0bd0a638fb7f36c8e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6b808ff61e3cf8b49abc2cb54bb04997fb3f5cdb899fcf9c37b6ae6a7a310abbe3cbace17fee0044d9463760e08ef379633c7f9c113a760f1e991914fed3de3
|
7
|
+
data.tar.gz: c06a8348edb9e3ab3765904c3bbcb0c5166d716f7f403bae7d3fb7be7d7a4cdfae7d17ce892a3f4fa4e3f6f7eef54913546eeed34f25d49ee9d232dd443a9ec1
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Jeff Cohen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Ez
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ez'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ez
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/ez.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ez/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ez"
|
8
|
+
spec.version = Ez::VERSION
|
9
|
+
spec.authors = ["Jeff Cohen"]
|
10
|
+
spec.email = ["cohen.jeff@gmail.com"]
|
11
|
+
spec.description = "Gem for easier Rails development."
|
12
|
+
spec.summary = "For educational purposes only."
|
13
|
+
spec.homepage = "http://www.jeffcohenonline.com/ez"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = []
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake", '~> 10.0', '>= 10.0.0'
|
23
|
+
end
|
data/lib/ez.rb
ADDED
data/lib/ez/apis.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# require_relative './jeff_controller.rb'
|
2
|
+
|
3
|
+
module ActionDispatch
|
4
|
+
module Routing
|
5
|
+
class RouteSet
|
6
|
+
class Dispatcher
|
7
|
+
def controller(params, default_controller=true)
|
8
|
+
if params && params.key?(:controller)
|
9
|
+
controller_param = params[:controller]
|
10
|
+
controller_reference(controller_param)
|
11
|
+
end
|
12
|
+
rescue NameError => e
|
13
|
+
raise ActionController::RoutingError, e.message, e.backtrace if default_controller
|
14
|
+
end
|
15
|
+
|
16
|
+
def controller_reference(controller_param)
|
17
|
+
begin
|
18
|
+
const_name = @controller_class_names[controller_param] ||= "#{controller_param.camelize}Controller"
|
19
|
+
ActiveSupport::Dependencies.constantize(const_name)
|
20
|
+
rescue NameError => e
|
21
|
+
Kernel.class_eval("class ::#{const_name} < ApplicationController; end")
|
22
|
+
ActiveSupport::Dependencies.constantize("::#{const_name}")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/ez/mapper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActionDispatch
|
2
|
+
module Routing
|
3
|
+
class Mapper
|
4
|
+
class Mapping
|
5
|
+
|
6
|
+
def route_uses_slash_and_no_hash?
|
7
|
+
@options[:to].is_a?(String) &&
|
8
|
+
@options[:to].index('#').nil? &&
|
9
|
+
@options[:to].index('/')
|
10
|
+
end
|
11
|
+
|
12
|
+
def to
|
13
|
+
if route_uses_slash_and_no_hash?
|
14
|
+
@options[:to].sub('/','#')
|
15
|
+
else
|
16
|
+
@options[:to]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/ez/version.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
class DbModifier
|
2
|
+
|
3
|
+
attr_reader :db, :spec
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
ActiveRecord::Base.establish_connection({
|
7
|
+
adapter: 'sqlite3',
|
8
|
+
database: "db/development.sqlite3",
|
9
|
+
})
|
10
|
+
@db = ActiveRecord::Base.connection
|
11
|
+
ActiveRecord::Migration.verbose = false
|
12
|
+
load_models_yml
|
13
|
+
end
|
14
|
+
|
15
|
+
def migrate
|
16
|
+
@changed = false
|
17
|
+
add_missing_schema
|
18
|
+
remove_dead_schema
|
19
|
+
puts "Everything is up-to-date." unless @changed
|
20
|
+
end
|
21
|
+
|
22
|
+
def tables
|
23
|
+
@tables ||= (db.tables - ['schema_migrations'])
|
24
|
+
end
|
25
|
+
|
26
|
+
def has_model?(model_name)
|
27
|
+
has_table?(model_name.tableize)
|
28
|
+
end
|
29
|
+
|
30
|
+
def has_table?(name)
|
31
|
+
tables.index(name).present?
|
32
|
+
end
|
33
|
+
|
34
|
+
def missing_model?(model_name)
|
35
|
+
missing_table?(model_name.tableize)
|
36
|
+
end
|
37
|
+
|
38
|
+
def missing_table?(name)
|
39
|
+
tables.index(name).nil?
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_missing_schema
|
43
|
+
@spec.each do |model_name, columns|
|
44
|
+
if missing_model?(model_name)
|
45
|
+
add_model(model_name, columns)
|
46
|
+
else
|
47
|
+
add_missing_columns(model_name, columns)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def display_change(message)
|
53
|
+
puts message
|
54
|
+
@change = true
|
55
|
+
end
|
56
|
+
|
57
|
+
def add_missing_columns(model_name, columns)
|
58
|
+
table_name = model_name.tableize
|
59
|
+
columns.each do |column|
|
60
|
+
col_name = column.keys.first
|
61
|
+
col_type = column[col_name]
|
62
|
+
if db.column_exists?(table_name, col_name.to_sym)
|
63
|
+
unless db.column_exists?(table_name, col_name.to_sym, col_type.to_sym)
|
64
|
+
display_change "Detected new column type for '#{col_name}' to #{col_type}"
|
65
|
+
db.change_column(table_name, col_name.to_sym, col_type.to_sym)
|
66
|
+
end
|
67
|
+
else
|
68
|
+
display_change "Detected new column '#{col_name}' as #{col_type} for model #{model_name}"
|
69
|
+
db.add_column(table_name, col_name.to_sym, col_type.to_sym)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def add_model(model_name, columns)
|
75
|
+
table_name = model_name.tableize
|
76
|
+
display_change "Defining model #{model_name}..."
|
77
|
+
ActiveRecord::Schema.define do
|
78
|
+
create_table table_name do |t|
|
79
|
+
columns.each do |column|
|
80
|
+
name = column.keys.first
|
81
|
+
col_type = column[name]
|
82
|
+
t.send(col_type, name)
|
83
|
+
end
|
84
|
+
t.timestamps
|
85
|
+
end
|
86
|
+
end
|
87
|
+
filename = "app/models/#{model_name.underscore}.rb"
|
88
|
+
unless File.exists?(filename)
|
89
|
+
display_change "Creating model file: #{filename}"
|
90
|
+
File.open(filename, "w") do |f|
|
91
|
+
f.puts "class #{model_name} < ActiveRecord::Base"
|
92
|
+
f.puts "end"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def remove_dead_schema
|
98
|
+
remove_dead_tables
|
99
|
+
remove_dead_columns
|
100
|
+
end
|
101
|
+
|
102
|
+
def remove_dead_columns
|
103
|
+
tables.each do |table_name|
|
104
|
+
model_name = table_name.classify.to_s
|
105
|
+
if @spec.has_key?(model_name)
|
106
|
+
columns = db.columns(table_name).map { |column| column.name.to_sym } - [:id, :created_at, :updated_at]
|
107
|
+
spec_columns = @spec[model_name].map do |column_entry|
|
108
|
+
column_entry.is_a?(Hash) ? column_entry.keys.first.to_sym : column_entry.to_sym
|
109
|
+
end
|
110
|
+
dead_columns = columns - spec_columns
|
111
|
+
if dead_columns.any?
|
112
|
+
display_change "Detected unused columns: #{dead_columns.to_sentence} for model #{model_name}"
|
113
|
+
db.remove_columns(table_name, *dead_columns)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def remove_dead_tables
|
120
|
+
tables_we_need = @spec.keys.map { |model| model.tableize }
|
121
|
+
dead_tables = tables - tables_we_need
|
122
|
+
|
123
|
+
dead_tables.each do |table_name|
|
124
|
+
model_name = table_name.classify
|
125
|
+
display_change "Dropping model #{model_name}"
|
126
|
+
db.drop_table(table_name)
|
127
|
+
display_change "Deleting file #{model_name.underscore}.rb"
|
128
|
+
File.unlink "app/models/#{model_name.underscore}.rb" rescue nil
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def load_models_yml
|
133
|
+
@spec = YAML.load_file('db/models.yml')
|
134
|
+
@spec ||= {}
|
135
|
+
|
136
|
+
@spec.each do |model, columns|
|
137
|
+
@spec[model] = []
|
138
|
+
columns.each do |column|
|
139
|
+
if column.is_a?(String) || column.is_a?(Symbol)
|
140
|
+
@spec[model] << { column.to_s => 'string' }
|
141
|
+
elsif column.is_a?(Hash) && column.keys.count == 1
|
142
|
+
@spec[model] << { column.keys.first.to_s => column.values.first.to_s }
|
143
|
+
else
|
144
|
+
raise "Bad syntax."
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative 'db_modifier'
|
2
|
+
|
3
|
+
namespace :ez do
|
4
|
+
|
5
|
+
desc "Reset the database scheme from scratch."
|
6
|
+
task :reset_tables => ['db:drop', :tables] do
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Automatically update the database schema and model files."
|
10
|
+
task :tables => :environment do
|
11
|
+
if File.exists?('db/models.yml')
|
12
|
+
db = DbModifier.new
|
13
|
+
db.migrate
|
14
|
+
else
|
15
|
+
puts "To get started, edit the db/models.yml file to describe your table schema."
|
16
|
+
File.open("db/models.yml", "w") do |f|
|
17
|
+
f.puts <<-EOS
|
18
|
+
# Example table for a typical Book model.
|
19
|
+
#
|
20
|
+
# Book:
|
21
|
+
# - title: string
|
22
|
+
# - price: integer
|
23
|
+
# - author: string
|
24
|
+
# - summary: text
|
25
|
+
# - hardcover: boolean
|
26
|
+
#
|
27
|
+
# Follow the syntax exactly (colons and spacing are important).
|
28
|
+
# Column type choices are: string, text, integer, and boolean.
|
29
|
+
# You can have as many models as you want in this file.
|
30
|
+
EOS
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ez
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Cohen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 10.0.0
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '10.0'
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 10.0.0
|
47
|
+
description: Gem for easier Rails development.
|
48
|
+
email:
|
49
|
+
- cohen.jeff@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- ez.gemspec
|
60
|
+
- lib/ez.rb
|
61
|
+
- lib/ez/apis.rb
|
62
|
+
- lib/ez/dispatcher.rb
|
63
|
+
- lib/ez/mapper.rb
|
64
|
+
- lib/ez/version.rb
|
65
|
+
- lib/tasks/db_modifier.rb
|
66
|
+
- lib/tasks/ez_tasks.rake
|
67
|
+
homepage: http://www.jeffcohenonline.com/ez
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata: {}
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.2.0
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: For educational purposes only.
|
91
|
+
test_files: []
|