roy 0.1.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- roy (0.0.1)
4
+ roy (0.2.1)
5
5
  rack
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -2,16 +2,16 @@ Roy
2
2
  ===
3
3
 
4
4
  Roy is a tiny module that aims to make any Ruby object Rack-friendly and
5
- provides it with a REST-like interface.
5
+ provide it with a REST-like interface.
6
6
 
7
7
  Roy tries to be as less invasive as possible. It provides your objects with a
8
- `#call` method that takes a Rack environment and dispatchs to a regular method
8
+ `#call` method that takes a Rack environment and dispatches to a regular method
9
9
  named after the HTTP method you want to catch.
10
10
 
11
11
  ## Tests
12
12
 
13
13
  You can execute the tests by running `rake test`. They are written with
14
- Minitest.
14
+ MiniTest.
15
15
 
16
16
  ## Example
17
17
 
@@ -59,10 +59,10 @@ class Example
59
59
  end
60
60
  end
61
61
  ```
62
- ### Environement
62
+ ### Environment
63
63
 
64
64
  Inside your handler methods, you have access to a `roy` readable attribute which
65
- is a struct containing the following fields:
65
+ is a Struct containing the following fields:
66
66
 
67
67
  * `env`: the Rack environment
68
68
  * `response`: a `Rack::Response` object that will be returned by `call`
@@ -82,3 +82,15 @@ composed of a status code and a message.
82
82
  Roy provides a `halt` method that takes a status code and an optional message.
83
83
  If there is no message it uses the default message from
84
84
  `Rack::Utils::HTTP_STATUS_CODES`
85
+
86
+ ### Plugins
87
+
88
+ Various plugins are shipped with Roy, here is the full list:
89
+
90
+ * **after**: modify the response after the app has been called
91
+ * **before**: modify the environment before calling the app
92
+ * **render**: integration with Tilt
93
+ * **plugins**: a simple plugin loader
94
+
95
+ Each plugin is designed to do only one thing. Thus it is very easy to take a
96
+ look at the code and see how the plugin works.
data/lib/roy.rb CHANGED
@@ -5,6 +5,7 @@ require 'roy/version'
5
5
 
6
6
  module Roy
7
7
  Env = Struct.new(:env, :request, :response, :headers, :params, :conf)
8
+ Defaults = OpenStruct.new(allow: Set.new, prefix: :'')
8
9
 
9
10
  def self.included(base)
10
11
  base.send(:extend, ClassMethods)
@@ -52,16 +53,16 @@ module Roy
52
53
  module ClassMethods
53
54
  attr_reader :conf
54
55
 
55
- def roy(options={})
56
- @conf ||= OpenStruct.new(allow: Set.new, prefix: :'')
56
+ def self.extended(base)
57
+ base.instance_eval { @conf ||= Defaults.dup }
58
+ end
57
59
 
60
+ def roy(options={})
58
61
  options.each do |k,v|
59
62
  case k
60
63
  when :allow
61
64
  conf.allow.merge(v)
62
65
  conf.allow.add(:head) if v.member?(:get)
63
- when :prefix
64
- conf.prefix = v
65
66
  else
66
67
  conf.send(:"#{k}=", v)
67
68
  end
@@ -2,6 +2,10 @@ module Roy
2
2
  def self.Plugins(*names)
3
3
  return Module.new.tap do |mod|
4
4
  names.each do |name|
5
+ if name.is_a?(Symbol)
6
+ require "roy/#{name}"
7
+ name = Roy.const_get("#{name}".capitalize.to_sym)
8
+ end
5
9
  mod.send(:include, name)
6
10
  end
7
11
  end
@@ -2,14 +2,20 @@ require 'tilt'
2
2
 
3
3
  module Roy
4
4
  module Render
5
- def render(engine, template_or_string, params={}, &block)
6
- case template_or_string
5
+
6
+ def render(engine, view_or_string, params={}, &block)
7
+ options = roy.conf.render || {}
8
+ template = case view_or_string
7
9
  when Symbol
10
+ file = [view_or_string.to_s, engine].map(&:to_s).join('.')
11
+ dir = roy.conf.views || 'views'
12
+ Tilt.new(File.join(dir, file), nil, options)
8
13
  else
9
- Tilt[engine].new {
10
- template_or_string.to_s
11
- }.render(self, params, &block)
14
+ Tilt[engine].new(nil, nil, options) { view_or_string.to_s }
12
15
  end
16
+
17
+ template.render(self, params, &block)
13
18
  end
19
+
14
20
  end
15
21
  end
@@ -1,3 +1,3 @@
1
1
  module Roy
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -0,0 +1,25 @@
1
+ require_relative 'helper'
2
+
3
+ defaults = Roy::Defaults.dup
4
+ Roy::Defaults.prefix = :http_
5
+
6
+ class DefaultsTestObject
7
+ include Roy
8
+
9
+ def http_get(*args)
10
+ 'success'
11
+ end
12
+ end
13
+
14
+ class DefaultsTest < MiniTest::Unit::TestCase
15
+ include Rack::Test::Methods
16
+
17
+ def app
18
+ DefaultsTestObject.new
19
+ end
20
+
21
+ def test_default_settings
22
+ get '/'
23
+ assert_equal 'success', last_response.body
24
+ end
25
+ end
@@ -1,11 +1,10 @@
1
1
  require_relative 'helper'
2
2
  require 'roy/plugins'
3
3
  require 'roy/before'
4
- require 'roy/after'
5
4
 
6
5
  class PluginsTestObject
7
6
  include Roy
8
- include Roy::Plugins(Before, After)
7
+ include Roy::Plugins(Before, :after)
9
8
 
10
9
  roy allow: [:get],
11
10
  before: lambda { |env|
@@ -12,12 +12,17 @@ class RenderTestObject
12
12
  include Roy
13
13
  include Roy::Render
14
14
 
15
- roy allow: [:get]
15
+ roy allow: [:get],
16
+ views: 'test/views'
16
17
 
17
18
  def get(*args)
18
19
  case args.first
19
20
  when 'template'
20
- halt 403
21
+ render :erb, :test
22
+ when 'template_layout'
23
+ render :erb, :layout do
24
+ render :erb, :test
25
+ end
21
26
  when 'inline'
22
27
  render :erb, Templates[:simple]
23
28
  when 'locals'
@@ -45,7 +50,12 @@ class RenderTest < MiniTest::Unit::TestCase
45
50
 
46
51
  def test_render_file_template
47
52
  get '/template'
48
- fail!
53
+ assert_equal inline(:simple), last_response.body
54
+ end
55
+
56
+ def test_render_file_layout
57
+ get '/template_layout'
58
+ assert_equal inline(:simple), last_response.body
49
59
  end
50
60
 
51
61
  def test_render_inline_template
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -0,0 +1 @@
1
+ <%= "Hello world!" %>
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: roy
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - madx
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-23 00:00:00 Z
13
+ date: 2011-10-29 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
@@ -84,10 +84,13 @@ files:
84
84
  - test/base_test.rb
85
85
  - test/before_test.rb
86
86
  - test/custom_options_test.rb
87
+ - test/defaults_test.rb
87
88
  - test/helper.rb
88
89
  - test/plugins_test.rb
89
90
  - test/prefix_test.rb
90
91
  - test/render_test.rb
92
+ - test/views/layout.erb
93
+ - test/views/test.erb
91
94
  homepage: https://github.com/madx/roy
92
95
  licenses: []
93
96
 
@@ -120,7 +123,10 @@ test_files:
120
123
  - test/base_test.rb
121
124
  - test/before_test.rb
122
125
  - test/custom_options_test.rb
126
+ - test/defaults_test.rb
123
127
  - test/helper.rb
124
128
  - test/plugins_test.rb
125
129
  - test/prefix_test.rb
126
130
  - test/render_test.rb
131
+ - test/views/layout.erb
132
+ - test/views/test.erb