sinatra-fuzzy_layout 0.0.3

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7fc2719e1a696966d44c55d0ecc4a7bbfbd13724
4
+ data.tar.gz: 418e7bc2ce86f54f786df88fb3e15ac70d92f809
5
+ SHA512:
6
+ metadata.gz: 8a5f11a749f273bd4bba5d4e2244956085cd2fb98c6f725a36e06c73d0c63c0ae06f291fd9918c67ce60ce336de8a904433bbeb9ea64f498a92783e241306aca
7
+ data.tar.gz: a04031df07c8bfa493c94adea7965be242eec0737de0c157c468e4b0d7f32e942c917b6960c399ecac32515054808b0a1a6e8f721a35d47c9a0c21529cecbf83
@@ -0,0 +1,133 @@
1
+ # Sinatra::FuzzyLayout
2
+
3
+ Enable or Disable `layout.<extension>` via regex.
4
+
5
+ How many times did you wished you could do something like this:
6
+
7
+ ```ruby
8
+
9
+ class App < Sinatra::Base
10
+
11
+ register Sinatra::FuzzyLayout
12
+
13
+ enable_layout_for "home", :admin
14
+ disable_layout_for /.+_reports/
15
+
16
+ get '/' do
17
+ haml :index # layout.haml is used
18
+ end
19
+
20
+ get '/admin' do
21
+ haml :admin # layout.haml is used
22
+ end
23
+
24
+ get '/home' do
25
+ haml :home # layout.haml is used
26
+ end
27
+
28
+ get '/system_reports' do
29
+ haml :system_reports # layout.haml is not used.
30
+ end
31
+
32
+ end
33
+ ```
34
+
35
+ Never?
36
+
37
+ k.
38
+
39
+
40
+ ## Installation
41
+
42
+ Run this:
43
+
44
+ gem install 'sinatra-fuzzy_layout'
45
+
46
+ Or, if your use bundler like the pros, add this line to
47
+ your application's Gemfile:
48
+
49
+ gem 'sinatra-fuzzy_layout'
50
+
51
+ And then execute:
52
+
53
+ $ bundle
54
+
55
+ ## Usage
56
+
57
+ Once the gem is installed, add this in your app file:
58
+
59
+ require 'sinatra/fuzzy_layout'
60
+
61
+
62
+ #### Classic Style
63
+
64
+ Once you `require` the gem, you can define the `enable_layout_for` and/or
65
+ `disable_layout_for` end-points
66
+
67
+ #### Modular Style
68
+
69
+ An additional step in case of Modular style apps is :
70
+
71
+ ```ruby
72
+ require "sinatra/fuzzy_layout" # just like in classic style
73
+
74
+ class App < Sinatra::Base
75
+
76
+ register Sinatra::FuzzyLayout
77
+
78
+ enable_layout_for :a_template
79
+ end
80
+ ```
81
+
82
+ Done.
83
+
84
+ ## How it works
85
+
86
+ (Note: if you use a different templating engine like `slim` or `erb`,
87
+ replace all instances of `haml` with that engine here on)
88
+
89
+ When the app encounters a `haml :view` in a route,
90
+ it tries to render that view by reading the `view.haml` file and checking
91
+ to see if a `layout` option is defined. This usually is set at a global
92
+ level in the app `set :haml, :layout => false` or set on a per-route
93
+ basis `haml :view, :layout => false`. If the layout is required, then the
94
+ `layout.haml` is also parsed and the `view.haml` part is added to the
95
+ layout.
96
+
97
+ This extension lets you define the layout options at a global level.
98
+ It modifies the `render` method in `Sinatra::Templates` so that the templates
99
+ are checked against the regexes defined and the layout option is set
100
+ (or unset) based on the result.
101
+
102
+
103
+ A template is first validated against the `enable_layouts_for` options. If the
104
+ `enable_layouts_for` directive is not defined in the app however, the check
105
+ only happens against `disable_layouts_for`. If the first check returns true,
106
+ i.e., if the layout is enabled for that template, the second check never
107
+ happens. This means that if you define a template in both the
108
+ `enable_layouts_for` and `disable_layouts_for` directives, the layout gets enabled
109
+ since the first check returns true.
110
+
111
+ Typically, only one template is used in each route (duh!). This means
112
+ that the regex checks happen only one in the best case and
113
+ twice in the worst case and so should not affect performance by a large
114
+ margin. However, if you are writing at webscale, you might want to skip
115
+ this.
116
+
117
+ Even if you use this plugin, it won't interfere with the normal Sinatra
118
+ layout directives (global and per-route) and so, you can mix-and-match if
119
+ you're into it. If you're still uncertain whether or not to use it, but
120
+ want to, I'd say go ahead.
121
+
122
+
123
+ ## Contributing
124
+
125
+ Lulz. Really?
126
+
127
+ Okay.
128
+
129
+ 1. Fork it
130
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
131
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
132
+ 4. Push to the branch (`git push origin my-new-feature`)
133
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,69 @@
1
+ require "sinatra/base"
2
+
3
+ class Array
4
+ def has_the_template?(arg)
5
+ self.select { |x| x.check arg }.any?
6
+ end
7
+ end
8
+
9
+ class Regexp
10
+ def check(arg)
11
+ false
12
+ true if self =~ arg
13
+ end
14
+ end
15
+
16
+ class String
17
+ def check(arg)
18
+ self === arg.to_s
19
+ end
20
+ end
21
+
22
+
23
+ class Symbol
24
+ def check(arg)
25
+ self === arg.to_sym
26
+ end
27
+ end
28
+
29
+ module Sinatra
30
+
31
+ module FuzzyLayout
32
+
33
+ module TemplatesHelpers
34
+ include Sinatra::Templates
35
+
36
+ def render(engine, data, options = {}, locals = {}, &block)
37
+ options = get_new_options_and_template(options, data)
38
+ super
39
+ end
40
+
41
+ private
42
+
43
+ def get_new_options_and_template(options, template)
44
+ if settings.enable_list.has_the_template?(template)
45
+ options[:layout] = true
46
+ elsif settings.disable_list.has_the_template?(template)
47
+ options[:layout] = false
48
+ end
49
+
50
+ options
51
+ end
52
+ end
53
+
54
+ def enable_layout_for(*templates)
55
+ Base.set :enable_list, templates || []
56
+ end
57
+
58
+ def disable_layout_for(*templates)
59
+ Base.set :disable_list, templates || []
60
+ end
61
+
62
+ def self.registered(app)
63
+ app.helpers FuzzyLayout::TemplatesHelpers
64
+ end
65
+ end
66
+
67
+ register FuzzyLayout
68
+ end
69
+
@@ -0,0 +1,85 @@
1
+ require_relative "spec_helper"
2
+ require_relative "sample_app"
3
+ require_relative "sample_app_multiple"
4
+
5
+ describe 'Test App with single declaration of enable or disable directives' do
6
+
7
+ def app
8
+ SampleApp
9
+ end
10
+
11
+ it "disables layout if route level :layout => false is set" do
12
+ get('/')
13
+ last_response.should be_ok
14
+ last_response.body.should == "<em>Spartaa!!</em>\n"
15
+ end
16
+
17
+ it "enables layout when layout is both enabled and disabled" do
18
+ get('/one')
19
+ last_response.should be_ok
20
+ last_response.body.should == "<h1>This is <em>one</em></h1>\n"
21
+ end
22
+
23
+ it "works when template is specified as a string" do
24
+ get('/two')
25
+ last_response.should be_ok
26
+ last_response.body.should == "<em>two</em>\n"
27
+ end
28
+
29
+ context "works when template is specified as a regex" do
30
+ it "should not have a layout for the route four" do
31
+ get('/four')
32
+ last_response.should be_ok
33
+ last_response.body.should == "<em>four</em>\n"
34
+ end
35
+
36
+ it "should not have a layout for the route five" do
37
+ get('/five')
38
+ last_response.should be_ok
39
+ last_response.body.should == "<em>five</em>\n"
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+
46
+
47
+ describe 'Test App with multiple declarations of enable directives' do
48
+
49
+ def app
50
+ SampleAppMultiple
51
+ end
52
+
53
+ it "disables layout if route level :layout => false is set" do
54
+ get('/')
55
+ last_response.should be_ok
56
+ last_response.body.should == "<em>Spartaa!!</em>\n"
57
+ end
58
+
59
+ it "enables layout when layout is both enabled and disabled" do
60
+ get('/one')
61
+ last_response.should be_ok
62
+ last_response.body.should == "<h1>This is <em>one</em></h1>\n"
63
+ end
64
+
65
+ it "works when template is specified as a string" do
66
+ get('/two')
67
+ last_response.should be_ok
68
+ last_response.body.should == "<em>two</em>\n"
69
+ end
70
+
71
+ context "works when template is specified as a regex" do
72
+ it "should not have a layout for the route four" do
73
+ get('/four')
74
+ last_response.should be_ok
75
+ last_response.body.should == "<em>four</em>\n"
76
+ end
77
+
78
+ it "should not have a layout for the route five" do
79
+ get('/five')
80
+ last_response.should be_ok
81
+ last_response.body.should == "<em>five</em>\n"
82
+ end
83
+ end
84
+
85
+ end
@@ -0,0 +1,20 @@
1
+ require "sinatra/base"
2
+ require "haml"
3
+ require "sinatra/fuzzy_layout"
4
+
5
+ class SampleApp < Sinatra::Base
6
+ register Sinatra::FuzzyLayout
7
+
8
+ enable_layout_for :one
9
+ disable_layout_for "one", "two", /f\w{3}/
10
+
11
+ get '/' do
12
+ haml :index, :layout => false
13
+ end
14
+
15
+ get('/one') { haml :one }
16
+ get('/two') { haml :two }
17
+ get('/three') { haml :three }
18
+ get('/four') { haml :four }
19
+ get('/five') { haml :five }
20
+ end
@@ -0,0 +1,21 @@
1
+ require "sinatra/base"
2
+ require "haml"
3
+ require "sinatra/fuzzy_layout"
4
+
5
+ class SampleAppMultiple < Sinatra::Base
6
+ register Sinatra::FuzzyLayout
7
+
8
+ enable_layout_for :one
9
+ disable_layout_for "one"
10
+ disable_layout_for "two", /f\w{3}/
11
+
12
+ get '/' do
13
+ haml :index, :layout => false
14
+ end
15
+
16
+ get('/one') { haml :one }
17
+ get('/two') { haml :two }
18
+ get('/three') { haml :three }
19
+ get('/four') { haml :four }
20
+ get('/five') { haml :five }
21
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+
7
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
8
+
9
+ require "rack/test"
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+ config.include Rack::Test::Methods
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1 @@
1
+ %em five
@@ -0,0 +1 @@
1
+ em five
@@ -0,0 +1 @@
1
+ %em four
@@ -0,0 +1 @@
1
+ em four
@@ -0,0 +1 @@
1
+ %em Spartaa!!
@@ -0,0 +1 @@
1
+ em Spartaa!!
@@ -0,0 +1 @@
1
+ %h1 This is #{yield}
@@ -0,0 +1 @@
1
+ h1 This is #{yield}
@@ -0,0 +1 @@
1
+ %em one
@@ -0,0 +1 @@
1
+ em one
@@ -0,0 +1 @@
1
+ %em three
@@ -0,0 +1 @@
1
+ em three
@@ -0,0 +1 @@
1
+ %em two
@@ -0,0 +1 @@
1
+ em two
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-fuzzy_layout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Kashyap
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sinatra
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: "Provides ability to enable or disable layouts \n for
56
+ templates via Regex"
57
+ email:
58
+ - kashyap.kmbc@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/sinatra/fuzzy_layout.rb
64
+ - Rakefile
65
+ - README.md
66
+ - spec/fuzzy_layout_spec.rb
67
+ - spec/sample_app.rb
68
+ - spec/sample_app_multiple.rb
69
+ - spec/spec_helper.rb
70
+ - spec/views/five.haml
71
+ - spec/views/five.slim
72
+ - spec/views/four.haml
73
+ - spec/views/four.slim
74
+ - spec/views/index.haml
75
+ - spec/views/index.slim
76
+ - spec/views/layout.haml
77
+ - spec/views/layout.slim
78
+ - spec/views/one.haml
79
+ - spec/views/one.slim
80
+ - spec/views/three.haml
81
+ - spec/views/three.slim
82
+ - spec/views/two.haml
83
+ - spec/views/two.slim
84
+ homepage: ''
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.0.3
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: In Sinatra, the user can disable the layout template either globally or
108
+ on a per template basis in the individual routes. This module will provide support
109
+ to enable or disable the layout rendering based on Regex description. Refer to
110
+ Readme for more details
111
+ test_files:
112
+ - spec/fuzzy_layout_spec.rb
113
+ - spec/sample_app.rb
114
+ - spec/sample_app_multiple.rb
115
+ - spec/spec_helper.rb
116
+ - spec/views/five.haml
117
+ - spec/views/five.slim
118
+ - spec/views/four.haml
119
+ - spec/views/four.slim
120
+ - spec/views/index.haml
121
+ - spec/views/index.slim
122
+ - spec/views/layout.haml
123
+ - spec/views/layout.slim
124
+ - spec/views/one.haml
125
+ - spec/views/one.slim
126
+ - spec/views/three.haml
127
+ - spec/views/three.slim
128
+ - spec/views/two.haml
129
+ - spec/views/two.slim
130
+ has_rdoc: