sinatra-pepper 0.0.1.preview

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *~
19
+ \#*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sinatra-pepper.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Uchio KONDO
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Sinatra::Pepper
2
+
3
+ A gem for loading sinatra extensions by condition, like chanko or chili gem for sinatra
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'sinatra' # require: 'sinatra/base'
11
+ gem 'sinatra-pepper'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install sinatra-pepper
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'sinatra/base'
26
+ require 'sinatra-pepper'
27
+
28
+ module Pepper
29
+ def self.registered(base)
30
+ base.set :foo_key, "Hello, admin!"
31
+ end
32
+ end
33
+
34
+ class Sample < Sinatra::Base
35
+ register Sinatra::Pepper
36
+ register_pepper Pepper do |env|
37
+ env['rack.session'] && env['rack.session']['is_admin']
38
+ end
39
+
40
+ set :foo_key, "Hello, friend!"
41
+ get '/' do
42
+ "#{settings.foo_key}"
43
+ end
44
+ end
45
+ Sample.run!
46
+ ```
47
+
48
+ Then, if you're normal user, see on `/`:
49
+
50
+ "Hello, friend!"
51
+
52
+ No effect from a pepper extension. But if you're admin, you'll see:
53
+
54
+ "Hello, admin!"
55
+
56
+ TBD More sample & test...
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = 'spec/**/*_spec.rb'
7
+ spec.rspec_opts = ['-cfs']
8
+ end
9
+ rescue LoadError => e
10
+ $stderr.puts "rspec/core is missing"
11
+ end
12
+
13
+ task :default => [:spec]
@@ -0,0 +1 @@
1
+ rvm 1.9.3@sinatra-pepper
@@ -0,0 +1 @@
1
+ require "sinatra/pepper"
@@ -0,0 +1,36 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Pepper
5
+ def self.registered(base)
6
+ base.class_eval {
7
+ alias_method :__call__!, :call!
8
+
9
+ def call!(env)
10
+ spiced_klass = nil
11
+ self.class.peppers.each_pair do |extension, condition|
12
+ if condition.call(env)
13
+ spiced_klass ||= Class.new(self.class)
14
+ spiced_klass.send :register, extension
15
+ end
16
+ end
17
+ spiced_klass ? spiced_klass.call(env) : self.__call__!(env)
18
+ end
19
+ }
20
+ end
21
+
22
+ # TODO peppers would not be inherited, I wonder is it OK
23
+ def peppers
24
+ @peppers ||= {}
25
+ end
26
+
27
+ def register_pepper(extension, opts={}, &condition)
28
+ if bloc = opts[:if]
29
+ register_pepper extension, &bloc
30
+ end
31
+ peppers[extension] = condition
32
+ end
33
+ end
34
+
35
+ register Pepper
36
+ end
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module Pepper
3
+ VERSION = '0.0.1.preview'
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sinatra/pepper/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sinatra-pepper"
8
+ gem.version = Sinatra::Pepper::VERSION
9
+ gem.authors = ["Uchio KONDO"]
10
+ gem.email = ["udzura@udzura.jp"]
11
+ gem.description = %q{A gem for loading sinatra extensions by condition, like chanko or chili gem for sinatra}
12
+ gem.summary = %q{A gem for loading sinatra extensions by condition, like chanko or chili gem for sinatra}
13
+ gem.homepage = "https://github.com/udzura/sinatra-pepper"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ # gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'sinatra', '>= 1.3.0'
21
+ gem.add_development_dependency 'rspec', '>= 0'
22
+ gem.add_development_dependency 'rack-test', '>= 0'
23
+ gem.add_development_dependency 'pry', '>= 0'
24
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sinatra::Pepper do
4
+ it { should respond_to :registered }
5
+ describe 'ad-hoc registration' do
6
+ module Pepper1
7
+ def self.registered(base)
8
+ base.set :foo_key, "admin_value"
9
+ end
10
+ end
11
+
12
+ class Sample < Sinatra::Base
13
+ register Sinatra::Pepper
14
+ register_pepper Pepper1 do |env|
15
+ env['rack.session'] && env['rack.session']['is_admin']
16
+ end
17
+
18
+ disable :show_exceptions
19
+ set :foo_key, "normal_value"
20
+ get '/' do
21
+ "#{settings.foo_key}"
22
+ end
23
+ end
24
+
25
+ def app
26
+ Sample
27
+ end
28
+
29
+ describe 'normal access' do
30
+ before { get '/' }
31
+ subject { last_response.body }
32
+ it { should == "normal_value" }
33
+ end
34
+
35
+ describe 'admin access' do
36
+ before do
37
+ get '/', {}, {'rack.session' => {'is_admin' => true} }
38
+ end
39
+ subject { last_response.body }
40
+ it { should == "admin_value" }
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,24 @@
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
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ lib = File.expand_path('../lib', __FILE__)
9
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
10
+ require 'sinatra-pepper'
11
+ require 'rack/test'
12
+
13
+ RSpec.configure do |config|
14
+ config.treat_symbols_as_metadata_keys_with_true_values = true
15
+ config.run_all_when_everything_filtered = true
16
+ config.filter_run :focus
17
+
18
+ config.include Rack::Test::Methods
19
+ # Run specs in random order to surface order dependencies. If you find an
20
+ # order dependency and want to debug it, you can fix the order by providing
21
+ # the seed, which is printed after each run.
22
+ # --seed 1234
23
+ config.order = 'random'
24
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-pepper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.preview
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Uchio KONDO
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rack-test
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A gem for loading sinatra extensions by condition, like chanko or chili
79
+ gem for sinatra
80
+ email:
81
+ - udzura@udzura.jp
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - dot.rvmrc
93
+ - lib/sinatra-pepper.rb
94
+ - lib/sinatra/pepper.rb
95
+ - lib/sinatra/pepper/version.rb
96
+ - sinatra-pepper.gemspec
97
+ - spec/sinatra/pepper_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: https://github.com/udzura/sinatra-pepper
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>'
115
+ - !ruby/object:Gem::Version
116
+ version: 1.3.1
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.24
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: A gem for loading sinatra extensions by condition, like chanko or chili gem
123
+ for sinatra
124
+ test_files:
125
+ - spec/sinatra/pepper_spec.rb
126
+ - spec/spec_helper.rb