padrino-core 0.9.13 → 0.9.14

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/bin/padrino CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'rubygems' unless defined?(Gem)
2
3
 
3
4
  # We load Padrino libs
4
5
  padrino_core_path = File.expand_path('../../lib', __FILE__)
@@ -15,12 +15,12 @@ module Padrino
15
15
  attr_accessor :name, :uri_root, :app_file, :app_class, :app_root, :app_obj, :app_host
16
16
 
17
17
  def initialize(name, options={})
18
- @name = name.underscore
19
- @app_class = options[:app_class] || name.classify
18
+ @name = name.to_s.underscore
19
+ @app_class = options[:app_class] || @name.camelize
20
20
  @app_file = options[:app_file] || locate_app_file
21
- raise MounterException, "Unable to locate app file for #{name}, try with :app_file => /path" unless @app_file
21
+ @app_obj = options[:app_obj] || app_constant || locate_app_object
22
+ ensure_app_file! || ensure_app_object!
22
23
  @app_root = options[:app_root] || File.dirname(@app_file)
23
- @app_obj = self.app_object
24
24
  @uri_root = "/"
25
25
  end
26
26
 
@@ -67,20 +67,47 @@ module Padrino
67
67
  app_obj.set :public, Padrino.root('public', app_data.uri_root)
68
68
  app_obj.set :static, File.exist?(app_obj.public)
69
69
  app_obj.setup_application! # We need to initialize here the app.
70
- router.map(:path => app_data.uri_root, :to => app_obj, :host => app_data.app_host)
70
+ router.map(:to => app_obj, :path => app_data.uri_root, :host => app_data.app_host)
71
71
  end
72
72
 
73
+ ###
74
+ # Returns the route objects for the mounted application
75
+ #
76
+ def routes
77
+ app_obj.routes
78
+ end
79
+
80
+ ###
81
+ # Returns the basic route information for each named route
82
+ #
83
+ #
84
+ def named_routes
85
+ app_obj.routes.map { |route|
86
+ name_array = "(#{route.named.to_s.split("_").map { |piece| %Q[:#{piece}] }.join(", ")})"
87
+ request_method = route.as_options[:conditions][:request_method][0]
88
+ full_path = File.join(self.uri_root, route.path)
89
+ next if route.named.blank? || request_method == 'HEAD'
90
+ OpenStruct.new(:verb => request_method, :identifier => route.named, :name => name_array, :path => full_path)
91
+ }.compact
92
+ end
93
+
94
+ ##
95
+ # Makes two Mounters equal if they have the same name and uri_root
96
+ #
97
+ def ==(other)
98
+ other.is_a?(Mounter) && self.app_class == other.app_class && self.uri_root == other.uri_root
99
+ end
100
+
101
+ protected
102
+
73
103
  ##
74
- # Return the class for the app
75
- #
76
- def app_object
77
- begin
78
- app_class.constantize
79
- rescue
80
- if app_file
81
- Padrino.require_dependencies(app_file)
82
- app_class.constantize
83
- end
104
+ # Locates and requires the file to load the app constant
105
+ #
106
+ def locate_app_object
107
+ @_app_object ||= begin
108
+ ensure_app_file!
109
+ Padrino.require_dependencies(app_file)
110
+ app_constant
84
111
  end
85
112
  end
86
113
 
@@ -89,18 +116,34 @@ module Padrino
89
116
  #
90
117
  def locate_app_file
91
118
  candidates = []
92
- candidates << app_object.app_file if app_object.respond_to?(:app_file) && File.exist?(app_object.app_file)
119
+ candidates << app_constant.app_file if app_constant.respond_to?(:app_file) && File.exist?(app_constant.app_file.to_s)
93
120
  candidates << Padrino.first_caller if File.identical?(Padrino.first_caller.to_s, Padrino.called_from.to_s)
94
121
  candidates << Padrino.mounted_root(name, "app.rb")
95
122
  candidates << Padrino.root("app", "app.rb")
96
123
  candidates.find { |candidate| File.exist?(candidate) }
97
124
  end
98
-
125
+
99
126
  ##
100
- # Makes two Mounters equal if they have the same name and uri_root
127
+ # Returns the class object for the app if defined, nil otherwise
101
128
  #
102
- def ==(other)
103
- other.is_a?(Mounter) && self.name == other.name && self.uri_root == other.uri_root
129
+ def app_constant
130
+ app_class.constantize if Object.const_defined?(app_class)
131
+ end
132
+
133
+ ###
134
+ # Raises an exception unless app_file is located properly
135
+ #
136
+ def ensure_app_file!
137
+ message = "Unable to locate source file for app '#{name}', try with :app_file => '/path/app.rb'"
138
+ raise MounterException, message unless @app_file
139
+ end
140
+
141
+ ###
142
+ # Raises an exception unless app_obj is defined properly
143
+ #
144
+ def ensure_app_object!
145
+ message = "Unable to locate app for '#{name}', try with :app_class => 'MyAppClass'"
146
+ raise MounterException, message unless @app_obj
104
147
  end
105
148
  end
106
149
 
@@ -125,8 +168,7 @@ module Padrino
125
168
  # Inserts a Mounter object into the mounted applications (avoids duplicates)
126
169
  #
127
170
  def insert_mounted_app(mounter)
128
- return false if Padrino.mounted_apps.include?(mounter)
129
- Padrino.mounted_apps << mounter
171
+ Padrino.mounted_apps.push(mounter) unless Padrino.mounted_apps.include?(mounter)
130
172
  end
131
173
 
132
174
  ##
@@ -138,7 +180,7 @@ module Padrino
138
180
  # Padrino.mount_core(:app_file => "/path/to/file", :app_class => "Blog")
139
181
  #
140
182
  def mount_core(*args)
141
- # TODO Remove this in 0.9.13 or before 1.0
183
+ # TODO Remove this in 0.9.14 or pre 1.0
142
184
  warn "DEPRECATION! #{Padrino.first_caller}: Padrino.mount_core has been deprecated.\nUse Padrino.mount('AppName').to('/') instead"
143
185
  options = args.extract_options!
144
186
  app_class = args.size > 0 ? args.first.to_s.camelize : nil
@@ -68,7 +68,11 @@ module Padrino
68
68
  end
69
69
 
70
70
  ##
71
- # Returns the routes of our app
71
+ # Returns the routes of our app.
72
+ #
73
+ # ==== Examples
74
+ #
75
+ # MyApp.routes
72
76
  #
73
77
  def routes
74
78
  router.routes
@@ -15,6 +15,6 @@ end
15
15
 
16
16
  # Load apps
17
17
  Padrino.mounted_apps.each do |app|
18
- puts "=> Loading Application #{app.app_object}"
19
- app.app_object.setup_application!
18
+ puts "=> Loading Application #{app.app_class}"
19
+ app.app_obj.setup_application!
20
20
  end
@@ -23,29 +23,37 @@ Dir["lib/tasks/**/*.rake"].
23
23
  # setting up the required environment for Padrino
24
24
  task :environment do
25
25
  Padrino.mounted_apps.each do |app|
26
- app.app_object.setup_application!
26
+ app.app_obj.setup_application!
27
27
  end
28
28
  end
29
29
 
30
- desc "Displays a listing of the named routes within a project"
30
+ # lists all routes of a given app
31
+ def list_app_routes(app, args)
32
+ app_routes = app.named_routes
33
+ app_routes.reject! { |r| r.identifier.to_s !~ /#{args.query}/ } if args.query.present?
34
+ app_routes.map! { |r| [r.verb, r.name, r.path] }
35
+ return if app_routes.empty?
36
+ shell.say "\nApplication: #{app.app_class}", :yellow
37
+ app_routes.unshift(["REQUEST", "URL", "PATH"])
38
+ max_col_1 = app_routes.max { |a, b| a[0].size <=> b[0].size }[0].size
39
+ max_col_2 = app_routes.max { |a, b| a[1].size <=> b[1].size }[1].size
40
+ app_routes.each_with_index do |row, i|
41
+ message = [row[1].ljust(max_col_2+2), row[0].center(max_col_1+2), row[2]]
42
+ shell.say(" " + message.join(" "), i==0 ? :bold : nil)
43
+ end
44
+ end
45
+
46
+ desc "Displays a listing of the named routes within a project, optionally only those matched by [query]"
31
47
  task :routes, :query, :needs => :environment do |t, args|
32
48
  Padrino.mounted_apps.each do |app|
33
- app_routes = app.app_object.router.routes
34
- app_routes.reject! { |r| r.named.blank? || r.as_options[:conditions][:request_method].try(:first) == 'HEAD' }
35
- app_routes.reject! { |r| r.named.to_s !~ /#{args.query}/ } if args.query.present?
36
- next if app_routes.empty?
37
- shell.say "\nApplication: #{app.name}", :yellow
38
- app_routes.map! do |route|
39
- name_string = "(#{route.named.to_s.split("_").map { |piece| %Q[:#{piece}] }.join(", ")})"
40
- request_method = route.as_options[:conditions][:request_method].try(:first)
41
- [request_method, name_string, route.path]
42
- end
43
- app_routes.unshift(["REQUEST", "URL", "PATH"])
44
- max_col_1 = app_routes.max { |a, b| a[0].size <=> b[0].size }[0].size
45
- max_col_2 = app_routes.max { |a, b| a[1].size <=> b[1].size }[1].size
46
- app_routes.each_with_index do |row, i|
47
- message = [row[1].ljust(max_col_2+2), row[0].center(max_col_1+2), row[2]]
48
- shell.say(" " + message.join(" "), i==0 ? :bold : nil)
49
- end
49
+ list_app_routes(app, args)
50
+ end
51
+ end
52
+
53
+ desc "Displays a listing of the named routes a given app [app]"
54
+ namespace :routes do
55
+ task :app, :app, :needs => :environment do |t, args|
56
+ app = Padrino.mounted_apps.find { |app| app.app_class == args.app }
57
+ list_app_routes(app, args) if app
50
58
  end
51
59
  end
@@ -84,7 +84,7 @@ module Padrino
84
84
  # If the file is related to their app (i.e. a controller/mailer/helper)
85
85
  if app = Padrino.mounted_apps.find { |a| file =~ /^#{File.dirname(a.app_file)}/ }
86
86
  # We need to reload their own app
87
- app.app_object.reload!
87
+ app.app_obj.reload!
88
88
  # App reloading will also perform safe_load of itself so we can go next
89
89
  if File.identical?(app.app_file, file)
90
90
  MTIMES[file] = mtime # This prevent a loop
@@ -5,7 +5,7 @@
5
5
  # without include full padrino core.
6
6
  #
7
7
  module Padrino
8
- VERSION = '0.9.13' unless defined?(Padrino::VERSION)
8
+ VERSION = '0.9.14' unless defined?(Padrino::VERSION)
9
9
  ##
10
10
  # Return the current Padrino version
11
11
  #
@@ -21,7 +21,7 @@ Complex1Demo.controllers do
21
21
  end
22
22
 
23
23
  Complex2Demo.controllers do
24
- get("/"){ "The magick number is: 62!" } # Change only the number!!!
24
+ get("/"){ "The magick number is: 16!" } # Change only the number!!!
25
25
  end
26
26
 
27
27
  Padrino.load!
@@ -3,6 +3,10 @@ require File.expand_path(File.dirname(__FILE__) + '/helper')
3
3
  class PadrinoTestApp < Padrino::Application; end
4
4
 
5
5
  class TestApplication < Test::Unit::TestCase
6
+ def setup
7
+ Padrino.mounted_apps.clear
8
+ end
9
+
6
10
  def teardown
7
11
  remove_views
8
12
  end
@@ -29,7 +33,7 @@ class TestApplication < Test::Unit::TestCase
29
33
  assert !PadrinoTestApp.flash
30
34
  end
31
35
 
32
- #compare to: test_routing: allow global provides
36
+ # compare to: test_routing: allow global provides
33
37
  should "set content_type to :html if none can be determined" do
34
38
  mock_app do
35
39
  provides :xml
@@ -46,6 +50,5 @@ class TestApplication < Test::Unit::TestCase
46
50
  get '/bar', {}, { 'HTTP_ACCEPT' => 'application/xml' }
47
51
  assert_equal "Foo in html", body
48
52
  end
49
-
50
53
  end
51
54
  end
data/test/test_mounter.rb CHANGED
@@ -39,7 +39,7 @@ class TestMounter < Test::Unit::TestCase
39
39
  assert_equal ["an_app"], Padrino.mounted_apps.collect(&:name)
40
40
  end
41
41
 
42
- should 'mount a core' do
42
+ should 'mount a primary app to root uri' do
43
43
  mounter = Padrino.mount("test", :app_file => __FILE__).to("/")
44
44
  assert_equal "test", mounter.name
45
45
  assert_equal "Test", mounter.app_class
@@ -49,7 +49,7 @@ class TestMounter < Test::Unit::TestCase
49
49
  assert_equal File.dirname(mounter.app_file), mounter.app_root
50
50
  end
51
51
 
52
- should 'mount a core to url' do
52
+ should 'mount a primary app to sub_uri' do
53
53
  mounter = Padrino.mount("test", :app_file => __FILE__).to('/me')
54
54
  assert_equal "test", mounter.name
55
55
  assert_equal "Test", mounter.app_class
@@ -59,6 +59,16 @@ class TestMounter < Test::Unit::TestCase
59
59
  assert_equal File.dirname(mounter.app_file), mounter.app_root
60
60
  end
61
61
 
62
+ should "raise error when app has no located file" do
63
+ assert_raise(Padrino::Mounter::MounterException) { Padrino.mount("tester_app").to('/test') }
64
+ assert_equal 0, Padrino.mounted_apps.size
65
+ end
66
+
67
+ should "raise error when app has no located object" do
68
+ assert_raise(Padrino::Mounter::MounterException) { Padrino.mount("tester_app", :app_file => "/path/to/file.rb").to('/test') }
69
+ assert_equal 0, Padrino.mounted_apps.size
70
+ end
71
+
62
72
  should 'mount multiple apps' do
63
73
  class ::OneApp < Padrino::Application; end
64
74
  class ::TwoApp < Padrino::Application; end
@@ -84,6 +94,47 @@ class TestMounter < Test::Unit::TestCase
84
94
  assert_equal Padrino.root("test", "app.rb"), Padrino.mounted_root("test", "app.rb")
85
95
  end
86
96
 
97
+ should "be able to access routes data for mounted apps" do
98
+ class ::OneApp < Padrino::Application
99
+ get("/test") { "test" }
100
+ get(:index, :provides => [:js, :json]) { "index" }
101
+ controllers :posts do
102
+ get(:index) { "index" }
103
+ get(:new, :provides => :js) { "new" }
104
+ get(:show, :provides => [:js, :html], :with => :id) { "show" }
105
+ post(:create, :provides => :js, :with => :id) { "create" }
106
+ end
107
+ end
108
+ class ::TwoApp < Padrino::Application
109
+ controllers :users do
110
+ get(:index) { "users" }
111
+ get(:new) { "users new" }
112
+ post(:create) { "users create" }
113
+ put(:update) { "users update" }
114
+ delete(:destroy) { "users delete" }
115
+ end
116
+ end
117
+
118
+ Padrino.mount("one_app").to("/")
119
+ Padrino.mount("two_app").to("/two_app")
120
+
121
+ assert_equal 11, Padrino.mounted_apps[0].routes.size
122
+ assert_equal 7, Padrino.mounted_apps[1].routes.size
123
+ assert_equal 5, Padrino.mounted_apps[0].named_routes.size
124
+ assert_equal 5, Padrino.mounted_apps[1].named_routes.size
125
+
126
+ first_route = Padrino.mounted_apps[0].named_routes[3]
127
+ assert_equal "posts_show", first_route.identifier.to_s
128
+ assert_equal "(:posts, :show)", first_route.name
129
+ assert_equal "GET", first_route.verb
130
+ assert_equal "/posts/show/:id(.:format)", first_route.path
131
+ another_route = Padrino.mounted_apps[1].named_routes[2]
132
+ assert_equal "users_create", another_route.identifier.to_s
133
+ assert_equal "(:users, :create)", another_route.name
134
+ assert_equal "POST", another_route.verb
135
+ assert_equal "/two_app/users/create", another_route.path
136
+ end
137
+
87
138
  should 'correctly instantiate a new padrino application' do
88
139
  mock_app do
89
140
  get("/demo_1"){ "Im Demo 1" }
@@ -96,4 +147,4 @@ class TestMounter < Test::Unit::TestCase
96
147
  assert_equal "Im Demo 2", body
97
148
  end
98
149
  end
99
- end
150
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-core
3
3
  version: !ruby/object:Gem::Version
4
- hash: 33
4
+ hash: 39
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 13
10
- version: 0.9.13
9
+ - 14
10
+ version: 0.9.14
11
11
  platform: ruby
12
12
  authors:
13
13
  - Padrino Team
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2010-06-19 00:00:00 +02:00
21
+ date: 2010-06-30 00:00:00 -07:00
22
22
  default_executable: padrino
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency
@@ -43,12 +43,12 @@ dependencies:
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- hash: 29
46
+ hash: 19
47
47
  segments:
48
48
  - 0
49
- - 2
50
- - 5
51
- version: 0.2.5
49
+ - 3
50
+ - 0
51
+ version: 0.3.0
52
52
  type: :runtime
53
53
  name: http_router
54
54
  prerelease: false