merb 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -49,7 +49,7 @@ module Merb
49
49
  #
50
50
  def render(opts={}, &blk)
51
51
  action = opts[:action] || params[:action]
52
- opts[:layout] ||= ancestral_trait[:layout]
52
+ opts[:layout] ||= _layout
53
53
 
54
54
  case
55
55
  when status = opts[:nothing]
@@ -161,12 +161,12 @@ module Merb
161
161
  if action = opts[:action]
162
162
  path =
163
163
  File.expand_path(MERB_ROOT / "dist/app/views" / self.class.name.snake_case / action)
164
- elsif layout = opts[:layout]
165
- path = ancestral_trait[:layout_root] / layout
164
+ elsif _layout = opts[:layout]
165
+ path = _layout_root / _layout
166
166
  else
167
167
  raise "called find_template without an :action or :layout"
168
168
  end
169
- extensions = [ancestral_trait[:template_extensions].keys].flatten.uniq
169
+ extensions = [_template_extensions.keys].flatten.uniq
170
170
  glob = "#{path}.{#{opts[:ext] || extensions.join(',')}}"
171
171
  Dir[glob].first
172
172
  end
@@ -175,11 +175,11 @@ module Merb
175
175
  if template =~ /\//
176
176
  t = template.split('/')
177
177
  template = t.pop
178
- path = ancestral_trait[:template_root] / t.join('/') / "_#{template}"
178
+ path = _template_root / t.join('/') / "_#{template}"
179
179
  else
180
- path = ancestral_trait[:template_root] / self.class.name.snake_case / "_#{template}"
180
+ path = _template_root / self.class.name.snake_case / "_#{template}"
181
181
  end
182
- extensions = [ancestral_trait[:template_extensions].keys].flatten.uniq
182
+ extensions = [_template_extensions.keys].flatten.uniq
183
183
  glob = "#{path}.{#{opts[:ext] || extensions.join(',')}}"
184
184
  Dir[glob].first
185
185
  end
@@ -1,5 +1,6 @@
1
+ require 'enumerator'
2
+
1
3
  module Merb
2
- # Thanks to Chris Wanstrath
3
4
  # use this in your controllers to switch output based on
4
5
  # the HTTP_ACCEPT header. like so:
5
6
  # respond_to do |type|
@@ -8,15 +9,13 @@ module Merb
8
9
  # type.xml { @foo.to_xml }
9
10
  # type.yaml { @foo.to_yaml }
10
11
  # end
11
-
12
- # TODO : revisit this whole patern. Can we improve on this?
13
12
  module ResponderMixin
14
13
 
15
14
  def respond_to(&block)
16
15
  responder = Rest::Responder.new(@env['HTTP_ACCEPT'], params)
17
16
  block.call(responder)
18
17
  responder.respond(headers)
19
- @status = responder.status
18
+ @status = responder.status if responder.status
20
19
  responder.body
21
20
  end
22
21
 
@@ -56,7 +55,6 @@ module Merb
56
55
  mime_type = negotiate_content
57
56
  if mime_type
58
57
  headers['Content-Type'] = mime_type.super_range
59
- @status = 200
60
58
  @body = @stack[mime_type.to_sym].call
61
59
  else
62
60
  headers['Content-Type'] = nil
@@ -68,10 +66,19 @@ module Merb
68
66
  protected
69
67
 
70
68
  def self.parse(accept_header)
71
- index = 0
72
- list = accept_header.split(/,/).map! do |entry|
69
+ # parse the raw accept header into a unique, sorted array of AcceptType objects
70
+ returning( accept_header.split(/,/).enum_for(:each_with_index).map do |entry,index|
73
71
  AcceptType.new(entry,index += 1)
74
- end.sort!.uniq
72
+ end.sort.uniq ) do |list|
73
+ # firefox (and possibly other browsers) send broken default accept headers.
74
+ # fix them up by sorting alternate xml forms (namely application/xhtml+xml)
75
+ # ahead of pure xml types (application/xml,text/xml).
76
+ if app_xml = list.detect{|e| e.super_range == 'application/xml'}
77
+ list.select{|e| e.to_s =~ /\+xml/}.each { |acc_type|
78
+ list[list.index(acc_type)],list[list.index(app_xml)] =
79
+ list[list.index(app_xml)],list[list.index(acc_type)] }
80
+ end
81
+ end
75
82
  end
76
83
 
77
84
  private
@@ -88,7 +95,7 @@ module Merb
88
95
  format = @params['format'].to_sym
89
96
  if @stack[format]
90
97
  if @accepts.map(&:to_sym).include?(format)
91
- @accepts.select{|a| a.to_sym == format }.first
98
+ @accepts.detect{|a| a.to_sym == format }
92
99
  else
93
100
  AcceptType.new(TYPES[format].first,0)
94
101
  end
@@ -116,7 +123,7 @@ module Merb
116
123
  end
117
124
 
118
125
  def <=>(entry)
119
- returning (entry.quality <=> quality).to_s do |c|
126
+ returning((entry.quality <=> quality).to_s) do |c|
120
127
  c.replace((index <=> entry.index).to_s) if c == '0'
121
128
  end.to_i
122
129
  end
@@ -17,8 +17,19 @@ module Merb
17
17
  %{<a href="#" onclick="#{function}; return false;">#{name}</a>}
18
18
  end
19
19
 
20
- def image_tag(img, path='/images/', opts={})
21
- %{<img src="#{path+img}" #{opts.map{|k,v| "#{k}=\"#{v}\""}.join(' ')} />}
20
+ # creates an <img> tag
21
+ # defaults to a src path prefix of /images/
22
+ #
23
+ # image_tag('foo.gif') => <img src='/images/foo.gif' />
24
+ # image_tag('foo.gif', :class => 'bar') => <img src='/images/foo.gif' class='bar' />
25
+ #
26
+ # you can override the default path by sending a :path parameter in the opts
27
+ #
28
+ # image_tag('foo.gif', :path => '/files/') => <img src='/files/foo.gif' />
29
+ #
30
+ def image_tag(img, opts={})
31
+ opts[:path] ||= '/images/'
32
+ %{<img src="#{opts.delete(:path) + img}" #{opts.map{|k,v| "#{k}=\"#{v}\""}.join(' ')} />}
22
33
  end
23
34
 
24
35
  # calls .to_json on data. This will use fjson if installed
@@ -6,10 +6,10 @@ module Merb
6
6
 
7
7
  def setup_session
8
8
  MERB_LOGGER.info("Setting up session")
9
- before = @cookies[ancestral_trait[:session_id_key]]
10
- @session, @cookies[ancestral_trait[:session_id_key]] = Merb::Session.persist(@cookies[ancestral_trait[:session_id_key]])
9
+ before = @cookies[_session_id_key]
10
+ @session, @cookies[_session_id_key] = Merb::Session.persist(@cookies[_session_id_key])
11
11
  @_fingerprint_before = Marshal.dump(@session).hash
12
- @_new_cookie = @cookies[ancestral_trait[:session_id_key]] != before
12
+ @_new_cookie = @cookies[_session_id_key] != before
13
13
  end
14
14
 
15
15
  def finalize_session
@@ -17,7 +17,7 @@ module Merb
17
17
  unless Marshal.dump(@session).hash == @_fingerprint_before
18
18
  @session.save
19
19
  end
20
- set_cookie(ancestral_trait[:session_id_key], @cookies[ancestral_trait[:session_id_key]], Time.now+Merb::Const::WEEK*2) if @_new_cookie
20
+ set_cookie(_session_id_key, @cookies[_session_id_key], Time.now+Merb::Const::WEEK*2) if @_new_cookie
21
21
  end
22
22
 
23
23
  end
@@ -4,14 +4,14 @@ module Merb
4
4
 
5
5
  def setup_session
6
6
  MERB_LOGGER.info("Setting up session")
7
- before = @cookies[ancestral_trait[:session_id_key]]
8
- @session , @cookies[ancestral_trait[:session_id_key]] = Merb::MemorySession.persist(@cookies[ancestral_trait[:session_id_key]])
9
- @_new_cookie = @cookies[ancestral_trait[:session_id_key]] != before
7
+ before = @cookies[_session_id_key]
8
+ @session , @cookies[_session_id_key] = Merb::MemorySession.persist(@cookies[_session_id_key])
9
+ @_new_cookie = @cookies[_session_id_key] != before
10
10
  end
11
11
 
12
12
  def finalize_session
13
13
  MERB_LOGGER.info("Finalize session")
14
- set_cookie(ancestral_trait[:session_id_key], @cookies[ancestral_trait[:session_id_key]], Time.now+Merb::Const::WEEK*2) if @_new_cookie
14
+ set_cookie(_session_id_key, @cookies[_session_id_key], Time.now+Merb::Const::WEEK*2) if @_new_cookie
15
15
  end
16
16
 
17
17
  end
@@ -3,7 +3,7 @@ module Merb
3
3
 
4
4
 
5
5
  module Erubis
6
- ::Merb::Controller.register_engine self, %w[ herb jerb erb ]
6
+ ::Merb::Controller.register_engine self, %w[ herb jerb erb rhtml ]
7
7
  class << self
8
8
 
9
9
  @@erbs = {}
@@ -23,15 +23,13 @@ module Merb
23
23
  module Template
24
24
  module Haml
25
25
 
26
- # Custom HAML-options to be merged.
27
-
28
- trait :haml_options => {
29
- :locals => {}
30
- }
31
-
32
26
  ::Merb::Controller.register_engine self, %w[ haml ]
33
27
 
34
28
  class << self
29
+
30
+ class_inheritable_accessor :haml_options
31
+ self.haml_options = { :locals => {} }
32
+
35
33
  @@hamls ||= {}
36
34
  @@mtimes ||= {}
37
35
  def exempt_from_layout?
@@ -40,15 +38,23 @@ module Merb
40
38
 
41
39
  def transform(options = {})
42
40
  opts, file, view_context = options.values_at(:opts, :file, :view_context)
43
- opts = ancestral_trait[:haml_options].merge(opts)
44
- if precompiled = get_precompiled(file)
45
- opts[:precompiled] ||= precompiled
46
- haml = ::Haml::Engine.new("", opts)
47
- else
48
- haml = ::Haml::Engine.new(IO.read(file), opts)
49
- set_precompiled(file, haml.precompiled)
41
+ opts = haml_options.merge(opts)
42
+ begin
43
+ if precompiled = get_precompiled(file)
44
+ opts[:precompiled] ||= precompiled
45
+ haml = ::Haml::Engine.new("", opts)
46
+ else
47
+ haml = ::Haml::Engine.new(IO.read(file), opts)
48
+ set_precompiled(file, haml.precompiled)
49
+ end
50
+
51
+ haml.to_html(view_context)
52
+ rescue
53
+ # ::Haml::Engine often inserts a bogus "(haml):#{line_number}" entry in the backtrace.
54
+ # Let's replace it with the path of the actual template
55
+ $@[0].sub! /\(haml\)/, file
56
+ raise # Raise the exception again
50
57
  end
51
- haml.to_html(view_context)
52
58
  end
53
59
 
54
60
  private
@@ -9,7 +9,16 @@ namespace :merb do
9
9
  f.chmod(0744)
10
10
  }
11
11
 
12
- puts "Freezing Merb Framework into dist/framework"
13
- puts "Use script/merb to start instead of plain merb"
12
+ puts " Freezing Merb Framework into dist/framework"
13
+ puts " Use script/merb to start instead of plain merb"
14
+ end
15
+ desc "unfreeze this app from the framework and use system gem."
16
+ task :unfreeze do
17
+ FileUtils.rm_rf MERB_ROOT / 'dist/framework'
18
+ FileUtils.rm MERB_ROOT / 'script/merb'
19
+
20
+ puts " Removed: "
21
+ puts " - #{MERB_ROOT / 'dist/framework'} (recursive) "
22
+ puts " - #{MERB_ROOT / 'script/merb'}"
14
23
  end
15
24
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: merb
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2007-03-18 00:00:00 -07:00
6
+ version: 0.3.0
7
+ date: 2007-04-28 00:00:00 -07:00
8
8
  summary: Merb == Mongrel + Erb. Pocket rocket web framework.
9
9
  require_paths:
10
10
  - lib
@@ -70,6 +70,8 @@ files:
70
70
  - lib/merb/core_ext/merb_class.rb
71
71
  - lib/merb/core_ext/merb_enumerable.rb
72
72
  - lib/merb/core_ext/merb_hash.rb
73
+ - lib/merb/core_ext/merb_inflections.rb
74
+ - lib/merb/core_ext/merb_inflector.rb
73
75
  - lib/merb/core_ext/merb_kernel.rb
74
76
  - lib/merb/core_ext/merb_module.rb
75
77
  - lib/merb/core_ext/merb_numeric.rb