Class: BigBand

Inherits:
Sinatra::Base show all
Extended by:
Generated
Defined in:
lib/big_band.rb,
lib/big_band/sass.rb,
lib/big_band/compass.rb,
lib/big_band/reloader.rb,
lib/big_band/config_file.rb,
lib/big_band/integration.rb,
lib/big_band/more_server.rb,
lib/big_band/more_helpers.rb,
lib/big_band/web_inspector.rb,
lib/big_band/advanced_routes.rb,
lib/big_band/basic_extensions.rb,
lib/big_band/more_server/unicorn.rb,
lib/big_band/more_server/rainbows.rb

Overview

BigBand is a collection of Sinatra extensions and offers better sinatra integration for common tools. It is pluggable and each extension can be used in stand alone mode.

The main features are:

  • Routes as first class objects

  • Better handling of #set: Merges hashes, more hooks

  • Better compass integration

  • YAML config files mapping to set (overwriting present values/methods is prevented)

  • Rails-like helpers, like content_for

  • Unicorn and Rainbows integration

  • Smart code reloader only reloading changed files and getting rid of old routes

  • Sass extensions

  • Routes for inspection in development mode

  • Helpers and configuration for Bacon, RSpec, Test::Spec and Test::Unit

  • Tasks listing all routes for Monk and Rake.

  • YARD: Add Sinatra routes to generated documentation

Planned features:

  • More template helpers

  • ORM integration

  • MSpec integration

Usage

Using all BigBand features:

require "sinatra/big_band"
class Example < Sinatra::BigBand
  # Yay, BigBand!
end

Or you may use the extension style:

class AnotherExample < Sinatra::Base
  register Sinatra::BigBand
end

Or for the lazy folks (if you use classic style):

require "sinatra"
require "sinatra/big_band"
# Yay, BigBand!

Sinatra::BigBand is just an alias for BigBand. It was introduced manly to (in my opinion) have prettier class signatures. First “class Example < Sinatra::BigBand” looks like Sinatra “class Example < BigBand”, lets give some credit to our beloved framework. Also, Sinatra::BigBand seems more akin to Sinatra::Base, Sinatra::Application and Sinatra::Default, than just BigBand. Other than that I do not plan to move BigBand completely into Sinatra simply to avoid more nesting. Last, but no least, the Sinatra docs sugest placing extensions inside Sinatra.

Using just your favorite BigBand features:

require "big_band"
class Example < Sinatra::Base
  register BigBand::SomeFeature
  # Yay, SomeFeature!
end

Or, if you like a more handy syntax:

require "sinatra/big_band"
class Example < Sinatra::BigBand :SomeFeature, MyStuff::Extension, :development => :DevelopmentOnlyFeature
  # Yay, BigBand::SomeFeature!
  # Yay, MyStuff::Extension!
  # Yay, BigBand::DevelopmentOnlyFeature, if this is development mode!
end

Loading all but one feature:

require "sinatra/big_band"
class Example < Sinatra::BigBand :except => :SomeFeature
  # Yay, all but BigBand::SomeFeature!
end

Or just your favorite feature without you subclassing Sinatra::Base manually:

require "sinatra"
require "big_band/some_feature"
Sinatra::Application.register BigBand::SomeFeature
# Yay, BigBand::SomeFeature!

Defined Under Namespace

Modules: AdvancedRoutes, BasicExtensions, Compass, ConfigFile, Generated, Integration, MoreHelpers, MoreServer, Reloader, Sass, WebInspector

Constant Summary collapse

VERSION =
"0.2.5"
DATE =
"2010-01-24"
CALLERS_TO_IGNORE =
(class << Sinatra::Base; CALLERS_TO_IGNORE; end)

Instance Attribute Summary

Attributes included from Generated

#big_band_constructor, #big_band_extensions

Class Method Summary collapse

Methods included from Generated

inherited, inspect, name

Methods inherited from Sinatra::Base

register, register_without_big_band

Class Method Details

.applicationsObject



118
119
120
# File 'lib/big_band.rb', line 118

def self.applications
  @applications ||= []
end

.big_band_extensionsObject

Extensions to load.



123
124
125
# File 'lib/big_band.rb', line 123

def self.big_band_extensions
  default_extensions
end

.default_extension(name, path = nil, env = nil) ⇒ Object



217
218
219
220
# File 'lib/big_band.rb', line 217

def self.default_extension(name, path = nil, env = nil)
  autoload name, path if path
  default_extensions(env ? {env => name} : name)
end

.default_extensions(*extensions) ⇒ Object

Default extensions that will be used whenever you subclass BigBand. You can also use this to create your own extension collection:

class MyExtensions < BigBand(:except => :ExtensionIDontLike)
  default_extension FunkyExtension, :development => DevExtension
end

Note: If given a string or symbol, it will also try to setup an autoloader:

MyExtensions.default_extensions :Foo


203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/big_band.rb', line 203

def self.default_extensions(*extensions)
  return @default_extensions if @default_extensions and extensions.empty?
  @nonenv_extensions ||= []
  @env_extensions ||= {:development => []}
  extensions.each do |extension|
    if extension.respond_to? :each_pair
      extension.each_pair { |env, exts| (@env_extensions[env] ||= []).push(*exts) }
    else
      @nonenv_extensions.push(*extension)
    end
  end
  @default_extensions = [@nonenv_extensions, @env_extensions].flatten
end

.generate_class(*options) ⇒ Object

Generates a class for the given extensions. Note that this class is ment to be subclassed rather than used directly. Given extensione will only be available for subclasses.

class Foo < BigBand.generate_class(:except => :SomeExtension)
end


133
134
135
136
137
138
139
140
# File 'lib/big_band.rb', line 133

def self.generate_class(*options)
  @generated_classes ||= {setify_parameters([]) => BigBand}
  @generated_classes[setify_parameters(options)] ||=  Class.new(Sinatra::Base) do
    extend BigBand::Generated
    @big_band_extensions = options
    @big_band_constructor = "BigBand(#{options.map { |o| o.inspect}.join ", "})"
  end
end

.load_extensions(klass, *extensions) ⇒ Object

Adds extensions to a Sinatra application:

class MyApp < Sinatra::Base
end

BigBand.load_extensions MyApp, :SomeExtension, :development => :AnotherExtension


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/big_band.rb', line 148

def self.load_extensions(klass, *extensions)
  extensions = default_extensions if extensions.empty?
  extensions.flatten.each do |extension|
    if extension.respond_to? :each_pair
      extension.each_pair do |key, value|
        values = [value].flatten
        case key
        when :production, :test, :development
          klass.configure(key) { BigBand.load_extensions(klass, *values) }
        when :except
          exts = @nonenv_extensions.reject { |e| values.include? e }
          exts << @env_extensions.inject({}) do |accepted, (env, list)|
            accepted.merge env => list.reject { |e| values.include? e }
          end
          load_extensions(klass, *exts)
        else raise ArgumentError, "unknown key #{key.inspect}"
        end
      end
    else
      klass.register module_for(extension)
    end
  end
end

.module_for(extension) ⇒ Object

Returns the module for a given extension identifier:

BigBand.module_for :BasicExtension # => BigBand::BasicExtension
BigBand.module_for Array           # => Array
BigBand.module_for "Foo::Bar"      # => BigBand::Foo::Bar or Foo::Bar or raises an exception


177
178
179
180
181
182
183
# File 'lib/big_band.rb', line 177

def self.module_for(extension)
  case extension
  when Module then extension
  when String then extension.split("::").inject(self) { |klass, name| klass.const_get name }
  when Symbol then const_get(extension)
  end
end

.setify_parameters(args) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/big_band.rb', line 185

def self.setify_parameters(args)
  case args
  when Hash then args.inject({}) { |h,(k,v)| h k => setify_parameters(v) }
  when Array then args.flatten.to_set
  else args
  end
end