padrino-core 0.2.9 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ begin
12
12
  gem.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
13
13
  gem.executables = ["padrino"]
14
14
  gem.add_runtime_dependency "sinatra", ">= 0.9.2"
15
+ gem.add_runtime_dependency "i18n", ">= 0.3.2"
15
16
  gem.add_runtime_dependency "thor", ">= 0.11.8"
16
17
  gem.add_development_dependency "haml", ">= 2.2.1"
17
18
  gem.add_runtime_dependency "bundler", ">= 0.5.0"
@@ -28,7 +29,7 @@ end
28
29
 
29
30
  require 'rake/testtask'
30
31
  Rake::TestTask.new(:test) do |test|
31
- test.libs << 'lib' << 'test'
32
+ test.libs << 'test'
32
33
  test.pattern = 'test/**/test_*.rb'
33
34
  test.verbose = true
34
35
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.9
1
+ 0.4.5
data/bin/padrino CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
  %w[rubygems thor].each { |gem| require gem }
3
- require File.dirname(__FILE__) + "/../lib/padrino-core/support_lite"
4
3
  require File.dirname(__FILE__) + "/../lib/padrino-core/tasks"
5
4
 
6
5
  arguments = ARGV.any? ? ARGV : ['-h']
@@ -28,16 +28,29 @@ module Padrino
28
28
  include(*extensions) if extensions.any?
29
29
  end
30
30
 
31
+ # With this method we can use layout like rails do or if a block given like sinatra
32
+ # By default we look in your/app/views/layouts/application.(haml|erb|etc)
33
+ #
34
+ # If you define:
35
+ #
36
+ # layout :custom
37
+ #
38
+ # Padrino look for your/app/views/layouts/custom.(haml|erb|etc)
39
+ def layout(name=:layout, &block)
40
+ return super if block_given?
41
+ @_layout = name
42
+ end
43
+
31
44
  # Reloads the application files from all defined load paths
32
45
  def reload!
33
46
  reset_routes! # remove all existing user-defined application routes
34
47
  Padrino.load_dependency(self.app_file) # reload the app file
35
- load_paths.each { |path| Padrino.load_dependencies(File.join(self.root, path)) }
48
+ load_paths.each { |path| Padrino.load_dependencies(File.join(self.root, path)) } # reload dependencies
36
49
  end
37
50
 
38
51
  # Resets application routes to only routes not defined by the user
39
52
  def reset_routes!
40
- @routes = Padrino::Application.dupe_routes
53
+ @routes = Padrino::Application.respond_to?(:dupe_routes) ? Padrino::Application.dupe_routes : {}
41
54
  end
42
55
 
43
56
  # Setup the application by registering initializers, load paths and logger
@@ -49,81 +62,134 @@ module Padrino
49
62
  self.register_initializers
50
63
  self.require_load_paths
51
64
  self.disable :logging # We need do that as default because Sinatra use commonlogger.
65
+ I18n.locale = self.locale
66
+ I18n.load_path += self.translations
67
+ self.get(""){ redirect("#{options.uri_root}/") } if self.uri_root != "/"
52
68
  @_configured = true
53
69
  end
54
70
 
55
71
  protected
72
+ # Defines default settings for Padrino application
73
+ def default_configuration!
74
+ # Overwriting Sinatra defaults
75
+ set :app_file, caller_files.first || $0 # Assume app file is first caller
76
+ set :environment, PADRINO_ENV.to_sym
77
+ set :raise_errors, true if development?
78
+ set :logging, false # !test?
79
+ set :sessions, true
80
+ # Padrino specific
81
+ set :uri_root, "/"
82
+ set :reload, development?
83
+ set :app_name, self.to_s.underscore.to_sym
84
+ set :default_builder, 'StandardFormBuilder'
85
+ set :flash, defined?(Rack::Flash)
86
+ set :authentication, false
87
+ # Padrino locale
88
+ set :locale, :en
89
+ set :translations, Proc.new { Dir[File.join(self.root, "/locale/**/*.{rb,yml}")] }
90
+ set :auto_locale, false
91
+ # Plugin specific
92
+ set :padrino_mailer, defined?(Padrino::Mailer)
93
+ set :padrino_helpers, defined?(Padrino::Helpers)
94
+ end
56
95
 
57
- # Defines default settings for Padrino application
58
- def default_configuration!
59
- # Overwriting Sinatra defaults
60
- set :app_file, caller_files.first || $0 # Assume app file is first caller
61
- set :environment, PADRINO_ENV.to_sym
62
- set :raise_errors, true if development?
63
- set :logging, false#!test?
64
- set :sessions, true
65
- # Padrino specific
66
- set :reload, development?
67
- set :app_name, self.to_s.underscore.to_sym
68
- set :default_builder, 'StandardFormBuilder'
69
- set :flash, defined?(Rack::Flash)
70
- # Plugin specific
71
- set :padrino_mailer, defined?(Padrino::Mailer)
72
- set :padrino_helpers, defined?(Padrino::Helpers)
73
- end
96
+ # Calculates any required paths after app_file and root have been properly configured
97
+ # Executes as part of the setup_application! method
98
+ def calculate_paths
99
+ raise ApplicationSetupError.new("Please define 'app_file' option for #{self.name} app!") unless self.app_file
100
+ set :views, find_view_path if find_view_path
101
+ set :images_path, File.join(self.public, "/images") unless self.respond_to?(:images_path)
102
+ end
74
103
 
75
- # Calculates any required paths after app_file and root have been properly configured
76
- # Executes as part of the setup_application! method
77
- def calculate_paths
78
- raise ApplicationSetupError.new("Please define 'app_file' option for #{self.name} app!") unless self.app_file
79
- set :views, find_view_path if find_view_path
80
- set :images_path, File.join(self.public, "/images") unless self.respond_to?(:images_path)
81
- end
104
+ # Requires the middleware and initializer modules to configure components
105
+ def register_initializers
106
+ use Padrino::RackLogger
107
+ use Padrino::Reloader if reload?
108
+ use Rack::Flash if flash?
109
+ register DatabaseSetup if defined?(DatabaseSetup)
110
+ @initializer_path ||= Padrino.root + '/config/initializers/*.rb'
111
+ Dir[@initializer_path].each { |file| register_initializer(file) }
112
+ end
82
113
 
83
- # Requires the middleware and initializer modules to configure components
84
- def register_initializers
85
- use Padrino::RackLogger
86
- use Padrino::Reloader if reload?
87
- use Rack::Flash if flash?
88
- register DatabaseSetup if defined?(DatabaseSetup)
89
- @initializer_path ||= Padrino.root + '/config/initializers/*.rb'
90
- Dir[@initializer_path].each { |file| register_initializer(file) }
91
- end
114
+ # Registers all desired padrino extension helpers/routing
115
+ def register_framework_extensions
116
+ register Padrino::Mailer if padrino_mailer?
117
+ register Padrino::Helpers if padrino_helpers?
118
+ register Padrino::AccessControl if authentication?
119
+ end
92
120
 
93
- # Registers all desired padrino extension helpers/routing
94
- def register_framework_extensions
95
- register Padrino::Mailer if padrino_mailer?
96
- register Padrino::Helpers if padrino_helpers?
97
- end
121
+ # Returns the load_paths for the application (relative to the application root)
122
+ def load_paths
123
+ @load_paths ||= ["urls.rb", "config/urls.rb", "models/*.rb", "mailers/*.rb", "controllers/**/*.rb", "helpers/*.rb"]
124
+ end
98
125
 
99
- # Requires all files within the application load paths
100
- def require_load_paths
101
- load_paths.each { |path| Padrino.require_dependencies(File.join(self.root, path)) }
102
- end
126
+ # Requires all files within the application load paths
127
+ def require_load_paths
128
+ load_paths.each { |path| Padrino.require_dependencies(File.join(self.root, path)) }
129
+ end
130
+
131
+ # Returns the path to the views directory from root by returning the first that is found
132
+ def find_view_path
133
+ @view_paths = ["views"].collect { |path| File.join(self.root, path) }
134
+ @view_paths.find { |path| Dir[File.join(path, '/**/*')].any? }
135
+ end
103
136
 
104
- # Returns the load_paths for the application (relative to the application root)
105
- def load_paths
106
- @load_paths ||= ["urls.rb", "config/urls.rb", "models/*.rb", "app/models/*.rb",
107
- "mailers/*.rb", "app/mailers/*.rb", "controllers/*.rb", "app/controllers/*.rb",
108
- "helpers/*.rb", "app/helpers/*.rb"]
137
+ # Registers an initializer with the application
138
+ # register_initializer('/path/to/initializer')
139
+ def register_initializer(file_path)
140
+ Padrino.require_dependencies(file_path)
141
+ file_class = File.basename(file_path, '.rb').camelize
142
+ register "#{file_class}Initializer".constantize
143
+ rescue NameError => e
144
+ logger.error "The module '#{file_class}Initializer' (#{file_path}) didn't loaded properly!"
145
+ logger.error " Initializer error was '#{e.message}'"
146
+ end
109
147
  end
110
148
 
111
- # Returns the path to the views directory from root by returning the first that is found
112
- def find_view_path
113
- @view_paths = ["views", "app/views"].collect { |path| File.join(self.root, path) }
114
- @view_paths.find { |path| Dir[File.join(path, '/**/*')].any? }
149
+ private
150
+ # Hijacking the sinatra render for do two thing:
151
+ #
152
+ # * Use layout like rails do
153
+ # * Use render 'path/to/my/template'
154
+ #
155
+ def render(engine, data=nil, options={}, locals={}, &block)
156
+ @template_cache.clear if Padrino.env != :production && @template_cache && @template_cache.respond_to?(:clear)
157
+ # If an engine is a string probably is a path so we try to resolve them
158
+ if data.nil?
159
+ data = engine.to_sym
160
+ engine = resolve_template_engine(engine)
161
+ end
162
+ # Use layout as rails do
163
+ if (options[:layout].nil? || options[:layout] == true) && !self.class.templates.has_key?(:layout)
164
+ layout = self.class.instance_variable_defined?(:@_layout) ? self.class.instance_variable_get(:@_layout) : :application
165
+ if layout
166
+ options[:layout] = File.join('layouts', layout.to_s).to_sym
167
+ logger.debug "Rendering layout #{options[:layout]}"
168
+ end
169
+ end
170
+ super
115
171
  end
116
172
 
117
- # Registers an initializer with the application
118
- # register_initializer('/path/to/initializer')
119
- def register_initializer(file_path)
120
- Padrino.require_dependencies(file_path)
121
- file_class = File.basename(file_path, '.rb').camelize
122
- register "#{file_class}Initializer".constantize
123
- rescue NameError => e
124
- logger.error "The module '#{file_class}Initializer' (#{file_path}) didn't loaded properly!"
125
- logger.error " Initializer error was '#{e.message}'"
173
+ # Returns the template engine (i.e haml) to use for a given template_path
174
+ # resolve_template_engine('users/new') => :haml
175
+ def resolve_template_engine(template_path)
176
+ resolved_template_path = File.join(self.options.views, template_path.to_s + ".*")
177
+ template_file = Dir[resolved_template_path].first
178
+ raise "Template path '#{template_path}' could not be located in views!" unless template_file
179
+ template_engine = File.extname(template_file)[1..-1].to_sym
180
+ end
181
+
182
+ # When we set :auto_locale, true then:
183
+ #
184
+ # * if we pass "/:locale/some/foo" we automatically set teh I18n locale to params[:locale]
185
+ # * if params[:locale] is empty we use the first HTTP_ACCEPT_LANGUAGE
186
+ def route_eval(&block)
187
+ if options.auto_locale
188
+ if params[:locale]
189
+ I18n.locale = params[:locale].to_sym rescue options.locale
190
+ end
191
+ end
192
+ super
126
193
  end
127
- end
128
194
  end
129
- end
195
+ end
@@ -1,6 +1,6 @@
1
1
  module Padrino
2
2
  PADRINO_IGNORE_CALLERS = [
3
- %r{lib/padrino-.*$}, # all padrino code
3
+ %r{lib/padrino-.*$}, # all padrino code
4
4
  %r{/padrino-.*/(lib|bin)}, # all padrino code
5
5
  %r{/bin/padrino$}, # all padrino code
6
6
  %r{/sinatra}, # all sinatra code
@@ -28,12 +28,6 @@ module Padrino
28
28
  Thread.current[:padrino_loaded]
29
29
  end
30
30
 
31
- # Attempts to require all dependencies with bundler; if this fails, uses system wide gems
32
- def load_required_gems
33
- load_bundler_manifest
34
- require_vendored_gems
35
- end
36
-
37
31
  # Attempts to require all dependency libs that we need.
38
32
  # If you use this method we can perform correctly a Padrino.reload!
39
33
  #
@@ -64,22 +58,19 @@ module Padrino
64
58
 
65
59
  protected
66
60
 
67
- # Loads the bundler manifest Gemfile if it exists
68
- def load_bundler_manifest
61
+ # Loads the vendored gems or system wide gems through Gemfile
62
+ def load_required_gems
63
+ require root('vendor', 'gems', 'environment')
64
+ Bundler.require_env(Padrino.env)
65
+ say! "=> Loaded bundled gems for #{Padrino.env} with #{Padrino.support.to_s.humanize}"
66
+ rescue LoadError
69
67
  require 'bundler'
70
- say "=> Locating Gemfile for #{PADRINO_ENV}"
71
- Bundler::Dsl.load_gemfile(root("Gemfile")).require_env(PADRINO_ENV)
72
- say " ... Loaded!"
73
- rescue Bundler::ManifestFileNotFound, Bundler::DefaultManifestNotFound => e
74
- say " ... Not Found"
75
- end
76
-
77
- # Require bundled gems if they exist
78
- def require_vendored_gems
79
- require_dependencies(root('/../vendor', 'gems', PADRINO_ENV))
80
- say! " (Loading bundled gems)"
81
- rescue LoadError => e
82
- say! " (Loading system gems)"
68
+ if File.exist?(root("Gemfile"))
69
+ Bundler::Dsl.load_gemfile(root("Gemfile")).require_env(Padrino.env)
70
+ say! "=> Located Gemfile for #{Padrino.env} with #{Padrino.support.to_s.humanize}"
71
+ else
72
+ say! "=> Gemfile for #{Padrino.env} not found!"
73
+ end
83
74
  end
84
75
 
85
76
  # Prints out a message to the stdout if not in test environment
@@ -0,0 +1,33 @@
1
+ en:
2
+ date:
3
+ formats:
4
+ # Use the strftime parameters for formats.
5
+ # When no format has been given, it uses default.
6
+ # You can provide other formats here if you like!
7
+ default: "%Y-%m-%d"
8
+ short: "%b %d"
9
+ long: "%B %d, %Y"
10
+
11
+ day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
12
+ abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
13
+
14
+ # Don't forget the nil at the beginning; there's no such thing as a 0th month
15
+ month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December]
16
+ abbr_month_names: [~, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
17
+ # Used in date_select and datime_select.
18
+ order: [ :year, :month, :day ]
19
+
20
+ time:
21
+ formats:
22
+ default: "%a, %d %b %Y %H:%M:%S %z"
23
+ short: "%d %b %H:%M"
24
+ long: "%B %d, %Y %H:%M"
25
+ am: "am"
26
+ pm: "pm"
27
+
28
+ # Used in array.to_sentence.
29
+ support:
30
+ array:
31
+ words_connector: ", "
32
+ two_words_connector: " and "
33
+ last_word_connector: ", and "
@@ -102,7 +102,7 @@ module Padrino
102
102
  @log.sync = true
103
103
  @mutex = @@mutex[@log] ||= Mutex.new
104
104
  @format_datetime = options[:format_datetime] || "%d/%b/%Y %H:%M:%S"
105
- @format_message = options[:format_message] || "%s - [%s] \"%s\""
105
+ @format_message = options[:format_message] || "%-5s - [%s] \"%s\""
106
106
  end
107
107
 
108
108
  # Flush the entire buffer to the log object.
@@ -59,7 +59,7 @@ module Padrino
59
59
 
60
60
  # Returns the root to the mounted apps base directory
61
61
  def mounted_root(*args)
62
- File.join(Padrino.root, @mounted_root ||= "apps", *args)
62
+ Padrino.root(@mounted_root ||= "", *args)
63
63
  end
64
64
 
65
65
  # Returns the mounted padrino applications (MountedApp objects)
@@ -79,7 +79,7 @@ module Padrino
79
79
  def mount_core(*args)
80
80
  options = args.extract_options!
81
81
  app_class = args.size > 0 ? args.first.to_s.camelize : nil
82
- options.reverse_merge!(:app_class => app_class, :app_file => Padrino.root('app/app.rb'), :app_root => Padrino.root)
82
+ options.reverse_merge!(:app_class => app_class, :app_file => Padrino.root("app", "app.rb"))
83
83
  mount("core", options).to("/")
84
84
  end
85
85
 
@@ -1,3 +1,5 @@
1
+ require 'pathname'
2
+
1
3
  module Padrino
2
4
  # What makes it especially suited for use in a any environment is that
3
5
  # any file will only be checked once and there will only be made one system
@@ -13,14 +13,27 @@ Required for Padrino to run:
13
13
  * Object#present?
14
14
  * Hash#slice, Hash#slice!
15
15
  * Hash#to_params
16
- * Hash#symbolize_keys
16
+ * Hash#symbolize_keys, Hash.symbolize_keys!
17
17
  * Hash#reverse_merge, Hash#reverse_merge!
18
18
  * SupportLite::OrderedHash
19
19
 
20
20
  =end
21
+ require 'i18n'
22
+ # Load our locales
23
+ I18n.load_path += Dir["#{File.dirname(__FILE__)}/padrino-core/locale/*.yml"]
24
+
25
+ module Padrino
26
+ # Return the current support used.
27
+ # Can be one of: :extlib, :active_support
28
+ def self.support
29
+ @_padrino_support
30
+ end
31
+ end
21
32
 
22
33
  if defined?(Extlib) # load if already using extlib
34
+ Padrino.instance_variable_set(:@_padrino_support, :extlib)
23
35
  require File.dirname(__FILE__) + '/support_lite/extlib_support'
24
36
  else # load active support by default
37
+ Padrino.instance_variable_set(:@_padrino_support, :active_support)
25
38
  require File.dirname(__FILE__) + '/support_lite/as_support'
26
- end
39
+ end
@@ -12,7 +12,9 @@ require 'active_support/core_ext/class/attribute_accessors' unless Class.method_
12
12
  require 'active_support/core_ext/hash' unless Hash.method_defined?(:reverse_merge)
13
13
  ## Hash#to_params
14
14
  require 'active_support/core_ext/object' unless Object.method_defined?(:to_query)
15
- class Hash; alias to_params to_query; end unless Hash.method_defined?(:to_params)
15
+ class Hash; alias :to_params :to_query; end unless Hash.method_defined?(:to_params)
16
+ ## Object#with_options
17
+ require 'active_support/option_merger'
16
18
  ## String#inflectors
17
19
  require 'active_support/inflector' unless String.method_defined?(:constantize)
18
20
  ## Object#blank?, Object#present?
@@ -21,6 +21,10 @@ unless Hash.method_defined?(:symbolize_keys)
21
21
  def symbolize_keys
22
22
  Mash.new(self).symbolize_keys
23
23
  end
24
+
25
+ def symbolize_keys!
26
+ self.replace(symbolize_keys.to_hash)
27
+ end
24
28
  end
25
29
  end
26
30
 
@@ -28,12 +32,36 @@ end
28
32
  unless Hash.method_defined?(:slice)
29
33
  require 'extlib/hash'
30
34
  class Hash
35
+ # Slice a hash to include only the given keys. This is useful for
36
+ # limiting an options hash to valid keys before passing to a method:
37
+ #
38
+ # def search(criteria = {})
39
+ # assert_valid_keys(:mass, :velocity, :time)
40
+ # end
41
+ #
42
+ # search(options.slice(:mass, :velocity, :time))
43
+ #
44
+ # If you have an array of keys you want to limit to, you should splat them:
45
+ #
46
+ # valid_keys = [:mass, :velocity, :time]
47
+ # search(options.slice(*valid_keys))
31
48
  def slice(*keys)
32
49
  keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
33
50
  hash = self.class.new
34
51
  keys.each { |k| hash[k] = self[k] if has_key?(k) }
35
52
  hash
36
53
  end
54
+
55
+ # Replaces the hash with only the given keys.
56
+ # Returns a hash contained the removed key/value pairs
57
+ # {:a => 1, :b => 2, :c => 3, :d => 4}.slice!(:a, :b) # => {:c => 3, :d =>4}
58
+ def slice!(*keys)
59
+ keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
60
+ omit = slice(*self.keys - keys)
61
+ hash = slice(*keys)
62
+ replace(hash)
63
+ omit
64
+ end
37
65
  end
38
66
  end
39
67
 
@@ -43,7 +71,7 @@ unless Hash.method_defined?(:to_params)
43
71
  end
44
72
 
45
73
  ## Hash#reverse_merge, Hash#reverse_merge!
46
- unless Hash.method_defined?(:present?)
74
+ unless Hash.method_defined?(:reverse_merge)
47
75
  class Hash
48
76
  def reverse_merge(other_hash)
49
77
  other_hash.merge(self)
@@ -52,6 +80,15 @@ unless Hash.method_defined?(:present?)
52
80
  def reverse_merge!(other_hash)
53
81
  replace(reverse_merge(other_hash))
54
82
  end
83
+
84
+ def deep_merge(other_hash)
85
+ target = dup
86
+ other_hash.each_pair do |k,v|
87
+ tv = target[k]
88
+ target[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
89
+ end
90
+ target
91
+ end
55
92
  end
56
93
  end
57
94
 
@@ -68,19 +105,22 @@ end
68
105
  unless String.method_defined?(:constantize)
69
106
  require 'extlib/inflection'
70
107
  class String
71
- def classify; Extlib::Inflection.classify(self); end
72
- def underscore; Extlib::Inflection.underscore(self); end
108
+ def classify; Extlib::Inflection.classify(self); end
109
+ def underscore; Extlib::Inflection.underscore(self); end
73
110
  def constantize; Extlib::Inflection.constantize(self); end
111
+ def camelize; Extlib::Inflection.camelize(self); end
112
+ def humanize; Extlib::Inflection.humanize(self); end
113
+ alias :titleize :humanize
74
114
  end
75
115
  end
76
116
 
77
117
  ## Object#blank?
78
- unless Array.method_defined?(:blank?)
118
+ unless Object.method_defined?(:blank?)
79
119
  require 'extlib/blank'
80
120
  end
81
121
 
82
122
  ## Object#present?
83
- unless Array.method_defined?(:present?)
123
+ unless Object.method_defined?(:present?)
84
124
  class Object
85
125
  def present?
86
126
  !blank?
@@ -88,6 +128,37 @@ unless Array.method_defined?(:present?)
88
128
  end
89
129
  end
90
130
 
131
+ ## Object#with_options
132
+ unless Object.method_defined?(:with_options)
133
+ class SupportLite::OptionMerger #:nodoc:
134
+ instance_methods.each do |method|
135
+ undef_method(method) if method !~ /^(__|instance_eval|class|object_id)/
136
+ end
137
+
138
+ def initialize(context, options)
139
+ @context, @options = context, options
140
+ end
141
+
142
+ private
143
+ def method_missing(method, *arguments, &block)
144
+ if arguments.last.is_a?(Proc)
145
+ proc = arguments.pop
146
+ arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) }
147
+ else
148
+ arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup)
149
+ end
150
+
151
+ @context.__send__(method, *arguments, &block)
152
+ end
153
+ end
154
+
155
+ class Object
156
+ def with_options(options)
157
+ yield SupportLite::OptionMerger.new(self, options)
158
+ end
159
+ end
160
+ end
161
+
91
162
  ## Module#alias_method_chain
92
163
  unless Module.method_defined?(:alias_method_chain)
93
164
  def alias_method_chain(target, feature)
@@ -78,6 +78,11 @@ module Padrino
78
78
  end
79
79
  options.boot
80
80
  end
81
+
82
+ protected
83
+ def self.banner(task)
84
+ "padrino-gen #{task.name}"
85
+ end
81
86
  end
82
87
  end
83
88
  end
data/padrino-core.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{padrino-core}
8
- s.version = "0.2.9"
8
+ s.version = "0.4.5"
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-12-22}
12
+ s.date = %q{2010-01-06}
13
13
  s.default_executable = %q{padrino}
14
14
  s.description = %q{The Padrino core gem required for use of this framework}
15
15
  s.email = %q{nesquena@gmail.com}
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/padrino-core/application.rb",
30
30
  "lib/padrino-core/caller.rb",
31
31
  "lib/padrino-core/loader.rb",
32
+ "lib/padrino-core/locale/en.yml",
32
33
  "lib/padrino-core/logger.rb",
33
34
  "lib/padrino-core/mounter.rb",
34
35
  "lib/padrino-core/reloader.rb",
@@ -51,9 +52,9 @@ Gem::Specification.new do |s|
51
52
  "test/fixtures/apps/simple.rb",
52
53
  "test/helper.rb",
53
54
  "test/test_application.rb",
55
+ "test/test_core.rb",
54
56
  "test/test_logger.rb",
55
- "test/test_padrino_core.rb",
56
- "test/test_padrino_mounter.rb",
57
+ "test/test_mounter.rb",
57
58
  "test/test_reloader_complex.rb",
58
59
  "test/test_reloader_simple.rb",
59
60
  "test/test_server.rb"
@@ -70,6 +71,7 @@ Gem::Specification.new do |s|
70
71
 
71
72
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
72
73
  s.add_runtime_dependency(%q<sinatra>, [">= 0.9.2"])
74
+ s.add_runtime_dependency(%q<i18n>, [">= 0.3.2"])
73
75
  s.add_runtime_dependency(%q<thor>, [">= 0.11.8"])
74
76
  s.add_development_dependency(%q<haml>, [">= 2.2.1"])
75
77
  s.add_runtime_dependency(%q<bundler>, [">= 0.5.0"])
@@ -79,6 +81,7 @@ Gem::Specification.new do |s|
79
81
  s.add_development_dependency(%q<webrat>, [">= 0.5.1"])
80
82
  else
81
83
  s.add_dependency(%q<sinatra>, [">= 0.9.2"])
84
+ s.add_dependency(%q<i18n>, [">= 0.3.2"])
82
85
  s.add_dependency(%q<thor>, [">= 0.11.8"])
83
86
  s.add_dependency(%q<haml>, [">= 2.2.1"])
84
87
  s.add_dependency(%q<bundler>, [">= 0.5.0"])
@@ -89,6 +92,7 @@ Gem::Specification.new do |s|
89
92
  end
90
93
  else
91
94
  s.add_dependency(%q<sinatra>, [">= 0.9.2"])
95
+ s.add_dependency(%q<i18n>, [">= 0.3.2"])
92
96
  s.add_dependency(%q<thor>, [">= 0.11.8"])
93
97
  s.add_dependency(%q<haml>, [">= 2.2.1"])
94
98
  s.add_dependency(%q<bundler>, [">= 0.5.0"])
@@ -1,9 +1,7 @@
1
1
  PADRINO_ROOT = File.dirname(__FILE__) unless defined? PADRINO_ROOT
2
2
 
3
3
  module LibDemo
4
- module_function
5
-
6
- def give_me_a_random
4
+ def self.give_me_a_random
7
5
  @rand ||= rand(100)
8
6
  end
9
7
  end
data/test/helper.rb CHANGED
@@ -23,7 +23,7 @@ module Kernel
23
23
  alias :silence_stdout :silence_logger
24
24
  end
25
25
 
26
- class Padrino::Application
26
+ class Class
27
27
  # Allow assertions in request context
28
28
  include Test::Unit::Assertions
29
29
  end
@@ -31,32 +31,23 @@ end
31
31
  class Test::Unit::TestCase
32
32
  include Rack::Test::Methods
33
33
 
34
- # Test App
35
- class PadrinoTestApp < Padrino::Application; end
36
-
37
34
  # Sets up a Sinatra::Base subclass defined with the block
38
35
  # given. Used in setup or individual spec methods to establish
39
36
  # the application.
40
- def mock_app(base=PadrinoTestApp, &block)
37
+ def mock_app(base=Padrino::Application, &block)
41
38
  @app = Sinatra.new(base, &block)
42
39
  end
43
-
40
+
44
41
  def app
45
42
  Rack::Lint.new(@app)
46
43
  end
47
44
 
48
- def stop_time_for_test
49
- time = Time.now
50
- Time.stubs(:now).returns(time)
51
- return time
52
- end
53
-
54
45
  # Asserts that a file matches the pattern
55
46
  def assert_match_in_file(pattern, file)
56
47
  assert File.exist?(file), "File '#{file}' does not exist!"
57
48
  assert_match pattern, File.read(file)
58
49
  end
59
-
50
+
60
51
  # Delegate other missing methods to response.
61
52
  def method_missing(name, *args, &block)
62
53
  if response && response.respond_to?(name)
@@ -2,10 +2,36 @@ require File.dirname(__FILE__) + '/helper'
2
2
 
3
3
  class TestApplication < Test::Unit::TestCase
4
4
 
5
+ def with_layout(name=:application)
6
+ # Build a temp layout
7
+ FileUtils.mkdir_p(File.dirname(__FILE__) + "/views/layouts")
8
+ layout = File.dirname(__FILE__) + "/views/layouts/#{name}.erb"
9
+ File.open(layout, 'wb') { |io| io.write "this is a <%= yield %>" }
10
+ yield
11
+ ensure
12
+ # Remove temp layout
13
+ File.unlink(layout) rescue nil
14
+ FileUtils.rm_rf(File.dirname(__FILE__) + "/views")
15
+ end
16
+
17
+ def with_view(name, content)
18
+ # Build a temp layout
19
+ FileUtils.mkdir_p(File.dirname(__FILE__) + "/views")
20
+ layout = File.dirname(__FILE__) + "/views/#{name}.erb"
21
+ File.open(layout, 'wb') { |io| io.write content }
22
+ yield
23
+ ensure
24
+ # Remove temp layout
25
+ File.unlink(layout) rescue nil
26
+ FileUtils.rm_rf(File.dirname(__FILE__) + "/views")
27
+ end
28
+
29
+ class PadrinoTestApp < Padrino::Application; end
30
+
5
31
  context 'for application functionality' do
6
32
 
7
33
  should 'check default options' do
8
- assert_match %r{test/helper.rb}, PadrinoTestApp.app_file
34
+ assert_match __FILE__, PadrinoTestApp.app_file
9
35
  assert_equal :test, PadrinoTestApp.environment
10
36
  assert_equal Padrino.root("views"), PadrinoTestApp.views
11
37
  assert PadrinoTestApp.raise_errors
@@ -27,4 +53,129 @@ class TestApplication < Test::Unit::TestCase
27
53
  assert !PadrinoTestApp.padrino_helpers
28
54
  end
29
55
  end
56
+
57
+ context 'for application layout functionality' do
58
+
59
+ should 'get no layout' do
60
+ mock_app do
61
+ get("/"){ "no layout" }
62
+ end
63
+
64
+ get "/"
65
+ assert_equal "no layout", body
66
+ end
67
+
68
+ should 'be compatible with sinatra layout' do
69
+ mock_app do
70
+ layout do
71
+ "this is a <%= yield %>"
72
+ end
73
+
74
+ get("/"){ render :erb, "sinatra layout" }
75
+ end
76
+
77
+ get "/"
78
+ assert_equal "this is a sinatra layout", body
79
+ end
80
+
81
+ should 'use rails way layout' do
82
+ with_layout do
83
+ mock_app do
84
+ get("/"){ render :erb, "rails way layout" }
85
+ end
86
+
87
+ get "/"
88
+ assert_equal "this is a rails way layout", body
89
+ end
90
+ end
91
+
92
+ should 'use rails way for a custom layout' do
93
+ with_layout :custom do
94
+ mock_app do
95
+ layout :custom
96
+ get("/"){ render :erb, "rails way custom layout" }
97
+ end
98
+
99
+ get "/"
100
+ assert_equal "this is a rails way custom layout", body
101
+ end
102
+ end
103
+ end
104
+
105
+ context 'for application render functionality' do
106
+
107
+ should 'be compatible with sinatra render' do
108
+ mock_app do
109
+ get("/"){ render :erb, "<%= 1+2 %>" }
110
+ end
111
+ get "/"
112
+ assert_equal "3", body
113
+ end
114
+
115
+ should 'be compatible with sinatra views' do
116
+ with_view :index, "<%= 1+2 %>" do
117
+ mock_app do
118
+ get("/foo") { render :erb, :index }
119
+ get("/bar") { erb :index }
120
+ get("/dir") { "3" }
121
+ get("/inj") { erb "<%= 2+1 %>" }
122
+ get("/rnj") { render :erb, "<%= 2+1 %>" }
123
+ end
124
+ get "/foo"
125
+ assert_equal "3", body
126
+ get "/bar"
127
+ assert_equal "3", body
128
+ get "/dir"
129
+ assert_equal "3", body
130
+ get "/inj"
131
+ assert_equal "3", body
132
+ get "/rnj"
133
+ assert_equal "3", body
134
+ end
135
+ end
136
+
137
+ should 'resolve template engine' do
138
+ with_view :index, "<%= 1+2 %>" do
139
+ mock_app do
140
+ get("/foo") { render :index }
141
+ get("/bar") { render "/index" }
142
+ end
143
+ get "/foo"
144
+ assert_equal "3", body
145
+ get "/bar"
146
+ assert_equal "3", body
147
+ end
148
+ end
149
+ end
150
+
151
+ context 'for application i18n functionality' do
152
+
153
+ should 'have a default locale en and auto_locale disabled' do
154
+ mock_app do
155
+ assert_equal :en, locale
156
+ assert !auto_locale
157
+ end
158
+ end
159
+
160
+ should 'change default locale from settings' do
161
+ mock_app do
162
+ set :locale, :it
163
+ enable :auto_locale
164
+ assert_equal :it, locale
165
+ assert auto_locale
166
+ end
167
+ end
168
+
169
+ should 'set locale when auto_locale is enabled' do
170
+ mock_app do
171
+ enable :auto_locale
172
+ get("/:locale"){ I18n.locale.to_s }
173
+ end
174
+
175
+ %w(it de fr).each do |lang|
176
+ get("/#{lang}")
177
+ assert_equal lang, body
178
+ end
179
+ end
180
+ end
30
181
  end
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- class TestPadrinoCore < Test::Unit::TestCase
3
+ class TestCore < Test::Unit::TestCase
4
4
 
5
5
  context 'for core functionality' do
6
6
 
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/helper'
2
2
 
3
- class TestPadrinoMounter < Test::Unit::TestCase
3
+ class TestMounter < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
6
  Padrino.mounted_apps.clear
@@ -27,7 +27,7 @@ class TestPadrinoMounter < Test::Unit::TestCase
27
27
  mounter.to("/test")
28
28
  assert_equal "test", mounter.name
29
29
  assert_equal "Test", mounter.app_class
30
- assert_match %r{test/apps/test/app.rb}, mounter.app_file
30
+ assert_match %r{test/app.rb}, mounter.app_file
31
31
  assert_equal "/test", mounter.uri_root
32
32
  assert_nil mounter.app_root
33
33
  end
@@ -46,7 +46,7 @@ class TestPadrinoMounter < Test::Unit::TestCase
46
46
  assert_equal Test, mounter.app_obj
47
47
  assert_equal Padrino.root('app/app.rb'), mounter.app_file
48
48
  assert_equal "/", mounter.uri_root
49
- assert_equal Padrino.root, mounter.app_root
49
+ assert_equal nil, mounter.app_root
50
50
  end
51
51
 
52
52
  should 'mount multiple apps' do
@@ -71,7 +71,7 @@ class TestPadrinoMounter < Test::Unit::TestCase
71
71
  Padrino.mounted_root = "apps"
72
72
  assert_equal Padrino.root("apps", "test", "app.rb"), Padrino.mounted_root("test", "app.rb")
73
73
  Padrino.mounted_root = nil
74
- assert_equal Padrino.root("apps", "test", "app.rb"), Padrino.mounted_root("test", "app.rb")
74
+ assert_equal Padrino.root("test", "app.rb"), Padrino.mounted_root("test", "app.rb")
75
75
  end
76
76
 
77
77
  should 'correctly instantiate a new padrino application' do
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.2.9
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2009-12-22 00:00:00 -08:00
15
+ date: 2010-01-06 00:00:00 +01:00
16
16
  default_executable: padrino
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -25,6 +25,16 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.9.2
27
27
  version:
28
+ - !ruby/object:Gem::Dependency
29
+ name: i18n
30
+ type: :runtime
31
+ version_requirement:
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 0.3.2
37
+ version:
28
38
  - !ruby/object:Gem::Dependency
29
39
  name: thor
30
40
  type: :runtime
@@ -115,6 +125,7 @@ files:
115
125
  - lib/padrino-core/application.rb
116
126
  - lib/padrino-core/caller.rb
117
127
  - lib/padrino-core/loader.rb
128
+ - lib/padrino-core/locale/en.yml
118
129
  - lib/padrino-core/logger.rb
119
130
  - lib/padrino-core/mounter.rb
120
131
  - lib/padrino-core/reloader.rb
@@ -137,9 +148,9 @@ files:
137
148
  - test/fixtures/apps/simple.rb
138
149
  - test/helper.rb
139
150
  - test/test_application.rb
151
+ - test/test_core.rb
140
152
  - test/test_logger.rb
141
- - test/test_padrino_core.rb
142
- - test/test_padrino_mounter.rb
153
+ - test/test_mounter.rb
143
154
  - test/test_reloader_complex.rb
144
155
  - test/test_reloader_simple.rb
145
156
  - test/test_server.rb