hotchkiss 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hotchkiss/application.rb +6 -13
- data/lib/hotchkiss/cli/const.rb +6 -1
- data/lib/hotchkiss/cli/generate.rb +29 -3
- data/lib/hotchkiss/cli/migrate.rb +36 -8
- data/lib/hotchkiss/cli/willy.rb +16 -2
- data/lib/hotchkiss/controller.rb +11 -3
- data/lib/hotchkiss/fast_responder.rb +0 -6
- data/lib/hotchkiss/router.rb +0 -8
- data/lib/hotchkiss/templates/new/config.ru +4 -0
- data/lib/hotchkiss/version.rb +1 -1
- metadata +4 -3
@@ -6,7 +6,6 @@ module HK
|
|
6
6
|
|
7
7
|
@@router = nil
|
8
8
|
@@root = nil
|
9
|
-
@@favicon = nil
|
10
9
|
|
11
10
|
attr_accessor :router, :root
|
12
11
|
|
@@ -16,26 +15,17 @@ module HK
|
|
16
15
|
|
17
16
|
def self.root=(root)
|
18
17
|
@@root = root if @@root.nil?
|
19
|
-
begin
|
20
|
-
@@favicon = File.read(File.join("#{root}/public/favicon.ico"))
|
21
|
-
rescue
|
22
|
-
@@favicon = nil
|
23
|
-
end
|
24
18
|
end
|
25
19
|
|
26
20
|
def self.root
|
27
21
|
@@root
|
28
22
|
end
|
29
23
|
|
30
|
-
def self.favicon
|
31
|
-
@@favicon
|
32
|
-
end
|
33
|
-
|
34
24
|
def self.burnbabyburn!
|
35
25
|
lambda do |env|
|
36
26
|
begin
|
37
27
|
route, params = @@router.match?(env)
|
38
|
-
env['hk.params'] = (params.nil? ?
|
28
|
+
env['hk.params'] = (params.nil? ? {} : params)
|
39
29
|
if !route.nil? && route.has_key?(:special)
|
40
30
|
env['hk.controller'] = route[:controller]
|
41
31
|
elsif !route.nil?
|
@@ -44,7 +34,7 @@ module HK
|
|
44
34
|
raise Exception, "Unknown route for path: #{env['REQUEST_PATH']}"
|
45
35
|
end
|
46
36
|
env['hk.action'] = route[:action]
|
47
|
-
Object.const_get(env['hk.controller']).new.call(env)
|
37
|
+
resp = Object.const_get(env['hk.controller']).new.call(env)
|
48
38
|
rescue Exception => e
|
49
39
|
env['hk.action'] = "on_exception"
|
50
40
|
env['hk.exception'] = e
|
@@ -53,8 +43,11 @@ module HK
|
|
53
43
|
else
|
54
44
|
env['hk.controller'] = :FastResponder
|
55
45
|
end
|
56
|
-
Object.const_get(env['hk.controller']).new.call(env)
|
46
|
+
resp = Object.const_get(env['hk.controller']).new.call(env)
|
57
47
|
end
|
48
|
+
response = Rack::Response.new()
|
49
|
+
response.write(resp)
|
50
|
+
response.finish
|
58
51
|
end
|
59
52
|
end
|
60
53
|
|
data/lib/hotchkiss/cli/const.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
1
3
|
module Cli
|
2
4
|
|
5
|
+
DB_INFO_FILE = Dir.pwd + "/config/db.yml"
|
6
|
+
|
3
7
|
TEMPLATE_DIR = Gem::Specification.find_by_name("hotchkiss").gem_dir +
|
4
8
|
"/lib/hotchkiss/templates/"
|
5
|
-
DB_INFOS = YAML::load(File.open(
|
9
|
+
DB_INFOS = YAML::load(File.open(DB_INFO_FILE)) if File.exists?(DB_INFO_FILE)
|
6
10
|
MG_DIR = Dir.pwd + "/db/migrations/"
|
11
|
+
MODEL_DIR = Dir.pwd + "/code/app/models"
|
7
12
|
|
8
13
|
end ## Cli
|
@@ -4,19 +4,45 @@ module Cli
|
|
4
4
|
|
5
5
|
desc "model NAME", "Generate model files."
|
6
6
|
def model(name)
|
7
|
-
|
7
|
+
init
|
8
|
+
file = Dir.pwd + "/code/app/models/#{name}.rb"
|
9
|
+
unless Dir.exists?(Cli::MODEL_DIR)
|
10
|
+
Dir.mkdir(Cli::MODEL_DIR)
|
11
|
+
end
|
12
|
+
open(file, File::CREAT|File::TRUNC|File::RDWR) do |f|
|
13
|
+
f << "class #{name.capitalize} < Sequel::Model\nend\n"
|
14
|
+
end
|
15
|
+
puts "Creation: #{file}"
|
8
16
|
end
|
9
17
|
|
10
18
|
desc "controller NAME", "Generate controller files."
|
11
19
|
def controller(name)
|
12
|
-
|
20
|
+
init
|
21
|
+
file = Dir.pwd + "/code/app/controllers/#{name}_controller.rb"
|
22
|
+
open(file, File::CREAT|File::TRUNC|File::RDWR) do |f|
|
23
|
+
f << "class #{name.capitalize}Controller < HK::Controller\nend\n"
|
24
|
+
end
|
25
|
+
puts "Creation: #{file}"
|
13
26
|
end
|
14
27
|
|
15
28
|
desc "migration NAME", "Generate migration file."
|
16
29
|
def migration(name)
|
30
|
+
init
|
17
31
|
timestamp = Time.now.to_i
|
18
32
|
final_name = "#{timestamp}_#{name}"
|
19
|
-
|
33
|
+
file = Dir.pwd + "/db/migrations/#{final_name}.rb"
|
34
|
+
FileUtils.cp_r(Cli::TEMPLATE_DIR + "generate/migration/migration", file)
|
35
|
+
puts "Creation: #{file}"
|
36
|
+
end
|
37
|
+
|
38
|
+
no_tasks do
|
39
|
+
|
40
|
+
def init
|
41
|
+
unless File.exists?(Cli::MG_DIR)
|
42
|
+
abort("#{Cli::MG_DIR} directory doesn't exist, did you create your app ?\nPlease use 'hk new [app_name] to create it.")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
20
46
|
end
|
21
47
|
|
22
48
|
end # Generate
|
@@ -5,35 +5,63 @@ module Cli
|
|
5
5
|
desc "reset", "migration reset (erase and migration up)"
|
6
6
|
def reset
|
7
7
|
init
|
8
|
-
|
9
|
-
|
8
|
+
try_catch_migrate do
|
9
|
+
Sequel::Migrator.run(@db, MG_DIR, :target => 0)
|
10
|
+
Sequel::Migrator.run(@db, MG_DIR)
|
11
|
+
end
|
10
12
|
end
|
11
13
|
|
12
14
|
desc "to", "Update migrations (up or down) to VERSION."
|
13
15
|
def to
|
14
16
|
init
|
15
|
-
|
16
|
-
|
17
|
-
|
17
|
+
try_catch_migrate do
|
18
|
+
raise "No VERSION was provided" unless ENV['VERSION']
|
19
|
+
version = ENV['VERSION'].to_i
|
20
|
+
Sequel::Migrator.run(@db, MG_DIR, :target => version)
|
21
|
+
end
|
18
22
|
end
|
19
23
|
|
20
24
|
desc "up", "Update migrations to latest available."
|
21
25
|
def up
|
22
26
|
init
|
23
|
-
|
27
|
+
try_catch_migrate do
|
28
|
+
Sequel::Migrator.run(@db, MG_DIR)
|
29
|
+
end
|
24
30
|
end
|
25
31
|
|
26
32
|
desc "down", "Full migration downgrade (erase all data)."
|
27
33
|
def down
|
28
34
|
init
|
29
|
-
|
35
|
+
try_catch_migrate do
|
36
|
+
Sequel::Migrator.run(@db, MG_DIR, :target => 0)
|
37
|
+
end
|
30
38
|
end
|
31
39
|
|
32
40
|
no_tasks do
|
41
|
+
|
33
42
|
def init
|
34
43
|
Sequel.extension :migration
|
35
|
-
|
44
|
+
begin
|
45
|
+
@db = Sequel.connect(DB_INFOS[:url], DB_INFOS[:options])
|
46
|
+
rescue NameError
|
47
|
+
puts "Did you configure your config/db.yml file ?"
|
48
|
+
abort "Are you in your app directory ?"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def try_catch_migrate
|
53
|
+
begin
|
54
|
+
yield
|
55
|
+
rescue NoMethodError => e
|
56
|
+
puts "Migration error:"
|
57
|
+
puts "Do you have some migration defined ?"
|
58
|
+
abort "You can have error with the name of your migration (timestamp_name.rb) - Avoid '-' character."
|
59
|
+
rescue Sequel::Migrator::Error, Exception => e
|
60
|
+
puts "Migration error:"
|
61
|
+
abort e.message
|
62
|
+
end
|
36
63
|
end
|
64
|
+
|
37
65
|
end
|
38
66
|
|
39
67
|
end # Migrate
|
data/lib/hotchkiss/cli/willy.rb
CHANGED
@@ -5,8 +5,12 @@ module Cli
|
|
5
5
|
desc "new NAME", "Create new hotchkiss application directories with skel files."
|
6
6
|
def new(name)
|
7
7
|
dir = Dir.pwd + '/' + name
|
8
|
-
|
9
|
-
|
8
|
+
unless File.exists?(dir)
|
9
|
+
FileUtils.cp_r(TEMPLATE_DIR + "new", dir)
|
10
|
+
FileUtils.mkpath(dir + '/db/migrations')
|
11
|
+
else
|
12
|
+
abort("#{dir} exists. Remove it or change your app name.")
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
16
|
desc "server [host - default 127.0.0.1] [port - default 3000]", "Run development server."
|
@@ -22,6 +26,16 @@ module Cli
|
|
22
26
|
HK::Server.start!(options)
|
23
27
|
end
|
24
28
|
|
29
|
+
desc "console", "IRB console."
|
30
|
+
def console
|
31
|
+
require 'irb'
|
32
|
+
db = YAML::load(File.open("#{Dir.pwd}/config/db.yml"))
|
33
|
+
Sequel.connect(db[:url], db[:options])
|
34
|
+
Dir["#{Dir.pwd}/code/app/models/*.rb"].each { |file| require file }
|
35
|
+
ARGV.clear
|
36
|
+
IRB.start
|
37
|
+
end
|
38
|
+
|
25
39
|
desc "generate SUBCOMMAND", "Generators for model and controller files."
|
26
40
|
subcommand "generate", Generate
|
27
41
|
|
data/lib/hotchkiss/controller.rb
CHANGED
@@ -1,15 +1,13 @@
|
|
1
|
-
require 'pp'
|
2
1
|
module HK
|
3
2
|
|
4
3
|
class Controller
|
5
4
|
|
6
5
|
def call(env)
|
6
|
+
@env = env
|
7
7
|
@request = Rack::Request.new(env)
|
8
8
|
@response = Rack::Response.new()
|
9
9
|
@request.update_param :url, env['hk.params']
|
10
10
|
resp = self.send(env['hk.action'])
|
11
|
-
@response.write(resp)
|
12
|
-
@response.finish
|
13
11
|
end
|
14
12
|
|
15
13
|
private
|
@@ -18,6 +16,16 @@ module HK
|
|
18
16
|
@request.params
|
19
17
|
end
|
20
18
|
|
19
|
+
def reroute(controller, action, args = {})
|
20
|
+
name = controller.to_s.capitalize + 'Controller'
|
21
|
+
c = Object.const_get(name).new
|
22
|
+
env = @env
|
23
|
+
env['hk.controller'] = name
|
24
|
+
env['hk.action'] = action
|
25
|
+
env['hk.params'].merge args
|
26
|
+
c.call(env)
|
27
|
+
end
|
28
|
+
|
21
29
|
def redirect_to(url)
|
22
30
|
@response.redirect(url)
|
23
31
|
end
|
data/lib/hotchkiss/router.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'pp'
|
2
|
-
|
3
1
|
module HK
|
4
2
|
|
5
3
|
class Router
|
@@ -8,7 +6,6 @@ module HK
|
|
8
6
|
@routes_by_method = {}
|
9
7
|
@routes = []
|
10
8
|
yield(self)
|
11
|
-
finish
|
12
9
|
compute
|
13
10
|
end
|
14
11
|
|
@@ -69,11 +66,6 @@ module HK
|
|
69
66
|
end
|
70
67
|
end
|
71
68
|
|
72
|
-
def finish
|
73
|
-
@routes << { :method => :get, :path => "/favicon.ico", :action => "favicon",
|
74
|
-
:controller => :FastResponder, :special => true }
|
75
|
-
end
|
76
|
-
|
77
69
|
end # Router
|
78
70
|
|
79
71
|
end ## HK
|
@@ -5,6 +5,10 @@ require 'tilt'
|
|
5
5
|
require 'sequel'
|
6
6
|
require 'hotchkiss'
|
7
7
|
|
8
|
+
use Rack::Static,
|
9
|
+
:urls => ["/static", "/favicon.ico"],
|
10
|
+
:root => "public"
|
11
|
+
|
8
12
|
HK::Application.root = ::File.expand_path(::File.dirname(__FILE__))
|
9
13
|
require HK::Application.root + '/config/init'
|
10
14
|
|
data/lib/hotchkiss/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hotchkiss
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Thibaut Deloffre
|
9
|
+
- Valerian Cubero
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
+
date: 2013-09-21 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: bundler
|
@@ -160,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
161
|
version: '0'
|
161
162
|
requirements: []
|
162
163
|
rubyforge_project:
|
163
|
-
rubygems_version: 1.8.
|
164
|
+
rubygems_version: 1.8.24
|
164
165
|
signing_key:
|
165
166
|
specification_version: 3
|
166
167
|
summary: Ruby web framework
|