Skip to content

Commit 121a264

Browse files
authored
Use the official Sinatra extensions API (#10)
1 parent 5b3bf8e commit 121a264

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
# phlex-sinatra
22

3-
[Phlex](https://github.com/phlex-ruby/phlex) already works with Sinatra (and everything else) but its normal usage leaves you without access to Sinatra's standard helper methods. This integration lets you use the `url()` helper method from within a Phlex view (along with the rest of the helper methods available in a Sinatra action).
3+
[Phlex](https://github.com/phlex-ruby/phlex) already works with Sinatra (and everything else) but its normal usage leaves you without access to Sinatra's standard helper methods. This extension lets you use Sinatra's `url()` helper method from within a Phlex view (along with the rest of the helper methods available in a Sinatra action).
44

55
## Installation
66

7-
Add phlex-sinatra to your application's Gemfile and run `bundle install`.
7+
Add phlex-sinatra to your application's `Gemfile` and run `bundle install`.
88

99
```ruby
1010
gem 'phlex-sinatra'
1111
```
1212

13+
Then require it in your app:
14+
15+
```ruby
16+
require 'phlex-sinatra'
17+
```
18+
1319
## Usage
1420

15-
To enable the integration use the `phlex` method in your Sinatra action and pass an _instance_ of the Phlex view (instead of using `.call` to get its output):
21+
Use the `phlex` helper method to render a Phlex view and pass an _instance_ of the Phlex view (instead of using `.call` to get its output). For a classic-style Sinatra app it'll Just Work™:
1622

1723
```ruby
1824
get '/foo' do
1925
phlex MyView.new
2026
end
2127
```
2228

29+
For a modular app the extension must be explicitly registered:
30+
31+
```ruby
32+
class MyApp < Sinatra::Base
33+
helpers Phlex::Sinatra
34+
35+
get '/foo' do
36+
phlex MyView.new
37+
end
38+
end
39+
```
40+
2341
You can now use Sinatra's `url()` helper method directly and its other methods (`params`, `request`, etc) via the `helpers` proxy:
2442

2543
```ruby

lib/phlex-sinatra.rb

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'phlex'
4+
require 'sinatra/base'
45
require_relative 'phlex/sinatra/version'
56

67
module Phlex
@@ -18,40 +19,30 @@ def initialize(obj)
1819
end
1920
end
2021

21-
module SGML
22-
module Overrides
23-
def helpers
24-
@_view_context
25-
end
22+
module SGMLOverrides
23+
def helpers
24+
@_view_context
25+
end
2626

27-
def url(...)
28-
helpers.url(...)
29-
end
27+
def url(...)
28+
helpers.url(...)
3029
end
3130
end
32-
end
3331

34-
class SGML
35-
include Sinatra::SGML::Overrides
36-
end
37-
end
38-
39-
module Sinatra
40-
module Templates
4132
def phlex(
4233
obj,
4334
content_type: nil,
4435
layout: false,
4536
layout_engine: :erb,
4637
stream: false
4738
)
48-
raise Phlex::Sinatra::TypeError.new(obj) unless obj.is_a?(Phlex::SGML)
39+
raise TypeError.new(obj) unless obj.is_a?(SGML)
4940

5041
if layout && stream
51-
raise Phlex::Sinatra::ArgumentError.new('streaming is not compatible with layout')
42+
raise ArgumentError.new('streaming is not compatible with layout')
5243
end
5344

54-
content_type ||= :svg if obj.is_a?(Phlex::SVG) && !layout
45+
content_type ||= :svg if obj.is_a?(SVG) && !layout
5546
self.content_type(content_type) if content_type
5647

5748
# Copy Sinatra's behaviour and interpret layout=true as meaning "use the
@@ -73,4 +64,8 @@ def phlex(
7364
end
7465
end
7566
end
67+
68+
SGML.include Sinatra::SGMLOverrides
7669
end
70+
71+
Sinatra.helpers Phlex::Sinatra

spec/general_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ def view_template
3636
end
3737
end
3838

39-
class TestApp < Sinatra::Application
39+
class TestApp < Sinatra::Base
40+
helpers Phlex::Sinatra
41+
4042
set :environment, :test
4143

4244
get '/error' do

spec/spec_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require 'phlex-sinatra'
44
require 'rack/test'
5-
require 'sinatra/base'
65

76
RSpec.configure do |config|
87
# Enable flags like --only-failures and --next-failure

spec/streaming_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ def view_template
1515
end
1616
end
1717

18-
class StreamingApp < Sinatra::Application
18+
class StreamingApp < Sinatra::Base
19+
helpers Phlex::Sinatra
20+
1921
set :environment, :test
2022

2123
get '/stream' do

0 commit comments

Comments
 (0)