Permalink
Browse files

do not change sinatra behavior

  • Loading branch information...
1 parent 7097590 commit 795121a99b9b672d42e68dadb8c7e477f8821561 @rkh committed May 2, 2011
Showing with 2 additions and 109 deletions.
  1. +1 −83 lib/sinatra/sugar.rb
  2. +1 −1 sinatra-sugar.gemspec
  3. +0 −25 spec/sinatra/sugar_spec.rb
View
@@ -25,32 +25,8 @@ def ignore_caller(pattern = nil)
end
module ClassMethods
-
attr_writer :root, :guessed_root
- # More advanced set:
- # - Adds set_#{key} and set_value hooks to set.
- # - Merges the old value with the new one, if both are hashes:
- # set :haml, :format => :html5, :escape_html => true
- # set :haml, :excape_html => false
- # haml # => { :format => :html5, :escape_html => false }
- # - Allowes passing a block (for Sinatra 0.9.x):
- # set(:foo) { Time.now }
- def set(key, value = self, &block)
- symbolized = (key.to_sym if key.respond_to? :to_sym)
- old_value = (send(symbolized) if symbolized and respond_to? symbolized)
- value = old_value.merge value if value.is_a? Hash and old_value.is_a? Hash
- super
- # HACK: Sinatra::Base.set uses recursion and in the final step value always
- # is a Proc. Also, if value is a Proc no step ever follows. I abuse this to
- # invoke the hooks only once per set.
- if value.is_a? Proc
- invoke_hook "set_#{key}", self
- invoke_hook :set_value, self, key
- end
- self
- end
-
# More advanced register:
# - If an exntesion is registered twice, the registered hook will only be called once.
def register(*extensions, &block)
@@ -76,64 +52,9 @@ def root_path(*args)
def root_glob(*args, &block)
Dir.glob(root_path(*args)).each(&block)
end
-
- # Whether or not to start a webserver.
- def run?
- @run ||= true
- @run and !@running and app_file? and $0.expand_path == app_file.expand_path
- end
-
- # Disable automatically running this class as soon it is subclassed.
- def inherited
- super
- @run = false
- end
-
- # The application's root directory. BigBand will guess if missing.
- def root
- return ".".expand_path unless app_file?
- return @root if defined?(@root) and @root
- @guessed_root ||= begin
- dir = app_file.expand_path.dirname
- if dir.basename == "lib" and not (dir / "lib").directory?
- dir.dirname
- else
- dir
- end
- end
- end
-
- # Returns true if the #root is known.
- def root?
- !!@root || app_file?
- end
-
- # Option parser for #run!
- def run_option_parser
- @run_option_parser ||= begin
- require 'optparse'
- OptionParser.new do |op|
- op.on('-x') { set :lock, true }
- op.on('-e env') { |val| set :environment, val.to_sym }
- op.on('-s server') { |val| set :server, val }
- op.on('-p port') { |val| set :port, val.to_i }
- end
- end
- end
-
- # Extended #run!, offers an extandable option parser for
- # BigBand with the same standard options as the one of
- # Sinatra::Base (see #run_option_parser).
- def run!(options = {})
- run_option_parser.parse!(ARGV.dup) unless ARGV.empty?
- @running = true
- super(options)
- end
-
end
module InstanceMethods
-
# See BigBand::BasicExtentions::ClassMethods#root_path
def root_path(*args)
self.class.root_path(*args)
@@ -153,7 +74,6 @@ def root
def root?
self.class.root
end
-
end
def self.registered(klass)
@@ -164,10 +84,8 @@ def self.registered(klass)
def self.set_app_file(klass)
klass.guessed_root = nil
end
-
end
-
+
Base.ignore_caller
register Sugar
-
end
View
@@ -2,7 +2,7 @@ SPEC = Gem::Specification.new do |s|
# Get the facts.
s.name = "sinatra-sugar"
- s.version = "0.5.0"
+ s.version = "0.5.1"
s.description = "Some extensions to the sinatra default behavior (usefull for other Sintatra extensions, part of BigBand)."
# BigBand depedencies
View
@@ -4,36 +4,11 @@
before { app :Sugar }
it_should_behave_like 'sinatra'
- describe "set" do
-
- it "adds hooks to Sinatra::Base#set" do
- extension = Module.new
- extension.should_receive(:set_foo).with(app)
- extension.should_receive(:set_value).with(app, :foo)
- app.register extension
- app.set :foo, 42
- end
-
- it "allows passing a block" do
- app.set(:foo) { 42 }
- app.foo.should == 42
- end
-
- it "merges hash values" do
- app.set :foo, :bar => 42
- app.set :foo, :baz => 23
- app.foo[:bar].should == 42
- app.foo[:baz].should == 23
- end
-
- end
-
describe "register" do
it "registers an extension only once" do
extension = Module.new
extension.should_receive(:registered).once.with(app)
10.times { app.register extension }
end
end
-
end

0 comments on commit 795121a

Please sign in to comment.