mack 0.1.0 → 0.2.0

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/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ ===0.2.0
2
+ * ticket: 3 Render url in Controllers/Views
3
+ * ticket: 5 Render Extension System.
4
+
1
5
  ===0.1.0
2
6
  * Added an inflections system. The default inflections are from Jeremy McAnally's great Rails plugin, acts_as_good_speeler. Thanks Jeremy! http://www.jeremymcanally.com/
3
7
  * Added a to_params method to Hash to help with testing.
data/lib/errors/errors.rb CHANGED
@@ -83,5 +83,22 @@ module Mack
83
83
  end
84
84
  end
85
85
 
86
+ # Potentially raised if a render(:url => "....") is a status other than 200.
87
+ # This is only raised if :raise_exception is passed in as true to the render options.
88
+ class UnsuccessfulRenderUrl < StandardError
89
+ # Takes the uri trying to be rendered the Net::HTTP response object.
90
+ def initialize(uri, response)
91
+ super("URI: #{uri}; status: #{response.code}; body: #{response.body}")
92
+ end
93
+ end
94
+
95
+ # Raised if an unsupported method, ie post or delete, is used with render url.
96
+ class UnsupportRenderUrlMethodType < StandardError
97
+ # Takes the method tried.
98
+ def initialize(method)
99
+ super("METHOD: #{method.to_s.upcase} is unsupported by render url.")
100
+ end
101
+ end
102
+
86
103
  end # Errors
87
104
  end # Mack
@@ -11,7 +11,7 @@ class PluginGenerator < Mack::Generator::Base
11
11
 
12
12
  require_param :name
13
13
 
14
- def generate
14
+ def generate # :nodoc:
15
15
  plugin_dir = File.join(MACK_ROOT, "vendor", "plugins", param(:name).downcase)
16
16
  template_dir = File.join(File.dirname(__FILE__), "templates")
17
17
 
@@ -2,16 +2,11 @@
2
2
  #
3
3
  # Example:
4
4
  # rake generate:scaffold name=post
5
- # This will generate the following in your mack application:
6
- # vendor/plugins/my_cool_plugin
7
- # vendor/plugins/my_cool_plugin/init.rb
8
- # vendor/plugins/my_cool_plugin/lib
9
- # vendor/plugins/my_cool_plugin/lib/my_cool_plugin.rb
10
5
  class ScaffoldGenerator < Mack::Generator::Base
11
6
 
12
7
  require_param :name
13
8
 
14
- def generate
9
+ def generate # :nodoc:
15
10
  @name_singular = param(:name).singular
16
11
  @name_plural = param(:name).plural
17
12
  @name_singular_camel = @name_singular.camelcase
@@ -40,6 +40,7 @@ module Mack
40
40
  # use local memory and store stuff for 5 minutes:
41
41
  DEFAULTS_DEVELOPMENT = {
42
42
  "mack::cache_classes" => false,
43
+ "mack::default_domain" => "http://localhost:3000",
43
44
  "log::level" => "debug",
44
45
  "log::console" => true,
45
46
  "cachetastic_default_options" => {
@@ -57,7 +58,10 @@ module Mack
57
58
 
58
59
  # use local memory and store stuff for 1 hour:
59
60
  DEFAULTS_TEST = {
61
+ "mack::default_domain" => "http://localhost",
62
+ "mack::default_domain_port" => 6666,
60
63
  "log::level" => "error",
64
+ "run_remote_tests" => true,
61
65
  "cachetastic_default_options" => {
62
66
  "debug" => false,
63
67
  "adapter" => "local_memory",
@@ -73,10 +77,12 @@ module Mack
73
77
 
74
78
  unless self.const_defined?("DEFAULTS")
75
79
  DEFAULTS = {
80
+ "mack::render_url_timeout" => 5,
76
81
  "mack::cache_classes" => true,
77
82
  "mack::use_lint" => true,
78
83
  "mack::show_exceptions" => true,
79
84
  "mack::session_id" => "_mack_session_id",
85
+ "mack::rendering_systems" => [:action, :text, :partial, :public, :url],
80
86
  "mack::cookie_values" => {
81
87
  "path" => "/"
82
88
  },
@@ -40,7 +40,7 @@ unless Object.const_defined?("MACK_INITIALIZED")
40
40
  fl = File.join(File.dirname(__FILE__), "..")
41
41
 
42
42
  # Require all the necessary files to make Mack actually work!
43
- ["errors", "core_extensions", "utils", "test_extensions", "routing", "sea_level", "tasks", "initialization/server", "generators"].each do |dir|
43
+ ["errors", "core_extensions", "utils", "test_extensions", "routing", "rendering", "sea_level", "tasks", "initialization/server", "generators"].each do |dir|
44
44
  dir_globs = Dir.glob(File.join(fl, dir, "**/*.rb"))
45
45
  dir_globs.each do |d|
46
46
  require d
@@ -0,0 +1,58 @@
1
+ module Mack
2
+ module Rendering
3
+ # This is the base class from which all rendering systems need to extend.
4
+ #
5
+ # Example:
6
+ # class Mack::Rendering::Pdf < Mack::Rendering::Base
7
+ # def render
8
+ # # do work to render stuff as a PDF
9
+ # end
10
+ # end
11
+ #
12
+ # Now add this to the list of available render systems:
13
+ # app_config.mack.rendering_systems << :pdf
14
+ #
15
+ # You should now be able to do this in your controller:
16
+ #
17
+ # class MyAwesomeController < Mack::Controller::Base
18
+ # def pdf
19
+ # render(:pdf => "my_pdf_template")
20
+ # end
21
+ # end
22
+ class Base
23
+
24
+ attr_accessor :view_binder # The Mack::ViewBinder that called this rendering system.
25
+ attr_accessor :options # The options associated with this render call
26
+
27
+ def initialize(view_binder, options)
28
+ self.view_binder = view_binder
29
+ self.options = options
30
+ end
31
+
32
+ # This is the only method that needs to be implemented by a rendering system.
33
+ # It should return a String.
34
+ def render
35
+ raise MethodNotImplemented.new("render")
36
+ end
37
+
38
+ private
39
+ # Used to render a file from disk.
40
+ def render_file(f, options = {})
41
+ options = {:is_partial => false, :ext => ".html.erb", :dir => MACK_VIEWS}.merge(options)
42
+ partial = f.to_s
43
+ parts = partial.split("/")
44
+ if parts.size == 1
45
+ # it's local to this controller
46
+ partial = "_" << partial if options[:is_partial]
47
+ partial = File.join(options[:dir], self.view_binder.controller.controller_name, partial + options[:ext])
48
+ else
49
+ # it's elsewhere
50
+ parts[parts.size - 1] = "_" << parts.last if options[:is_partial]
51
+ partial = File.join(options[:dir], parts.join("/") + options[:ext])
52
+ end
53
+ return Mack::ViewBinder.render(File.open(partial).read, self.view_binder.controller, options)
54
+ end
55
+
56
+ end # Base
57
+ end # Rendering
58
+ end # Mack
@@ -0,0 +1,26 @@
1
+ module Mack
2
+ module Rendering
3
+ # Used when someone calls render(:action => "index")
4
+ class Action < Base
5
+
6
+ def render
7
+ begin
8
+ # Try to render the action:
9
+ return render_file(options[:action], options)
10
+ rescue Errno::ENOENT => e
11
+ begin
12
+ # If the action doesn't exist on disk, try to render it from the public directory:
13
+ t = render_file(options[:action], {:dir => MACK_PUBLIC, :ext => ".html", :layout => false}.merge(options))
14
+ # Because it's being served from public don't wrap a layout around it!
15
+ # self.controller.instance_variable_get("@render_options").merge!({:layout => false})
16
+ return t
17
+ rescue Errno::ENOENT => ex
18
+ end
19
+ # Raise the original exception because something bad has happened!
20
+ raise e
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ module Mack
2
+ module Rendering
3
+ # Used when someone calls render(:partial => "latest_news")
4
+ class Partial < Base
5
+
6
+ def render
7
+ render_file(options[:partial], {:is_partial => true}.merge(options))
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Mack
2
+ module Rendering
3
+ # Used when someone calls render(:public => "index")
4
+ # This renders a file from the public directory.
5
+ class Public < Base
6
+
7
+ def render
8
+ render_file(options[:public], {:dir => MACK_PUBLIC, :ext => ".html", :layout => false}.merge(options))
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Mack
2
+ module Rendering
3
+ # Used when someone calls render(:text => "Hello World!")
4
+ class Text < Base
5
+
6
+ def render
7
+ Mack::ViewBinder.render(options[:text], self.view_binder.controller, options)
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,58 @@
1
+ require 'net/http'
2
+ module Mack
3
+ module Rendering
4
+ # Used when someone calls render(:url => "http://www.mackframework.com")
5
+ class Url < Base
6
+
7
+ def render
8
+ options = {:method => :get, :domain => app_config.mack.default_domain, :raise_exception => false}.merge(self.options)
9
+ case options[:method]
10
+ when :get
11
+ do_render_url(options) do |uri, options|
12
+ unless options[:parameters].empty?
13
+ uri = uri.to_s
14
+ uri << "?"
15
+ options[:parameters].each_pair do |k,v|
16
+ uri << URI.encode(k.to_s)
17
+ uri << "="
18
+ uri << URI.encode(v.to_s)
19
+ uri << "&"
20
+ end
21
+ uri.gsub!(/&$/, "")
22
+ uri = URI.parse(uri)
23
+ end
24
+ Net::HTTP.get_response(uri)
25
+ end
26
+ when :post
27
+ do_render_url(options) do |uri, options|
28
+ Net::HTTP.post_form(uri, options[:parameters] || {})
29
+ end
30
+ else
31
+ raise Mack::Errors::UnsupportRenderUrlMethodType.new(options[:method])
32
+ end
33
+ end
34
+
35
+ private
36
+ def do_render_url(options)
37
+ Timeout::timeout(app_config.mack.render_url_timeout || 5) do
38
+ url = options[:url]
39
+ unless url.match(/^[a-zA-Z]+:\/\//)
40
+ url = File.join(options[:domain], options[:url])
41
+ end
42
+ uri = URI.parse(url)
43
+ response = yield uri, options
44
+ if response.code == "200"
45
+ return response.body
46
+ else
47
+ if options[:raise_exception]
48
+ raise Mack::Errors::UnsuccessfulRenderUrl.new(uri, response)
49
+ else
50
+ return ""
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -148,6 +148,32 @@ module Mack
148
148
  # def latest_news
149
149
  # render(:partial => "some_other/old_news")
150
150
  # end
151
+ #
152
+ # # This will render a url. If the url does not return a status code of '200',
153
+ # # an empty string will be returned by default. The default method for rendering
154
+ # # urls is a get.
155
+ # def yahoo
156
+ # render(:url => "http://www.yahoo.com")
157
+ # end
158
+ #
159
+ # # This will render a url. If the url does not return a status code of '200',
160
+ # # a Mack::Errors::UnsuccessfulRenderUrl exception will be raised.
161
+ # def idontexist
162
+ # render(:url => "http://www.idontexist.com", :raise_exception => true)
163
+ # end
164
+ #
165
+ # # This will render a url with a post.
166
+ # def post_to_somewhere
167
+ # render(:url => "http://www.mackframework.com/post_to_me", :method => :post,
168
+ # :parameters => {:id => 1, :user => "markbates"})
169
+ # end
170
+ #
171
+ # # This will render a 'local' url. If a domain is not present render url will
172
+ # # reach out for the config parameter "mack::default_domain" and prepend that
173
+ # # to the url. This can be overridden locally with the :domain option.
174
+ # def get_index
175
+ # render(:url => "/")
176
+ # end
151
177
  # end
152
178
  def render(options = {:action => self.action_name})
153
179
  raise Mack::Errors::DoubleRender.new if render_performed?
@@ -32,52 +32,22 @@ class Mack::ViewBinder
32
32
  # <%= render(:text => "Hello") %>
33
33
  # <%= render(:action => "show") %>
34
34
  # <%= render(:partial => :latest_news) %>
35
+ # <%= render(:url => "http://www.mackframework.com") %>
35
36
  def render(options = {})
36
- if options[:action]
37
- begin
38
- # Try to render the action:
39
- return render_file(options[:action], options)
40
- rescue Errno::ENOENT => e
37
+ app_config.mack.rendering_systems.each do |render_option|
38
+ if options[render_option]
41
39
  begin
42
- # If the action doesn't exist on disk, try to render it from the public directory:
43
- t = render_file(options[:action], {:dir => MACK_PUBLIC, :ext => ".html", :layout => false}.merge(options))
44
- # Because it's being served from public don't wrap a layout around it!
45
- # self.controller.instance_variable_get("@render_options").merge!({:layout => false})
46
- return t
47
- rescue Errno::ENOENT => ex
40
+ rc = "Mack::Rendering::#{render_option.to_s.camelcase}".constantize.new(self, options)
41
+ rescue Exception => e
42
+ raise Mack::Errors::UnknownRenderOption.new(options)
48
43
  end
49
- # Raise the original exception because something bad has happened!
50
- raise e
44
+ return rc.render
51
45
  end
52
- elsif options[:text]
53
- return Mack::ViewBinder.render(options[:text], self.controller, options)
54
- elsif options[:partial]
55
- return render_file(options[:partial], {:is_partial => true}.merge(options))
56
- elsif options[:public]
57
- t = render_file(options[:public], {:dir => MACK_PUBLIC, :ext => ".html", :layout => false}.merge(options))
58
- # self.controller.instance_variable_get("@render_options").merge!({:layout => false})
59
- return t
60
- else
61
- raise Mack::UnknownRenderOption.new(options)
62
46
  end
47
+ raise Mack::Errors::UnknownRenderOption.new(options)
63
48
  end
64
49
 
65
50
  private
66
- def render_file(f, options = {})
67
- options = {:is_partial => false, :ext => ".html.erb", :dir => MACK_VIEWS}.merge(options)
68
- partial = f.to_s
69
- parts = partial.split("/")
70
- if parts.size == 1
71
- # it's local to this controller
72
- partial = "_" << partial if options[:is_partial]
73
- partial = File.join(options[:dir], self.controller.controller_name, partial + options[:ext])
74
- else
75
- # it's elsewhere
76
- parts[parts.size - 1] = "_" << parts.last if options[:is_partial]
77
- partial = File.join(options[:dir], parts.join("/") + options[:ext])
78
- end
79
- return Mack::ViewBinder.render(File.open(partial).read, self.controller, options)
80
- end
81
51
 
82
52
  # Transfer instance variables from the controller to the view.
83
53
  def transfer_vars(x)
@@ -3,6 +3,12 @@ require "test/unit"
3
3
  module Mack
4
4
 
5
5
  module TestHelpers
6
+
7
+ def remote_test
8
+ if (app_config.run_remote_tests)
9
+ yield
10
+ end
11
+ end
6
12
 
7
13
  # Performs a 'get' request for the specified uri.
8
14
  def get(uri, options = {})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-04 00:00:00 -05:00
12
+ date: 2008-03-11 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -122,6 +122,12 @@ files:
122
122
  - lib/initialization/server/simple_server.rb
123
123
  - lib/mack.rb
124
124
  - lib/mack_tasks.rb
125
+ - lib/rendering/base.rb
126
+ - lib/rendering/classes/action.rb
127
+ - lib/rendering/classes/partial.rb
128
+ - lib/rendering/classes/public.rb
129
+ - lib/rendering/classes/text.rb
130
+ - lib/rendering/classes/url.rb
125
131
  - lib/routing/route_map.rb
126
132
  - lib/routing/urls.rb
127
133
  - lib/sea_level/controller_base.rb