padrino-core 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ begin
13
13
  gem.add_runtime_dependency "sinatra", ">= 0.9.2"
14
14
  gem.add_runtime_dependency "thor", ">= 0.11.8"
15
15
  gem.add_development_dependency "haml", ">= 2.2.1"
16
- gem.add_development_dependency "shoulda", ">= 0"
16
+ gem.add_development_dependency "shoulda", ">= 0"
17
17
  gem.add_development_dependency "mocha", ">= 0.9.7"
18
18
  gem.add_development_dependency "rack-test", ">= 0.5.0"
19
19
  gem.add_development_dependency "webrat", ">= 0.5.1"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ %w[rubygems activesupport thor].each { |gem| require gem }
3
+ require File.dirname(__FILE__) + "/../lib/padrino-core/tasks"
4
+
5
+ arguments = ARGV.any? ? ARGV : ['-h']
6
+ Padrino::Tasks::Base.start(arguments)
@@ -5,6 +5,7 @@ Dir[File.dirname(__FILE__) + '/padrino-core/**/*.rb'].each {|file| require file
5
5
  PADRINO_ENV = ENV["PADRINO_ENV"] ||= ENV["RACK_ENV"] ||= "development" unless defined?(PADRINO_ENV)
6
6
 
7
7
  module Padrino
8
+
8
9
  # Helper method for file references.
9
10
  #
10
11
  # @param args [Array] Path components relative to ROOT_DIR.
@@ -13,18 +14,20 @@ module Padrino
13
14
  def self.root(*args)
14
15
  File.join(PADRINO_ROOT, *args)
15
16
  end
16
-
17
+
17
18
  # Returns the prepared rack application mapping each 'mounted' application
18
19
  def self.application
19
20
  Rack::Builder.new do
20
21
  Padrino.mounted_apps.each do |app|
22
+ require(app.app_file)
23
+ app_klass = app.klass.constantize
21
24
  map app.uri_root do
22
- require app.app_file
23
- app.klass.constantize.set :uri_root, app.uri_root
24
- run app.klass.constantize
25
+ app_klass.set :uri_root, app.uri_root
26
+ app_klass.set :app_file, app.app_file
27
+ run app_klass
25
28
  end
26
29
  end
27
30
  end
28
31
  end
29
-
32
+
30
33
  end
@@ -2,7 +2,7 @@ module Padrino
2
2
  # Subclasses of this become independent Padrino applications (stemming from Sinatra::Application)
3
3
  # These subclassed applications can be easily mounted into other Padrino applications as well.
4
4
  class Application < Sinatra::Application
5
-
5
+
6
6
  def logger
7
7
  @log_stream ||= self.class.log_to_file? ? Padrino.root("log/#{PADRINO_ENV.downcase}.log") : $stdout
8
8
  @logger ||= Logger.new(@log_stream)
@@ -26,19 +26,20 @@ module Padrino
26
26
  # Makes the routes defined in the block and in the Modules given
27
27
  # in `extensions` available to the application
28
28
  def controllers(*extensions, &block)
29
+ @routes = {} if reload? # This perform a basic reload controller
29
30
  instance_eval(&block) if block_given?
30
- include(*extensions) if extensions.any?
31
+ include(*extensions) if extensions.any?
31
32
  end
32
33
 
33
34
  # Makes the urls defined in the block and in the Modules given
34
35
  # in `extensions` available to the application
35
36
  def urls(*extensions, &block)
36
37
  instance_eval(&block) if block_given?
37
- include(*extensions) if extensions.any?
38
+ include(*extensions) if extensions.any?
38
39
  end
39
40
 
40
41
  protected
41
-
42
+
42
43
  # Setup the application by registering initializers, load paths and logger
43
44
  # Invoked automatically when an application instance is created
44
45
  def setup_application!
@@ -57,6 +58,7 @@ module Padrino
57
58
  set :logging, true
58
59
  set :sessions, true
59
60
  set :log_to_file, !development?
61
+ set :reload, development?
60
62
  # Padrino specific
61
63
  set :app_name, self.to_s.underscore.to_sym
62
64
  set :app_file, Padrino.mounted_root(self.app_name.to_s, "/app.rb")
@@ -72,6 +74,7 @@ module Padrino
72
74
  def register_initializers
73
75
  use Rack::Session::Cookie
74
76
  use Rack::Flash if flash?
77
+ use Padrino::Reloader if reload?
75
78
  Dir[Padrino.root + '/config/initializers/*.rb'].each do |file|
76
79
  Padrino.load_dependencies(file)
77
80
  file_class = File.basename(file, '.rb').classify
@@ -81,8 +84,9 @@ module Padrino
81
84
 
82
85
  # Registers all desired padrino extension helpers/routing
83
86
  def register_framework_extensions
84
- register Padrino::Helpers if padrino_helpers?
85
87
  register Padrino::Routing
88
+ register Padrino::Mailer
89
+ register Padrino::Helpers if padrino_helpers?
86
90
  end
87
91
 
88
92
  # Require all files within the application's load paths
@@ -3,10 +3,17 @@ module Padrino
3
3
  # Requires necessary dependencies as well as application files from root lib and models
4
4
  def load!
5
5
  load_required_gems # load bundler gems
6
- load_dependencies("#{root}/lib/**/*.rb", "#{root}/models/*.rb") # load root app dependencies
6
+ load_dependencies("#{root}/config/apps.rb", "#{root}/lib/**/*.rb", "#{root}/models/*.rb") # load root app dependencies
7
+ reload! # We need to fill our Stat::CACHE but we do that only for development
8
+ end
9
+
10
+ # Method for reload required classes
11
+ def reload!
12
+ Stat::reload!
7
13
  end
8
14
 
9
15
  # Attempts to load/require all dependency libs that we need.
16
+ # If you use this method we can perform correctly a Padrino.reload!
10
17
  #
11
18
  # @param paths [Array] Path where is necessary require or load a dependency
12
19
  # @example For load all our app libs we need to do:
@@ -16,23 +23,24 @@ module Padrino
16
23
  Dir[path].each { |file| require(file) }
17
24
  end
18
25
  end
19
-
26
+ alias_method :load_dependency, :load_dependencies
27
+
20
28
  # Attempts to require all dependencies with bundler; if fails, we try to use system wide gems
21
29
  def load_required_gems
22
30
  begin
23
31
  require 'bundler'
24
32
  gemfile_path = root("Gemfile")
25
- puts ">> Loading GemFile #{gemfile_path}"
33
+ puts "=> Loading GemFile #{gemfile_path} for #{PADRINO_ENV}"
26
34
  Bundler::Environment.load(gemfile_path).require_env(PADRINO_ENV)
27
35
  rescue Bundler::DefaultManifestNotFound => e
28
- puts ">> You didn't create Bundler Gemfile manifest or you are not in a Sinatra application."
36
+ puts "=> You didn't create Bundler Gemfile manifest or you are not in a Sinatra application."
29
37
  end
30
38
 
31
39
  begin
32
- require root('/../vendor', 'gems', PADRINO_ENV)
33
- puts ">> Using bundled gems"
40
+ load_dependencies(root('/../vendor', 'gems', PADRINO_ENV))
41
+ puts "=> Using bundled gems"
34
42
  rescue LoadError => e
35
- puts ">> Using system wide gems (No bundled gems)"
43
+ puts "=> Using system wide gems (No bundled gems)"
36
44
  end
37
45
  end
38
46
  end
@@ -1,17 +1,19 @@
1
1
  module Padrino
2
2
  # Represents a particular mounted padrino application
3
3
  # Stores the name of the application (app folder name) and url mount path
4
- # @example MountedApplication.new("blog_app").to("/blog")
5
- class MountedApplication
4
+ # @example Mounter.new("blog_app").to("/blog")
5
+ # @example Mounter.new("blog_app", :app_file => "/path/to/root/app.rb").to("/blog")
6
+ # @example Mounter.new("blog_app", :app_class => "Blog").to("/blog")
7
+ class Mounter
6
8
  attr_accessor :name, :uri_root, :app_file, :klass
7
- def initialize(name)
8
- @name = name
9
- @klass = name.classify
9
+ def initialize(name, options={})
10
+ @name = name
11
+ @klass = options[:app_class] || name.classify
12
+ @app_file = options[:app_file] || Padrino.mounted_root(name, 'app.rb')
10
13
  end
11
14
 
12
15
  # registers the mounted application to Padrino
13
16
  def to(mount_url)
14
- @app_file = Padrino.mounted_root(name, "app.rb")
15
17
  @uri_root = mount_url
16
18
  Padrino.mounted_apps << self
17
19
  end
@@ -30,8 +32,9 @@ module Padrino
30
32
 
31
33
  # Mounts a new sub-application onto Padrino
32
34
  # @example Padrino.mount("blog_app").to("/blog")
33
- def mount(name)
34
- MountedApplication.new(name)
35
+ # @example Padrino.mount("blog_app", :app_file => "/path/to/root/app.rb").to("/blog")
36
+ def mount(name, options={})
37
+ Mounter.new(name, options)
35
38
  end
36
39
  end
37
40
  end
@@ -0,0 +1,31 @@
1
+ module Padrino
2
+ # High performant source reloader
3
+ #
4
+ # This class acts as Rack middleware.
5
+ #
6
+ # It is performing a check/reload cycle at the start of every request, but
7
+ # also respects a cool down time, during which nothing will be done.
8
+ class Reloader
9
+
10
+ def initialize(app, cooldown = 1, backend = Stat)
11
+ @app = app
12
+ @cooldown = cooldown
13
+ @last = (Time.now - cooldown)
14
+ end
15
+
16
+ def call(env)
17
+ if @cooldown and Time.now > @last + @cooldown
18
+ if Thread.list.size > 1
19
+ Thread.exclusive { Padrino.reload! }
20
+ else
21
+ Padrino.reload!
22
+ end
23
+
24
+ @last = Time.now
25
+ end
26
+
27
+ @app.call(env)
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,74 @@
1
+ module Padrino
2
+ # What makes it especially suited for use in a any environment is that
3
+ # any file will only be checked once and there will only be made one system
4
+ # call stat(2).
5
+ #
6
+ # Please note that this will not reload files in the background, it does so
7
+ # only when actively called.
8
+ module Stat
9
+ class << self
10
+ CACHE = {}
11
+ MTIMES = {}
12
+
13
+ def reload!
14
+ rotation do |file, mtime|
15
+ previous_mtime = MTIMES[file] ||= mtime
16
+ safe_load(file, mtime) if mtime > previous_mtime
17
+ end
18
+ end
19
+
20
+ # A safe Kernel::load, issuing the hooks depending on the results
21
+ def safe_load(file, mtime)
22
+ puts "=> Reloading #{file}"
23
+ load(file)
24
+ file
25
+ rescue LoadError, SyntaxError => ex
26
+ $stderr.puts ex
27
+ ensure
28
+ MTIMES[file] = mtime
29
+ end
30
+
31
+ def rotation
32
+ files = [$0, *$LOADED_FEATURES].uniq
33
+ paths = ['./', *$LOAD_PATH].uniq
34
+
35
+ files.map{|file|
36
+ next if file =~ /\.(so|bundle)$/ # cannot reload compiled files
37
+
38
+ found, stat = figure_path(file, paths)
39
+ next unless found && stat && mtime = stat.mtime
40
+
41
+ CACHE[file] = found
42
+
43
+ yield(found, mtime)
44
+ }.compact
45
+ end
46
+
47
+ # Takes a relative or absolute +file+ name, a couple possible +paths+ that
48
+ # the +file+ might reside in. Returns the full path and File::Stat for the
49
+ # path.
50
+ def figure_path(file, paths)
51
+ found = CACHE[file]
52
+ found = file if !found and Pathname.new(file).absolute?
53
+ found, stat = safe_stat(found)
54
+ return found, stat if found
55
+
56
+ paths.find do |possible_path|
57
+ path = ::File.join(possible_path, file)
58
+ found, stat = safe_stat(path)
59
+ return ::File.expand_path(found), stat if found
60
+ end
61
+
62
+ return false, false
63
+ end
64
+
65
+ def safe_stat(file)
66
+ return unless file
67
+ stat = ::File.stat(file)
68
+ return file, stat if stat.file?
69
+ rescue Errno::ENOENT, Errno::ENOTDIR
70
+ CACHE.delete(file) and false
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,37 @@
1
+ require 'thor'
2
+
3
+ module Padrino
4
+ module Tasks
5
+ class Base < Thor
6
+ include Thor::Actions
7
+
8
+ desc "start", "Starts the Padrino application"
9
+ def start
10
+ say "Starting the Padrino application from root #{destination_root}"
11
+ end
12
+
13
+ desc "stop", "Stops the Padrino application"
14
+ def stop
15
+ say "Stopping the Padrino application"
16
+ end
17
+
18
+ desc "test", "Executes all the Padrino test files"
19
+ def test
20
+ say "Executing Padrino test files"
21
+ end
22
+
23
+ desc "console ENVIRONMENT", "Boots up the Padrino application irb console"
24
+ def console(environment="development")
25
+ require File.dirname(__FILE__) + "/version.rb"
26
+ raise "Are you in a Padrino Project? We didn't find config/boot.rb !!!" unless File.exist?("config/boot.rb")
27
+ ENV["PADRINO_ENV"] ||= environment
28
+ puts "=> Loading #{environment} console (Padrino v.#{Padrino.version})"
29
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
30
+ libs = " -r irb/completion"
31
+ libs << " -r config/boot"
32
+ libs << " -r #{File.dirname(__FILE__)}/tasks/console"
33
+ exec "#{irb} #{libs} --simple-prompt"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,21 @@
1
+ # Reloads classes
2
+ def reload!
3
+ Padrino.reload!
4
+ true
5
+ end
6
+
7
+ # Show applications
8
+ def applications
9
+ puts "==== List of Mounted Applications ====\n\n"
10
+ Padrino.mounted_apps.each do |app|
11
+ puts " * '#{app.name}' mapped to '#{app.uri_root}'"
12
+ end
13
+ puts
14
+ Padrino.mounted_apps.collect { |app| "#{app.name} => #{app.uri_root}" }
15
+ end
16
+
17
+ # Load apps
18
+ Padrino.mounted_apps.each do |app|
19
+ puts "=> Loading Application #{app.name}"
20
+ Padrino.load_dependency(app.app_file)
21
+ end
@@ -0,0 +1,7 @@
1
+ # Manages Padrino version from the VERSION file managed by Jeweler
2
+
3
+ module Padrino
4
+ def self.version
5
+ @version ||= File.read(File.dirname(__FILE__) + '/../../VERSION').chomp
6
+ end
7
+ end
@@ -5,13 +5,15 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{padrino-core}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
12
- s.date = %q{2009-11-16}
12
+ s.date = %q{2009-11-17}
13
+ s.default_executable = %q{padrino}
13
14
  s.description = %q{The Padrino core gem required for use of this framework}
14
15
  s.email = %q{nesquena@gmail.com}
16
+ s.executables = ["padrino"]
15
17
  s.extra_rdoc_files = [
16
18
  "LICENSE",
17
19
  "README.rdoc"
@@ -21,34 +23,35 @@ Gem::Specification.new do |s|
21
23
  ".gitignore",
22
24
  "LICENSE",
23
25
  "README.rdoc",
26
+ "README.rdoc",
27
+ "Rakefile",
24
28
  "Rakefile",
25
29
  "VERSION",
30
+ "bin/padrino",
26
31
  "lib/padrino-core.rb",
27
32
  "lib/padrino-core/application.rb",
28
33
  "lib/padrino-core/loader.rb",
29
34
  "lib/padrino-core/mounter.rb",
35
+ "lib/padrino-core/reloader.rb",
36
+ "lib/padrino-core/stat.rb",
30
37
  "lib/padrino-core/support_lite.rb",
38
+ "lib/padrino-core/tasks.rb",
39
+ "lib/padrino-core/tasks/console.rb",
40
+ "lib/padrino-core/version.rb",
31
41
  "padrino-core.gemspec",
32
42
  "test/active_support_helpers.rb",
33
43
  "test/fixtures/extended_app/app.rb",
34
44
  "test/fixtures/simple_app/app.rb",
35
45
  "test/helper.rb",
36
46
  "test/test_padrino_core.rb",
37
- "test/test_padrino_mounting.rb"
47
+ "test/test_padrino_mounting.rb",
48
+ "test/test_padrino_tasks.rb"
38
49
  ]
39
50
  s.homepage = %q{http://github.com/padrino/padrino-core}
40
51
  s.rdoc_options = ["--charset=UTF-8"]
41
52
  s.require_paths = ["lib"]
42
53
  s.rubygems_version = %q{1.3.5}
43
54
  s.summary = %q{The required Padrino core gem}
44
- s.test_files = [
45
- "test/active_support_helpers.rb",
46
- "test/fixtures/extended_app/app.rb",
47
- "test/fixtures/simple_app/app.rb",
48
- "test/helper.rb",
49
- "test/test_padrino_core.rb",
50
- "test/test_padrino_mounting.rb"
51
- ]
52
55
 
53
56
  if s.respond_to? :specification_version then
54
57
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -1,7 +1,7 @@
1
1
  require 'sinatra/base'
2
2
  require 'haml'
3
3
 
4
- PADRINO_ROOT = File.dirname(__FILE__)
4
+ # PADRINO_ROOT = File.dirname(__FILE__)
5
5
 
6
6
  class ExtendedCoreDemo < Padrino::Application
7
7
  configure do
@@ -1,7 +1,7 @@
1
1
  require 'sinatra/base'
2
2
  require 'haml'
3
3
 
4
- PADRINO_ROOT = File.dirname(__FILE__)
4
+ # PADRINO_ROOT = File.dirname(__FILE__)
5
5
 
6
6
  class SimpleCoreDemo < Padrino::Application
7
7
  configure do
@@ -8,8 +8,8 @@ require 'webrat'
8
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
9
  $LOAD_PATH.unshift(File.dirname(__FILE__))
10
10
  require 'active_support_helpers'
11
- require File.dirname(__FILE__) + '/../../padrino-helpers/lib/padrino-helpers.rb'
12
- require File.dirname(__FILE__) + '/../lib/padrino-core.rb'
11
+ require 'padrino-helpers'
12
+ require 'padrino-core'
13
13
 
14
14
  class Test::Unit::TestCase
15
15
  include Padrino::Helpers::OutputHelpers
@@ -1,5 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
- require File.dirname(__FILE__) + '/fixtures/simple_app/app'
2
+ # require File.dirname(__FILE__) + '/fixtures/simple_app/app'
3
3
 
4
4
  class TestPadrinoCore < Test::Unit::TestCase
5
5
  context 'for core functionality' do
@@ -1,5 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
- require File.dirname(__FILE__) + '/fixtures/simple_app/app'
2
+ # require File.dirname(__FILE__) + '/fixtures/simple_app/app'
3
3
 
4
4
  class TestPadrinoMounting < Test::Unit::TestCase
5
5
  context 'for mounting applications functionality' do
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ # require File.dirname(__FILE__) + '/fixtures/simple_app/app'
3
+
4
+ class TestPadrinoTasks < Test::Unit::TestCase
5
+ context 'for padrino command line tasks' do
6
+
7
+ end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -12,8 +12,8 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2009-11-16 00:00:00 -08:00
16
- default_executable:
15
+ date: 2009-11-17 00:00:00 -08:00
16
+ default_executable: padrino
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: sinatra
@@ -87,8 +87,8 @@ dependencies:
87
87
  version:
88
88
  description: The Padrino core gem required for use of this framework
89
89
  email: nesquena@gmail.com
90
- executables: []
91
-
90
+ executables:
91
+ - padrino
92
92
  extensions: []
93
93
 
94
94
  extra_rdoc_files:
@@ -101,11 +101,17 @@ files:
101
101
  - README.rdoc
102
102
  - Rakefile
103
103
  - VERSION
104
+ - bin/padrino
104
105
  - lib/padrino-core.rb
105
106
  - lib/padrino-core/application.rb
106
107
  - lib/padrino-core/loader.rb
107
108
  - lib/padrino-core/mounter.rb
109
+ - lib/padrino-core/reloader.rb
110
+ - lib/padrino-core/stat.rb
108
111
  - lib/padrino-core/support_lite.rb
112
+ - lib/padrino-core/tasks.rb
113
+ - lib/padrino-core/tasks/console.rb
114
+ - lib/padrino-core/version.rb
109
115
  - padrino-core.gemspec
110
116
  - test/active_support_helpers.rb
111
117
  - test/fixtures/extended_app/app.rb
@@ -113,6 +119,7 @@ files:
113
119
  - test/helper.rb
114
120
  - test/test_padrino_core.rb
115
121
  - test/test_padrino_mounting.rb
122
+ - test/test_padrino_tasks.rb
116
123
  has_rdoc: true
117
124
  homepage: http://github.com/padrino/padrino-core
118
125
  licenses: []
@@ -141,10 +148,5 @@ rubygems_version: 1.3.5
141
148
  signing_key:
142
149
  specification_version: 3
143
150
  summary: The required Padrino core gem
144
- test_files:
145
- - test/active_support_helpers.rb
146
- - test/fixtures/extended_app/app.rb
147
- - test/fixtures/simple_app/app.rb
148
- - test/helper.rb
149
- - test/test_padrino_core.rb
150
- - test/test_padrino_mounting.rb
151
+ test_files: []
152
+