mack 0.5.0 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/CHANGELOG +19 -0
  2. data/bin/mack +3 -2
  3. data/bin/mack_ring_server +19 -1
  4. data/lib/{sea_level/controller_base.rb → controller/base.rb} +87 -128
  5. data/lib/{sea_level → controller}/cookie_jar.rb +3 -3
  6. data/lib/{sea_level → controller}/filter.rb +0 -0
  7. data/lib/{sea_level → controller}/request.rb +0 -0
  8. data/lib/{sea_level → controller}/response.rb +0 -0
  9. data/lib/{sea_level → controller}/session.rb +0 -0
  10. data/lib/{sea_level → controller}/uploaded_file.rb +0 -0
  11. data/lib/distributed/routing/urls.rb +1 -1
  12. data/lib/distributed/utils/rinda.rb +1 -1
  13. data/lib/errors/errors.rb +6 -4
  14. data/lib/generators/mack_application_generator/templates/config/initializers/mime_types.rb.template +3 -0
  15. data/lib/generators/mack_application_generator/templates/public/favicon.ico.template +0 -0
  16. data/lib/initialization/configuration.rb +2 -1
  17. data/lib/initialization/console.rb +2 -2
  18. data/lib/initialization/{initializers/logging.rb → logging.rb} +0 -0
  19. data/lib/initialization/{initializers/orm_support.rb → orm_support.rb} +0 -0
  20. data/lib/initialization/{initializers/plugins.rb → plugins.rb} +0 -0
  21. data/lib/mack.rb +107 -131
  22. data/lib/mack_tasks.rb +1 -1
  23. data/lib/rendering/engine/base.rb +26 -0
  24. data/lib/rendering/engine/builder.rb +30 -0
  25. data/lib/rendering/engine/erubis.rb +67 -0
  26. data/lib/rendering/engine/haml.rb +18 -0
  27. data/lib/rendering/engine/markaby.rb +27 -0
  28. data/lib/rendering/engine/registry.rb +48 -0
  29. data/lib/rendering/type/action.rb +37 -0
  30. data/lib/rendering/type/base.rb +59 -0
  31. data/lib/rendering/type/file_base.rb +32 -0
  32. data/lib/rendering/type/inline.rb +26 -0
  33. data/lib/rendering/type/layout.rb +26 -0
  34. data/lib/rendering/type/partial.rb +40 -0
  35. data/lib/rendering/type/public.rb +29 -0
  36. data/lib/rendering/type/template.rb +22 -0
  37. data/lib/rendering/type/text.rb +17 -0
  38. data/lib/rendering/type/url.rb +120 -0
  39. data/lib/rendering/type/xml.rb +34 -0
  40. data/lib/rendering/view_template.rb +168 -0
  41. data/lib/routing/route_map.rb +20 -11
  42. data/lib/runner.rb +137 -0
  43. data/lib/utils/mime_types.rb +56 -0
  44. data/lib/utils/mime_types.yml +449 -0
  45. data/lib/{sea_level/helpers/view_helpers → view_helpers}/html_helpers.rb +0 -0
  46. data/lib/{sea_level/helpers/view_helpers → view_helpers}/string_helpers.rb +0 -0
  47. metadata +58 -29
  48. data/lib/initialization/initializer.rb +0 -110
  49. data/lib/rendering/base.rb +0 -62
  50. data/lib/rendering/classes/action.rb +0 -26
  51. data/lib/rendering/classes/partial.rb +0 -12
  52. data/lib/rendering/classes/public.rb +0 -13
  53. data/lib/rendering/classes/text.rb +0 -12
  54. data/lib/rendering/classes/url.rb +0 -59
  55. data/lib/rendering/classes/xml.rb +0 -24
  56. data/lib/sea_level/view_binder.rb +0 -88
@@ -9,7 +9,7 @@ module Mack
9
9
  begin
10
10
  ring_server.take([options[:space], options[:klass_def], nil, nil], options[:timeout])
11
11
  rescue Exception => e
12
- puts e.message
12
+ # MACK_DEFAULT_LOGGER.error(e)
13
13
  end
14
14
  register(options)
15
15
  end
data/lib/errors/errors.rb CHANGED
@@ -6,8 +6,8 @@ module Mack
6
6
  # Example:
7
7
  # class FooController < Mack::Controller::Base
8
8
  # def index
9
- # render(:text => "Hello World")
10
- # render(:action => "edit")
9
+ # render(:text, "Hello World")
10
+ # render(:action, "edit")
11
11
  # end
12
12
  # end
13
13
  class DoubleRender < StandardError
@@ -86,12 +86,14 @@ module Mack
86
86
  end
87
87
  end
88
88
 
89
- # Potentially raised if a render(:url => "....") is a status other than 200.
89
+ # Potentially raised if a render(:url, "....") is a status other than 200.
90
90
  # This is only raised if :raise_exception is passed in as true to the render options.
91
91
  class UnsuccessfulRenderUrl < StandardError
92
92
  # Takes the uri trying to be rendered the Net::HTTP response object.
93
93
  def initialize(uri, response)
94
- super("URI: #{uri}; status: #{response.code}; body: #{response.body}")
94
+ code = response.code if response.respond_to?(:code)
95
+ code = response.status if response.respond_to?(:status)
96
+ super("URI: #{uri}; status: #{code}")
95
97
  end
96
98
  end
97
99
 
@@ -0,0 +1,3 @@
1
+ # Register mime types for a file extension in this file.
2
+ # Example:
3
+ # Mack::Utils::MimeTypes.register(:iphone, "app/iphone")
@@ -59,7 +59,8 @@ module Mack
59
59
  DEFAULTS_TEST = {
60
60
  "log::level" => "error",
61
61
  "run_remote_tests" => true,
62
- "mack::drb_timeout" => 0
62
+ "mack::drb_timeout" => 0,
63
+ "mack::cookie_values" => {}
63
64
  } unless self.const_defined?("DEFAULTS_TEST")
64
65
 
65
66
  unless self.const_defined?("DEFAULTS")
@@ -5,10 +5,10 @@
5
5
  # * Mack::TestHelpers
6
6
  # * Mack::Routes::Urls
7
7
 
8
- require File.join(File.dirname(__FILE__), "initializer")
9
-
10
8
  fl = File.join(File.dirname(__FILE__), "..")
11
9
 
10
+ require File.join(fl, "mack")
11
+
12
12
  require File.join(fl, "test_extensions", "test_helpers")
13
13
 
14
14
  # self.send(:include, Mack::TestHelpers)
data/lib/mack.rb CHANGED
@@ -1,139 +1,115 @@
1
- require File.join(File.dirname(__FILE__), "initialization", "initializer")
1
+ require 'rubygems'
2
+ require 'rack'
3
+ require 'digest'
4
+ require 'mack_ruby_core_extensions'
5
+ require 'application_configuration'
6
+ require 'cachetastic'
7
+ require 'fileutils'
8
+ require 'log4r'
9
+ require 'crypt/rijndael'
10
+ require 'singleton'
11
+ require 'uri'
12
+ require 'drb'
13
+ require 'rinda/ring'
14
+ require 'rinda/tuplespace'
15
+ require 'builder'
16
+ require 'erubis'
17
+ require 'erb'
18
+ require 'markaby'
19
+ require 'haml'
20
+ require 'genosaurus'
21
+ require 'net/http'
2
22
 
3
- module Mack
4
- # This is the heart and soul of the Mack framework! This class interfaces with the Rack framework.
5
- # It handles all the dispatching back and forth between the Rack framework and a Mack application.
6
- class Runner
7
- include Mack::Routes::Urls
8
-
9
- attr_reader :response # :nodoc:
10
- attr_reader :request # :nodoc:
11
- attr_reader :cookies # :nodoc:
12
- # This method needs to be defined as part of the Rack framework. As is noted for the Mack::Runner
13
- # class, this is where the center of the Mack framework lies.
14
- def call(env)
15
- # pp env
16
- begin
17
- setup(env) do
18
- begin
19
- route = Mack::Routes::RouteMap.instance.get_route_from_request(self.request)
20
- if route[:redirect_to]
21
- # because the route is specified to be a redirect, let's do that:
22
- redirect_to(route)
23
- else
24
- # let's handle a normal request:
25
- begin
26
- cont = "#{route[:controller].to_s.camelcase}Controller".constantize
27
- rescue NameError => e
28
- raise Mack::Errors::ResourceNotFound.new(self.request.path_info)
29
- end
30
- c = cont.new(self.request, self.response, self.cookies)
31
- self.response.controller = c
32
- self.response.write(c.run)
33
- end
34
- rescue Mack::Errors::ResourceNotFound, Mack::Errors::UndefinedRoute => e
35
- return try_to_find_resource(env, e)
36
- end
37
- end # setup
38
- rescue Exception => e
39
- MACK_DEFAULT_LOGGER.error(e)
40
- raise e
41
- end
42
- end
43
-
44
- private
45
- def log_request
46
- s_time = Time.now
47
- x = yield
48
- e_time = Time.now
49
- p_time = e_time - s_time
50
- if app_config.log.detailed_requests
51
- msg = "\n\t[#{@request.request_method.upcase}] '#{@request.path_info}'\n"
52
- msg << "\tSession ID: #{@request.session.id}\n"
53
- msg << "\tParameters: #{@request.all_params.inspect}\n"
54
- msg << "\tCompleted in #{p_time} (#{(1 / p_time).round} reqs/sec) | #{@response.status} [#{@request.full_host}]"
55
- else
56
- msg = "[#{@request.request_method.upcase}] '#{@request.path_info}' (#{p_time})"
57
- end
58
- MACK_DEFAULT_LOGGER.info(msg)
59
- x
60
- end
61
-
62
- # Setup the request, response, cookies, session, etc...
63
- # yield up, and then clean things up afterwards.
64
- def setup(env)
65
- exception = nil
66
- log_request do
67
- @request = Mack::Request.new(env)
68
- @response = Mack::Response.new
69
- @cookies = Mack::CookieJar.new(self.request, self.response)
70
- session do
71
- begin
72
- yield
73
- rescue Exception => e
74
- exception = e
75
- end
76
- end
77
- end
78
- raise exception if exception
79
- self.response.finish
80
- end
81
-
82
- def session
83
- sess_id = self.cookies[app_config.mack.session_id]
84
- unless sess_id
85
- sess_id = create_new_session
86
- else
87
- sess = Cachetastic::Caches::MackSessionCache.get(sess_id)
88
- if sess
89
- self.request.session = sess
90
- else
91
- # we couldn't find it in the store, so we need to create it:
92
- sess_id = create_new_session
93
- end
94
- end
23
+ require File.join(File.dirname(__FILE__), "initialization", "configuration.rb")
95
24
 
96
- yield
97
-
98
- Cachetastic::Caches::MackSessionCache.set(sess_id, self.request.session)
99
- end
100
-
101
- def create_new_session
102
- id = String.randomize(40).downcase
103
- self.cookies[app_config.mack.session_id] = {:value => id, :expires => nil}
104
- sess = Mack::Session.new(id)
105
- self.request.session = sess
106
- Cachetastic::Caches::MackSessionCache.set(id, sess)
107
- id
25
+ unless Mack::Configuration.initialized
26
+
27
+ puts "Starting application in #{Mack::Configuration.env} mode."
28
+ puts "Mack root: #{Mack::Configuration.root}"
29
+
30
+ require File.join(File.dirname(__FILE__), "initialization", "logging.rb")
31
+
32
+ require File.join(File.dirname(__FILE__), "initialization", "orm_support.rb")
33
+
34
+ fl = File.join(File.dirname(__FILE__))
35
+
36
+ # Require all the necessary files to make Mack actually work!
37
+ ["distributed", "errors", "core_extensions", "utils", "test_extensions", "routing", "view_helpers", "rendering", "controller", "tasks", "initialization/server", "generators"].each do |dir|
38
+ dir_globs = Dir.glob(File.join(fl, dir, "**/*.rb"))
39
+ dir_globs.each do |d|
40
+ require d
108
41
  end
109
-
110
- def try_to_find_resource(env, exception)
111
- env = env.dup
112
- # we can't find a route for this, so let's try and see if it's in the public directory:
113
- if File.extname(env["PATH_INFO"]).blank?
114
- env["PATH_INFO"] << ".html"
115
- end
116
- if File.exists?(File.join(Mack::Configuration.public_directory, env["PATH_INFO"]))
117
- return Rack::File.new(File.join(Mack::Configuration.public_directory)).call(env)
42
+ end
43
+
44
+
45
+
46
+ # ------------------------------------------------------------------------
47
+
48
+ # set up application stuff:
49
+
50
+ # set up routes:
51
+ require File.join(Mack::Configuration.config_directory, "routes")
52
+
53
+ # set up initializers:
54
+ Dir.glob(File.join(Mack::Configuration.config_directory, "initializers", "**/*.rb")) do |d|
55
+ require d
56
+ end
57
+ Mack::Utils::GemManager.instance.do_requires
58
+
59
+ # require 'plugins':
60
+ require File.join(File.dirname(__FILE__), "initialization", "plugins.rb")
61
+
62
+ # make sure that default_controller is available to other controllers
63
+ path = File.join(Mack::Configuration.app_directory, "controllers", "default_controller.rb")
64
+ require path if File.exists?(path)
65
+
66
+ # require 'app' files:
67
+ Dir.glob(File.join(Mack::Configuration.app_directory, "**/*.rb")).each do |d|
68
+ # puts "d: #{d}"
69
+ begin
70
+ require d
71
+ rescue NameError => e
72
+ if e.message.match("uninitialized constant")
73
+ mod = e.message.gsub("uninitialized constant ", "")
74
+ x =%{
75
+ module ::#{mod}
76
+ end
77
+ }
78
+ eval(x)
79
+ require d
118
80
  else
119
- raise exception
81
+ raise e
120
82
  end
121
83
  end
122
-
123
- # This will redirect the request to the specified url. A default status of
124
- # 302, Moved Temporarily, is set if no status is specified. A simple HTML
125
- # page is rendered in case the redirect does not occur.
126
- def redirect_to(route)
127
- status = route[:status] || 302
128
- url = route[:redirect_to]
129
- options = self.request.all_params
130
- options.merge!(route)
131
- options - [:controller, :action, :redirect_to, :method, :status, :format]
132
- url = url_for_pattern(url, options)
133
- self.response.status = status
134
- self.response[:location] = url
135
- self.response.write(redirect_html(self.request.path_info, url, status))
84
+ end
85
+
86
+ # require 'lib' files:
87
+ Dir.glob(File.join(Mack::Configuration.lib_directory, "**/*.rb")).each do |d|
88
+ require d
89
+ end
90
+
91
+
92
+ # ------------------------------------------------------------------------
93
+
94
+ # Include ApplicationHelper into all controllers:
95
+ if Object.const_defined?("ApplicationHelper")
96
+ ApplicationHelper.include_safely_into(Mack::Controller::Base, Mack::Rendering::ViewTemplate)
97
+ end
98
+ # Find other Helpers and include them into their respective controllers.
99
+ Object.constants.collect {|c| c if c.match(/Controller$/)}.compact.each do |cont|
100
+ if Object.const_defined?("#{cont}Helper")
101
+ h = "#{cont}Helper".constantize
102
+ h.include_safely_into(cont, Mack::Rendering::ViewTemplate)
136
103
  end
137
-
138
104
  end
139
- end
105
+
106
+ # Find view level Helpers and include them into the Mack::Rendering::ViewTemplate
107
+ Mack::ViewHelpers.constants.each do |cont|
108
+ h = "Mack::ViewHelpers::#{cont}".constantize
109
+ h.include_safely_into(Mack::Rendering::ViewTemplate)
110
+ end
111
+
112
+ Mack::Configuration.set(:initialized, "true") if Mack::Configuration.initialized.nil?
113
+ end
114
+
115
+ require File.join(File.dirname(__FILE__), "runner")
data/lib/mack_tasks.rb CHANGED
@@ -5,7 +5,7 @@ require 'rubygems'
5
5
  require 'application_configuration'
6
6
 
7
7
  require File.join(File.dirname(__FILE__), "initialization", "configuration.rb")
8
- require File.join(File.dirname(__FILE__), "initialization", "initializers", "orm_support.rb")
8
+ require File.join(File.dirname(__FILE__), "initialization", "orm_support.rb")
9
9
 
10
10
  # Requires all rake tasks that ship with the Mack framework.
11
11
  [File.join(File.dirname(__FILE__)), File.join(FileUtils.pwd, "lib"), File.join(FileUtils.pwd, "vendor", "plugins")].each do |dir|
@@ -0,0 +1,26 @@
1
+ module Mack
2
+ module Rendering # :nodoc:
3
+ module Engine # :nodoc:
4
+ # Engines are used to transform a IO, using a supplied binding to a String.
5
+ #
6
+ # The method 'render' needs to be implemented as render(io, binding) in all subclasses.
7
+ class Base
8
+
9
+ # The Mack::Rendering::ViewTemplate object to be used with this engine.
10
+ attr_reader :view_template
11
+
12
+ def initialize(view_template)
13
+ @view_template = view_template
14
+ end
15
+
16
+ # See Mack::Rendering::ViewTemplate content_for for more details.
17
+ def capture(*args, &block)
18
+ yield
19
+ end
20
+
21
+ needs_method :render
22
+
23
+ end # Base
24
+ end # Engines
25
+ end # Rendering
26
+ end # Mack
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), "..", "view_template")
2
+ module Mack
3
+ module Rendering # :nodoc:
4
+ module Engine # :nodoc:
5
+ # Allows use of the Builder::XmlMarkup engine to be used with rendering.
6
+ class Builder < Mack::Rendering::Engine::Base
7
+
8
+ def render(io, binding)
9
+ @_xml = ::Builder::XmlMarkup.new(:target => @_xml_output, :indent => 1)
10
+ view_template.instance_variable_set("@_xml", @_xml)
11
+ eval(io, binding)
12
+ end
13
+
14
+ def extension
15
+ :builder
16
+ end
17
+
18
+ # Used to give XmlBuilder templates access to a 'root' xml object.
19
+ module ViewTemplateHelpers
20
+ def xml
21
+ @_xml
22
+ end
23
+ end # ViewTemplateHelpers
24
+
25
+ end # Builder
26
+ end # Engine
27
+ end # Rendering
28
+ end # Mack
29
+
30
+ Mack::Rendering::ViewTemplate.send(:include, Mack::Rendering::Engine::Builder::ViewTemplateHelpers)
@@ -0,0 +1,67 @@
1
+ module Mack
2
+ module Rendering # :nodoc:
3
+ module Engine # :nodoc:
4
+ # Allows use of the Builder::XmlMarkup engine to be used with rendering.
5
+ class Erubis < Mack::Rendering::Engine::Base
6
+
7
+ def render(io, binding)
8
+ src = Mack::Rendering::Engine::Erubis::TemplateCache.instance.cache[io]
9
+ if src.nil?
10
+ src = ::Erubis::Eruby.new(io).src
11
+ Mack::Rendering::Engine::Erubis::TemplateCache.instance.cache[io] = src
12
+ end
13
+ eval(src, binding)
14
+ end
15
+
16
+ def extension
17
+ :erb
18
+ end
19
+
20
+ def concat(txt, b)
21
+ eval( "_buf", b) << txt
22
+ end
23
+
24
+ # See Mack::Rendering::ViewTemplate content_for for more details.
25
+ # Thanks Merb.
26
+ def capture(*args, &block)
27
+ # get the buffer from the block's binding
28
+ buffer = _erb_buffer( block.binding ) rescue nil
29
+
30
+ # If there is no buffer, just call the block and get the contents
31
+ if buffer.nil?
32
+ block.call(*args)
33
+ # If there is a buffer, execute the block, then extract its contents
34
+ else
35
+ pos = buffer.length
36
+ block.call(*args)
37
+
38
+ # extract the block
39
+ data = buffer[pos..-1]
40
+
41
+ # replace it in the original with empty string
42
+ buffer[pos..-1] = ''
43
+
44
+ data
45
+ end
46
+ end
47
+
48
+ private
49
+ def _erb_buffer( the_binding ) # :nodoc:
50
+ eval( "_buf", the_binding, __FILE__, __LINE__)
51
+ end
52
+
53
+ class TemplateCache # :nodoc:
54
+ include Singleton
55
+
56
+ attr_reader :cache
57
+
58
+ def initialize
59
+ @cache = {}
60
+ end
61
+
62
+ end
63
+
64
+ end # Erubis
65
+ end # Engines
66
+ end # Rendering
67
+ end # Mack