padrino-lazy 0.0.1alpha
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/LICENSE +20 -0
- data/README.rdoc +51 -0
- data/Rakefile +62 -0
- data/bin/padrino-lazy +156 -0
- data/lib/padrino-lazy/version.rb +13 -0
- data/padrino-lazy.gemspec +20 -0
- metadata +53 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Carlo Bertini
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= LAZY Agnostic Application Generators (padrino-lazy)
|
2
|
+
|
3
|
+
=== Overview
|
4
|
+
|
5
|
+
Through padrino-lazy, we can include some common properties shared between the model object used a base model object
|
6
|
+
|
7
|
+
Padrino is very good framework, and *almost* follows the DRY principle (Don't Repeat Yourself)
|
8
|
+
When we need to have some common properties shared between the model object of our ORM, using the generator padrino-gen,
|
9
|
+
We can *only* include these properties in each model to generate.
|
10
|
+
|
11
|
+
I hope this idea will be included in Padrino Framework.
|
12
|
+
|
13
|
+
== Principle / Step
|
14
|
+
|
15
|
+
1. generate a model
|
16
|
+
2. convert the model into base model ( move this model into lib/ folder)
|
17
|
+
3. save config file with model base's list
|
18
|
+
4. generate a model from base model
|
19
|
+
5. generate a migration file
|
20
|
+
|
21
|
+
== Usage
|
22
|
+
|
23
|
+
Step 1-2-3
|
24
|
+
$ padrino-lazy base --c config_file --b base_model_name --f field_to_create
|
25
|
+
|
26
|
+
Step 4-5
|
27
|
+
$ padrino-lazy model --c config_file --b base_model_name --f field_to_create --m model_name
|
28
|
+
|
29
|
+
=== Options
|
30
|
+
|
31
|
+
--f list field to generate
|
32
|
+
--c config file's name (saved into config/ folder)
|
33
|
+
--b base model name (es: BaseModel)
|
34
|
+
--m model name to create (es: User)
|
35
|
+
|
36
|
+
== Known issue
|
37
|
+
|
38
|
+
* VERY VERY alpha code !! (thanks to my pig/lazy side :D )
|
39
|
+
* --f options need dobule quote around fields
|
40
|
+
* TESTING only with datamapper and activerecord into linux machine
|
41
|
+
|
42
|
+
=== TODO:
|
43
|
+
* Test all code !
|
44
|
+
* Use Padrino::Generators instead of Commander
|
45
|
+
* Default config file name (very lazy :P )
|
46
|
+
|
47
|
+
|
48
|
+
== THANK
|
49
|
+
Team Padrino
|
50
|
+
DaddYE
|
51
|
+
Piedinodifata
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rubygems/specification' unless defined?(Gem::Specification)
|
2
|
+
require 'rake' unless defined?(Rake)
|
3
|
+
|
4
|
+
# Runs the sh command with sudo if the rake command is run with sudo
|
5
|
+
def sudo_sh(command)
|
6
|
+
command = `whoami`.strip! != "root" ? "sudo #{command}" : command
|
7
|
+
sh command
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns the gem specification object for a gem
|
11
|
+
def gemspec
|
12
|
+
@gemspec ||= begin
|
13
|
+
::Gem::Specification.load("padrino-lazy.gemspec")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Most notable functions are:
|
18
|
+
# $ rake package # packages the gem into the pkg folder
|
19
|
+
# $ rake install # installs the gem into system
|
20
|
+
# $ rake release # publishes gem to rubygems
|
21
|
+
|
22
|
+
desc "Validates the gemspec"
|
23
|
+
task :gemspec do
|
24
|
+
gemspec.validate
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Displays the current version"
|
28
|
+
task :version do
|
29
|
+
puts "Current version: #{gemspec.version}"
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "Installs the gem locally"
|
33
|
+
task :install => :package do
|
34
|
+
sudo_sh "gem install pkg/#{gemspec.name}-#{gemspec.version}"
|
35
|
+
end
|
36
|
+
|
37
|
+
desc "Uninstalls the gem locally"
|
38
|
+
task :uninstall do
|
39
|
+
sudo_sh "gem uninstall padrino-lazy -v #{gemspec.version}"
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "Release the gem"
|
43
|
+
task :release => :package do
|
44
|
+
sh "gem push pkg/#{gemspec.name}-#{gemspec.version}.gem"
|
45
|
+
sh "rm -rf pkg"
|
46
|
+
sh "git add . && git commit -m 'Bump to version #{gemspec.version}' && git push"
|
47
|
+
end
|
48
|
+
|
49
|
+
# rake package
|
50
|
+
begin
|
51
|
+
require 'rake/gempackagetask'
|
52
|
+
rescue LoadError
|
53
|
+
task(:gem) { $stderr.puts '`gem install rake` to package gems' }
|
54
|
+
else
|
55
|
+
Rake::GemPackageTask.new(gemspec) do |pkg|
|
56
|
+
pkg.gem_spec = gemspec
|
57
|
+
end
|
58
|
+
task :gem => :gemspec
|
59
|
+
end
|
60
|
+
|
61
|
+
task :package => :gemspec
|
62
|
+
task :default => :install
|
data/bin/padrino-lazy
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems' unless defined?(Gem)
|
3
|
+
require 'commander/import'
|
4
|
+
require 'yaml'
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
padrino_gen_path = File.expand_path('../../lib/padrino-lazy', __FILE__)
|
8
|
+
$:.unshift(padrino_gen_path)
|
9
|
+
require 'version'
|
10
|
+
|
11
|
+
##################################
|
12
|
+
### COMMANDER ###
|
13
|
+
##################################
|
14
|
+
|
15
|
+
program :name, 'padrino-lazy'
|
16
|
+
program :version, Padrino::Lazy.version
|
17
|
+
program :description, 'Padrino model generator for lazy dev!'
|
18
|
+
program :help, 'Author', 'Carlo Bertini [WaYdotNET] <waydotnet@gmail.com>'
|
19
|
+
|
20
|
+
default_command :help
|
21
|
+
|
22
|
+
|
23
|
+
# HACK commander's bug
|
24
|
+
# see bug https://github.com/visionmedia/commander/issues/8
|
25
|
+
def hack_options command
|
26
|
+
command.option '--c', '--config FILE', String, 'config file to save into config directory'
|
27
|
+
command.option '--b', '--base STRING', String, 'base model name'
|
28
|
+
command.option '--f', '--fields STRING', String, 'field to create'
|
29
|
+
end
|
30
|
+
|
31
|
+
command :base do |c|
|
32
|
+
hack_options c
|
33
|
+
c.syntax = "padrino-lazy base [options]"
|
34
|
+
c.description ="Creation BASE"
|
35
|
+
|
36
|
+
c.action do |args, options|
|
37
|
+
say "common options"
|
38
|
+
common options
|
39
|
+
|
40
|
+
say "create a basic model"
|
41
|
+
%x{ padrino g model #{options.base} #{options.fields} -s }
|
42
|
+
say "store data into config file"
|
43
|
+
store_result options
|
44
|
+
|
45
|
+
say "copy file to lib folder"
|
46
|
+
FileUtils.mkdir "#{Dir.pwd}/lib" if !File.exist? "#{Dir.pwd}/lib"
|
47
|
+
FileUtils.cp("#{Dir.pwd}/app/models/#{options.base.to_underscore}.rb", "#{Dir.pwd}/lib/#{options.base.to_underscore}.rb")
|
48
|
+
|
49
|
+
say "delete model"
|
50
|
+
%x{ padrino g model #{options.base} -d }
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
command :model do |c|
|
56
|
+
hack_options c
|
57
|
+
c.syntax = "padrino-lazy model [options]"
|
58
|
+
c.description ="Creation MODEL"
|
59
|
+
c.option '--m STRING','--model', String, 'model name'
|
60
|
+
c.action do |args, options|
|
61
|
+
common options
|
62
|
+
options.model = ask ("model: ") if !options.model
|
63
|
+
|
64
|
+
load_base_model options
|
65
|
+
|
66
|
+
# create model
|
67
|
+
%x{ padrino g model #{options.model} #{options.fields} }
|
68
|
+
|
69
|
+
# read model file
|
70
|
+
lazy = File.open("app/models/#{options.model.to_underscore}.rb").read
|
71
|
+
|
72
|
+
# lazy model :P
|
73
|
+
lazy_work lazy, options
|
74
|
+
|
75
|
+
File.open("app/models/#{options.model.to_underscore}.rb","w+"){|f| f.puts lazy }
|
76
|
+
|
77
|
+
# create migration
|
78
|
+
%x{ padrino g migration AddBasicModelTo#{options.model} #{options.fields} }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
##################################
|
83
|
+
### PADRINO-LAZY ###
|
84
|
+
##################################
|
85
|
+
|
86
|
+
|
87
|
+
# HACK: camelcase
|
88
|
+
class String
|
89
|
+
def to_underscore
|
90
|
+
self.gsub(/::/, '/').
|
91
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
92
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
93
|
+
tr("-", "_").
|
94
|
+
downcase
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
# check common input
|
100
|
+
def common options
|
101
|
+
# check input information
|
102
|
+
options.base = ask ("base model: ") if !options.base
|
103
|
+
options.fields = ask ("fields: ") if !options.fields
|
104
|
+
options.config = ask ("config file: ") if !options.config
|
105
|
+
|
106
|
+
@base_all= []
|
107
|
+
if File.exist? "config/#{options.config}.yml"
|
108
|
+
file = File.open("config/#{options.config}.yml")
|
109
|
+
@base_all = YAML.load(file)
|
110
|
+
end
|
111
|
+
@base_current = nil
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
# replace/remove code
|
116
|
+
def lazy_work(lazy, options)
|
117
|
+
# DataMapper
|
118
|
+
lazy.gsub!("class #{options.model}\n","class #{options.model} < #{options.base}\n")
|
119
|
+
lazy.gsub!("include DataMapper::Resource\n","")
|
120
|
+
lazy.gsub!("property :id, Serial\n","")
|
121
|
+
# ActiveRecord
|
122
|
+
lazy.gsub!("ActiveRecord::Base","#{options.base}")
|
123
|
+
# CouchRest
|
124
|
+
lazy.gsub!("CouchRest::Model::Base","#{options.base}")
|
125
|
+
# MongoId ????
|
126
|
+
lazy.gsub!("class #{options.model}\n","class #{options.model} < #{options.base}\n")
|
127
|
+
lazy.gsub!("include Mongoid::Document\n","")
|
128
|
+
lazy.gsub!("include Mongoid::Timestamps # adds created_at and updated_at fields\n","")
|
129
|
+
# MongoMapper
|
130
|
+
lazy.gsub!("class #{options.model}\n","class #{options.model} < #{options.base}\n")
|
131
|
+
lazy.gsub!("include Mongoid::Document\n","")
|
132
|
+
# Mongomatic
|
133
|
+
lazy.gsub!("Mongomatic::Base","#{options.base}")
|
134
|
+
# Ohm
|
135
|
+
lazy.gsub!("Ohm::Model","#{options.base}")
|
136
|
+
lazy.gsub!("include Ohm::Timestamping\n","")
|
137
|
+
lazy.gsub!("include Ohm::Typecast\n","")
|
138
|
+
# Sequel
|
139
|
+
lazy.gsub!("Sequel::Model","#{options.base}")
|
140
|
+
end
|
141
|
+
|
142
|
+
# load select base model
|
143
|
+
def load_base_model options
|
144
|
+
@base_all.each{|f| @base_current = f if f["base"] == options.base}
|
145
|
+
end
|
146
|
+
|
147
|
+
# save options to config file
|
148
|
+
def store_result options
|
149
|
+
say "load base model"
|
150
|
+
load_base_model options
|
151
|
+
@base_all.delete @base_current
|
152
|
+
@base_current = { "base" => options.base, "fields" => options.fields}
|
153
|
+
@base_all << @base_current
|
154
|
+
File.open("config/#{options.config}.yml","w+"){|f| f.puts @base_all.to_yaml}
|
155
|
+
end
|
156
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
##
|
2
|
+
# Manages current Padrino lazy version for use in gem generation.
|
3
|
+
module Padrino
|
4
|
+
module Lazy
|
5
|
+
VERSION = '0.0.1alpha' unless defined?(Padrino::Lazy::VERSION)
|
6
|
+
##
|
7
|
+
# Return the current Padrino version
|
8
|
+
#
|
9
|
+
def self.version
|
10
|
+
VERSION
|
11
|
+
end
|
12
|
+
end # Lazy
|
13
|
+
end # Padrino
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/padrino-lazy/version.rb", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "padrino-lazy"
|
6
|
+
s.rubyforge_project = "padrino-lazy"
|
7
|
+
s.authors = "Carlo Bertini [WaYdotNET]"
|
8
|
+
s.email = "waydotnet@gmail.com"
|
9
|
+
s.summary = "Padrino model base generator for Padrino Framework"
|
10
|
+
s.homepage = "http://www.waydotnet.com"
|
11
|
+
s.description = "padrino model base generator for the Padrino Ruby Web Framework"
|
12
|
+
s.required_rubygems_version = ">= 1.3.6"
|
13
|
+
s.version = Padrino::Lazy.version
|
14
|
+
s.date = Time.now.strftime("%Y-%m-%d")
|
15
|
+
s.extra_rdoc_files = Dir["*.rdoc"]
|
16
|
+
s.files = %w(LICENSE README.rdoc Rakefile padrino-lazy.gemspec) + Dir.glob("{bin,lib,test}/**/*")
|
17
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
18
|
+
s.require_path = "lib"
|
19
|
+
s.executables = `ls bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: padrino-lazy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1alpha
|
5
|
+
prerelease: 5
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Carlo Bertini [WaYdotNET]
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-05-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: padrino model base generator for the Padrino Ruby Web Framework
|
15
|
+
email: waydotnet@gmail.com
|
16
|
+
executables:
|
17
|
+
- padrino-lazy
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files:
|
20
|
+
- README.rdoc
|
21
|
+
files:
|
22
|
+
- LICENSE
|
23
|
+
- README.rdoc
|
24
|
+
- Rakefile
|
25
|
+
- padrino-lazy.gemspec
|
26
|
+
- bin/padrino-lazy
|
27
|
+
- lib/padrino-lazy/version.rb
|
28
|
+
homepage: http://www.waydotnet.com
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options:
|
32
|
+
- --charset=UTF-8
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>'
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.3.1
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project: padrino-lazy
|
49
|
+
rubygems_version: 1.8.3
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Padrino model base generator for Padrino Framework
|
53
|
+
test_files: []
|