ramaze 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/Rakefile +13 -7
  2. data/doc/README +1 -1
  3. data/doc/README.html +729 -0
  4. data/doc/meta/users.kml +61 -59
  5. data/doc/readme_chunks/installing.txt +1 -1
  6. data/examples/auth/auth.rb +13 -8
  7. data/examples/element.rb +1 -2
  8. data/examples/ramaise.rb +139 -0
  9. data/examples/simple.rb +2 -5
  10. data/examples/sourceview/public/coderay.css +104 -0
  11. data/examples/sourceview/public/jquery.treeview.css +10 -9
  12. data/examples/sourceview/public/jquery.treeview.js +7 -7
  13. data/examples/sourceview/public/sourceview.js +43 -7
  14. data/examples/sourceview/sourceview.rb +36 -33
  15. data/examples/sourceview/template/index.haml +33 -17
  16. data/examples/wikore/spec/wikore.rb +1 -0
  17. data/examples/wikore/src/controller.rb +0 -1
  18. data/examples/wikore/src/model.rb +17 -16
  19. data/lib/proto/src/controller/main.rb +1 -1
  20. data/lib/proto/start.rb +2 -3
  21. data/lib/ramaze.rb +3 -0
  22. data/lib/ramaze/contrib/auto_params.rb +3 -3
  23. data/lib/ramaze/contrib/gzip_filter.rb +3 -4
  24. data/lib/ramaze/contrib/route.rb +5 -0
  25. data/lib/ramaze/controller/resolve.rb +1 -1
  26. data/lib/ramaze/dispatcher/action.rb +1 -1
  27. data/lib/ramaze/dispatcher/error.rb +7 -4
  28. data/lib/ramaze/gestalt.rb +1 -2
  29. data/lib/ramaze/helper.rb +8 -4
  30. data/lib/ramaze/helper/auth.rb +1 -1
  31. data/lib/ramaze/helper/cache.rb +4 -0
  32. data/lib/ramaze/helper/identity.rb +2 -2
  33. data/lib/ramaze/snippets/{string/DIVIDE.rb → divide.rb} +11 -8
  34. data/lib/ramaze/snippets/string/unindent.rb +7 -0
  35. data/lib/ramaze/snippets/thread/into.rb +5 -12
  36. data/lib/ramaze/spec/helper/wrap.rb +1 -1
  37. data/lib/ramaze/template/haml.rb +4 -14
  38. data/lib/ramaze/template/sass.rb +4 -14
  39. data/lib/ramaze/trinity/request.rb +34 -1
  40. data/lib/ramaze/version.rb +1 -1
  41. data/rake_tasks/coverage.rake +46 -0
  42. data/rake_tasks/spec.rake +1 -1
  43. data/spec/contrib/auto_params.rb +6 -1
  44. data/spec/contrib/route.rb +0 -2
  45. data/spec/contrib/sequel/fill.rb +6 -4
  46. data/spec/ramaze/controller/resolve.rb +31 -0
  47. data/spec/ramaze/helper/cache.rb +14 -7
  48. data/spec/ramaze/template/haml.rb +14 -0
  49. data/spec/ramaze/template/sass.rb +23 -1
  50. data/spec/ramaze/trinity/request.rb +19 -0
  51. data/spec/snippets/{string/DIVIDE.rb → divide.rb} +5 -1
  52. data/spec/snippets/kernel/__dir__.rb +1 -1
  53. data/spec/snippets/string/unindent.rb +22 -0
  54. data/spec/snippets/thread/into.rb +21 -0
  55. metadata +90 -80
@@ -4,10 +4,10 @@
4
4
  # using ruby2ruby and ParseTree
5
5
  #
6
6
  # Usage:
7
- # require 'ramaze/contrib'
7
+ #
8
8
  # Ramaze.contrib :auto_params
9
9
  #
10
- # For example,
10
+ # Then, for example,
11
11
  #
12
12
  # def search(query) end
13
13
  #
@@ -86,7 +86,7 @@ module Ramaze
86
86
  # use Method#get_args to insert values from request.params into Action#params
87
87
 
88
88
  def self.resolve_method(name, *params)
89
- if method = action_methods.delete(name)
89
+ if method = [ name, name.gsub('__','/') ].find{|n| action_methods.delete(n) }
90
90
  meth = instance_method(method)
91
91
  arity = meth.arity
92
92
 
@@ -6,11 +6,10 @@
6
6
  # Use this to compress "large" pages with gzip. All major browsers support gzipped pages.
7
7
  # This filter brought to you by your friends in #ramaze: Pistos, manveru, rikur and Kashia.
8
8
  #
9
- # Usage:
10
- # in start.rb:
9
+ # Usage, in start.rb:
11
10
  #
12
- # require 'path/to/gzip-filter'
13
- # Ramaze::Dispatcher::Action::FILTER << Ramaze::Filter::Gzip
11
+ # require 'ramaze/contrib/gzip_filter'
12
+ # Ramaze::Dispatcher::Action::FILTER << Ramaze::Filter::Gzip
14
13
 
15
14
  require 'zlib'
16
15
 
@@ -1,6 +1,11 @@
1
1
  # Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
2
2
  # All files in this distribution are subject to the terms of the Ruby license.
3
3
 
4
+ # Usage:
5
+ #
6
+ # Ramaze.contrib :route
7
+ # Ramaze::Contrib::Route[ %r!^/(\d+\.\d{2})$! ] = "/price/%.2f"
8
+
4
9
  module Ramaze
5
10
  module Contrib
6
11
  class Route
@@ -126,7 +126,7 @@ module Ramaze
126
126
 
127
127
  # Based on methodname and arity, tries to find the right method on current controller.
128
128
  def resolve_method(name, *params)
129
- if method = action_methods.delete(name)
129
+ if method = [ name, name.gsub('__','/') ].find{|n| action_methods.delete(n) }
130
130
  arity = instance_method(method).arity
131
131
  if params.size == arity or arity < 0
132
132
  return method, params
@@ -23,7 +23,7 @@ module Ramaze
23
23
  # post-processing.
24
24
 
25
25
  def process(path)
26
- Inform.info("Dynamic request from #{request.remote_addr}: #{path}")
26
+ Inform.info("Dynamic request from #{request.ip}: #{request.request_uri}")
27
27
  body = Controller.handle(path)
28
28
  response = Response.current.build(body)
29
29
  FILTER.inject(response){|r,f| f.call(r) }
@@ -40,11 +40,14 @@ module Ramaze
40
40
  status ||= 500
41
41
 
42
42
  if controller = metainfo[:controller]
43
- begin
44
- action = Controller.resolve(controller.mapping + path)
45
- return response.build(action.render, status)
46
- rescue Ramaze::Error => e
43
+ newpath = (controller.mapping + path).squeeze('/')
44
+ action_response = Dispatcher::Action.process(newpath)
45
+ case action_response
46
+ when Ramaze::Error
47
47
  Inform.debug("No custom error page found on #{controller}, going to #{path}")
48
+ else
49
+ action_response.status = status
50
+ return action_response
48
51
  end
49
52
  end
50
53
 
@@ -8,8 +8,7 @@ Example:
8
8
  require 'ramaze/gestalt'
9
9
 
10
10
  def random_color
11
- r = lambda{|n| rand(n).to_s(16) }
12
- '#' + [255, 255, 255].map{r[255] + r[255] + r[255]
11
+ ('#' << '%02x' * 3) % (1..3).map{ rand(255) }
13
12
  end
14
13
 
15
14
  puts Ramaze::Gestalt.build{
@@ -23,10 +23,14 @@ module Ramaze
23
23
  def helper *syms
24
24
  syms.each do |sym|
25
25
  mod_name = sym.to_s.capitalize + 'Helper'
26
- glob = "{helper,#{BASEDIR/:ramaze/:helper}}/#{sym}.{rb,so}"
27
- require Dir[glob].first
28
- include ::Ramaze.const_get(mod_name)
29
- extend ::Ramaze.const_get(mod_name)
26
+ begin
27
+ include ::Ramaze.const_get(mod_name)
28
+ extend ::Ramaze.const_get(mod_name)
29
+ rescue NameError
30
+ files = Dir["{helper,#{BASEDIR/:ramaze/:helper}}/#{sym}.{rb,so}"]
31
+ raise LoadError, "#{mod_name} not found" unless files.any?
32
+ require(files.first) ? retry : raise
33
+ end
30
34
  end
31
35
  end
32
36
  end
@@ -29,7 +29,7 @@ module Ramaze
29
29
  if check_auth(username, password)
30
30
  session[:logged_in] = true
31
31
  session[:username] = username
32
- inside_stack? ? answer : redirect( R(self) )
32
+ inside_stack? ? answer : redirect_referrer
33
33
  else
34
34
  if AUTH_ELEMENT and AUTH_ELEMENT.to_s.split.any?
35
35
  open_element = "<#{AUTH_ELEMENT}>"
@@ -37,8 +37,12 @@ module Ramaze
37
37
  # "hi #{request['name']}"
38
38
  # end
39
39
  # end
40
+ #
41
+ # cache acts as a wrapper for value_cache if no args are given
40
42
 
41
43
  def cache *args
44
+ return value_cache if args.size == 0
45
+
42
46
  if args.last.is_a? Hash
43
47
  opts = args.pop
44
48
  end
@@ -20,11 +20,11 @@ module Ramaze
20
20
  # Simple form for use or overwriting.
21
21
  # Has to provide the same functionality when overwritten or directly
22
22
  # embedded into a page.
23
- def openid_login_form
23
+ def openid_login_form(caption="login")
24
24
  %{
25
25
  <form method="GET" action="#{Rs(:openid_begin)}">
26
26
  Identity URL: <input type="text" name="url" />
27
- <input type="submit" />
27
+ <input type="submit" value="#{caption}"/>
28
28
  </form>
29
29
  }
30
30
  end
@@ -1,17 +1,20 @@
1
1
  # Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
2
2
  # All files in this distribution are subject to the terms of the Ruby license.
3
3
 
4
- # Extensions for String
4
+ # A convenient way to do File.join
5
+ #
6
+ # Example:
7
+ # 'a' / 'b' # -> 'a/b'
8
+ # File.dirname(__FILE__) / 'bar' # -> "ramaze/snippets/string/bar"
5
9
 
6
10
  class String
7
-
8
- # A convenient way to do File.join
9
- #
10
- # Example:
11
- # 'a' / 'b' # -> 'a/b'
12
- # File.dirname(__FILE__) / 'bar' # -> "ramaze/snippets/string/bar"
13
-
14
11
  def / obj
15
12
  File.join(self, obj.to_s)
16
13
  end
17
14
  end
15
+
16
+ class Symbol
17
+ def / obj
18
+ self.to_s / obj
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ class String
2
+ def unindent
3
+ space = self.split("\n")[1].to_s[/^(\s+)/, 1]
4
+ strip.gsub(/^#{space}/, '')
5
+ end
6
+ alias ui unindent
7
+ end
@@ -6,20 +6,13 @@ class Thread
6
6
  # :action, :response, :request, :session,
7
7
  # :task, :adapter, :controller, :exception
8
8
 
9
- def self.into
10
- Thread.new(Thread.current) do |thread|
11
- current = Thread.current
12
-
13
- vars = Dir["#{Ramaze::BASEDIR}/**/*.rb"].
14
- map{|f| File.readlines(f).
15
- map{|l| l[/Thread\.current\[:([^\]]*)\]/, 1] } }
16
-
17
- vars.flatten.compact.uniq.each do |var|
18
- var = var.to_sym
19
- current[var] = thread[var]
9
+ def self.into *args
10
+ Thread.new(Thread.current, *args) do |thread, *args|
11
+ thread.keys.each do |k|
12
+ Thread.current[k] = thread[k] unless k.to_s =~ /^__/
20
13
  end
21
14
 
22
- yield
15
+ yield *args
23
16
  end
24
17
  end
25
18
  end
@@ -164,7 +164,7 @@ class SpecFile
164
164
  puts "[ #@name ]".center(80, '-')
165
165
  puts "ExitStatus:".yellow, PP.pp(@status)
166
166
  puts "StdOut:".yellow, @stdout
167
- puts "StdErr:".yellow, PP.pp(@stderr, '')
167
+ puts "StdErr:".yellow, @stderr
168
168
  end
169
169
 
170
170
  def parse
@@ -11,32 +11,22 @@ module Ramaze
11
11
 
12
12
  class Haml < Template
13
13
 
14
- # Custom HAML-options for your controller to be merged.
15
-
16
- trait :haml_options => {
17
- :locals => {}
18
- }
19
-
20
14
  ENGINES[self] = %w[ haml ]
21
15
 
22
16
  class << self
23
17
 
24
- # Transform any String via Haml, takes optionally an hash with the
25
- # haml_options that you can set also by
26
- # trait :haml_options => {}
27
- # if you pass the options it will merge the trait with them. (your
28
- # options override the defaults from trait[:haml_options]
18
+ # Transform via Haml templating engine
29
19
 
30
20
  def transform action
31
21
  haml = wrap_compile(action)
32
22
  haml.to_html(action.instance)
33
23
  end
34
24
 
35
- # Instantiates Haml::Engine with the template and haml_options from
36
- # the trait.
25
+ # Instantiates Haml::Engine with the template and haml_options trait from
26
+ # the controller.
37
27
 
38
28
  def compile(action, template)
39
- ::Haml::Engine.new(template, ancestral_trait[:haml_options])
29
+ ::Haml::Engine.new(template, action.controller.trait[:haml_options] || {})
40
30
  end
41
31
  end
42
32
  end
@@ -11,21 +11,11 @@ module Ramaze
11
11
 
12
12
  class Sass < Template
13
13
 
14
- # Custom SASS-options for your controller to be merged.
15
-
16
- trait :sass_options => {
17
- :locals => {}
18
- }
19
-
20
14
  ENGINES[self] = %w[ sass ]
21
15
 
22
16
  class << self
23
17
 
24
- # Transform any String via Sass, takes optionally an hash with the
25
- # sass_options that you can set also by
26
- # trait :sass_options => {}
27
- # if you pass the options it will merge the trait with them. (your
28
- # options override the defaults from trait[:sass_options]
18
+ # Transform via Sass templating engine
29
19
 
30
20
  def transform action
31
21
  Response.current['Content-Type'] = "text/css"
@@ -33,11 +23,11 @@ module Ramaze
33
23
  sass.to_css()
34
24
  end
35
25
 
36
- # Instantiates Sass::Engine with the template and sass_options from
37
- # the trait.
26
+ # Instantiates Sass::Engine with the template and sass_options trait from
27
+ # the controller.
38
28
 
39
29
  def compile(action, template)
40
- ::Sass::Engine.new(template, ancestral_trait[:sass_options])
30
+ ::Sass::Engine.new(template, action.controller.trait[:sass_options] || {})
41
31
  end
42
32
  end
43
33
  end
@@ -4,6 +4,7 @@
4
4
  require 'cgi'
5
5
  require 'tmpdir'
6
6
  require 'digest/md5'
7
+ require 'ipaddr'
7
8
  require 'rack'
8
9
  require 'rack/request'
9
10
 
@@ -34,7 +35,39 @@ module Ramaze
34
35
  super
35
36
  end
36
37
 
37
- unless defined?(rack_params)
38
+ def request_uri
39
+ env['REQUEST_URI'] || path_info
40
+ end
41
+
42
+ def ip
43
+ env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR']
44
+ end
45
+
46
+ # Request is from a local network?
47
+ # Checks both IPv4 and IPv6
48
+
49
+ ipv4 = %w[ 127.0.0.1/32 192.168.0.0/16 172.16.0.0/12 10.0.0.0/8 169.254.0.0/16 ]
50
+ ipv6 = %w[ fc00::/7 fe80::/10 fec0::/10 ::1 ]
51
+ LOCAL = (ipv4 + ipv6).map{|a| IPAddr.new(a)}
52
+
53
+ # --
54
+ # Mongrel somehow puts together multiple IPs when proxy is involved.
55
+ # ++
56
+
57
+ def local_net?(address = ip)
58
+ address = address.to_s.split(',').first
59
+ addr = IPAddr.new(address)
60
+ LOCAL.find{|range| range.include?(addr) }
61
+ end
62
+
63
+ def [](key, *rest)
64
+ value = params[key.to_s]
65
+ return value if rest.empty?
66
+ keys = rest.flatten.map{|k| k.to_s}
67
+ Array[value, *params.values_at(*keys)]
68
+ end
69
+
70
+ unless method_defined?(:rack_params)
38
71
  alias rack_params params
39
72
 
40
73
  # Wrapping Request#params to support a one-level hash notation.
@@ -5,7 +5,7 @@ module Ramaze #:nodoc:
5
5
  module Version #:nodoc:
6
6
  MAJOR = 0
7
7
  MINOR = 2
8
- TINY = 0
8
+ TINY = 1
9
9
 
10
10
  STRING = [MAJOR, MINOR, TINY].join('.')
11
11
  end
@@ -0,0 +1,46 @@
1
+ # Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
2
+ # All files in this distribution are subject to the terms of the Ruby license.
3
+
4
+ require 'rake'
5
+ require 'lib/ramaze/snippets/divide'
6
+
7
+ spec_base = File.expand_path('spec/ramaze/')
8
+ example_base = File.expand_path('examples')
9
+ snippets_base = File.expand_path('spec/snippets')
10
+ # ignore files with these paths
11
+ ignores = [ './*', './helper/*', './ramaze/adapter.rb', './ramaze/request.rb', ]
12
+
13
+ files = Dir[spec_base/'**'/'*.rb'] +
14
+ Dir[example_base/'**/spec'/'*.rb']
15
+ ignores.each do |ignore|
16
+ ignore_files = Dir[spec_base/ignore]
17
+ ignore_files.each do |ignore_file|
18
+ files.delete File.expand_path(ignore_file)
19
+ end
20
+ end
21
+
22
+ files.sort!
23
+
24
+ last = files.pop
25
+
26
+ COV_CMD = "rcov --aggregate coverage.data --%shtml -%s -x gem -x rack %s"
27
+
28
+ def sys(cmd)
29
+ puts cmd
30
+ system(cmd)
31
+ end
32
+
33
+ task :coverage => :clean do
34
+ # these are the tests that can be run in parallel.
35
+ # IMHO, ideally we should have
36
+ # * 100% coverage of ramaze with pure tests
37
+ # * 100% coverage with non-pure functional tests
38
+ pure_specs = Dir[snippets_base/'**/*.rb'].entries
39
+ sys(COV_CMD % ["no-","t", pure_specs.join(' ')])
40
+
41
+ files.each do |file|
42
+ sys(COV_CMD % ["no-","t", file])
43
+ end
44
+ sys(COV_CMD % ["", "t", last] )
45
+
46
+ end
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'rake'
5
5
  require 'ramaze/spec/helper/layout'
6
- require 'lib/ramaze/snippets/string/DIVIDE'
6
+ require 'lib/ramaze/snippets/divide'
7
7
 
8
8
  SPEC_BASE = File.expand_path('spec')
9
9
  EXAMPLE_BASE = File.expand_path('examples')
@@ -1,13 +1,14 @@
1
1
  require 'spec/helper'
2
2
  testcase_requires 'ruby2ruby'
3
3
 
4
- require 'ramaze/contrib'
5
4
  Ramaze.contrib :auto_params
6
5
 
7
6
  module AnotherController
8
7
  def another_page
9
8
  'another page'
10
9
  end
10
+
11
+ define_method(:css/'style.css') { 'style.css' }
11
12
  end
12
13
 
13
14
  class MainController < Ramaze::Controller
@@ -94,4 +95,8 @@ describe 'Normal behavior' do
94
95
  it 'should work with included actions' do
95
96
  get('/another_page').body.should == 'another page'
96
97
  end
98
+
99
+ it 'should work with /' do
100
+ get('/css/style.css').body.should == 'style.css'
101
+ end
97
102
  end