hotchkiss 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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? ? "" : params)
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
 
@@ -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(Dir.pwd + "/config/db.yml"))
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
- puts name
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
- puts name
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
- FileUtils.cp_r(Cli::TEMPLATE_DIR + "generate/migration/migration", Dir.pwd + "/db/migrations/#{final_name}.rb")
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
- Sequel::Migrator.run(@db, MG_DIR, :target => 0)
9
- Sequel::Migrator.run(@db, MG_DIR)
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
- version = ENV['VERSION'].to_i
16
- raise "No VERSION was provided" if version.nil?
17
- Sequel::Migrator.run(@db, MG_DIR, :target => version)
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
- Sequel::Migrator.run(@db, MG_DIR)
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
- Sequel::Migrator.run(@db, MG_DIR, :target => 0)
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
- @db = Sequel.connect(DB_INFOS[:url], DB_INFOS[:options])
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
@@ -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
- FileUtils.mkdir(dir)
9
- FileUtils.cp_r(TEMPLATE_DIR + "new/", dir)
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
 
@@ -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
@@ -1,11 +1,5 @@
1
1
  class FastResponder < HK::Controller
2
2
 
3
- def favicon
4
- @favicon = HK::Application.favicon
5
- status @favicon.nil? ? 500 : 200
6
- @favicon
7
- end
8
-
9
3
  def on_exception
10
4
  e = request.env["hk.exception"]
11
5
  status 500
@@ -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
 
@@ -1,5 +1,5 @@
1
1
  module HK
2
2
 
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
 
5
5
  end ## HK
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.2
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-06-20 00:00:00.000000000 Z
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.25
164
+ rubygems_version: 1.8.24
164
165
  signing_key:
165
166
  specification_version: 3
166
167
  summary: Ruby web framework