Module: Spar

Defined in:
lib/spar.rb,
lib/spar/cli.rb,
lib/spar/static.rb,
lib/spar/helpers.rb,
lib/spar/rewrite.rb,
lib/spar/version.rb,
lib/spar/compiler.rb,
lib/spar/not_found.rb,
lib/spar/compressor.rb,
lib/spar/exceptions.rb,
lib/spar/compiled_asset.rb,
lib/spar/deployers/deployer.rb,
lib/spar/directive_processor.rb

Defined Under Namespace

Modules: Compiler, Compressor, Helpers Classes: CLI, CompiledAsset, Deployer, DirectiveProcessor, Exceptions, NotFound, Rewrite, Static

Constant Summary collapse

DEFAULTS =
{
  'digest'   => false,
  'debug'    => true,
  'compress' => false,
  'js_compressor' => {
    'mangle' => false
  },
  'css_compressor' => {},
  'cache_control'  => "public, max-age=#{60 * 60 * 24 * 7}"
}
VERSION =
"1.0.9"

Class Method Summary collapse

Class Method Details

.appObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/spar.rb', line 117

def self.app
  app = Rack::Builder.new do
    use Spar::Rewrite
    use Spar::Exceptions

    run Rack::Cascade.new([Spar.static, Spar.sprockets, Spar.not_found])

    use Rack::ContentType
  end
end

.assetsObject



63
64
65
# File 'lib/spar.rb', line 63

def self.assets
  @assets ||= Spar::Assets.new
end

.environmentObject



59
60
61
# File 'lib/spar.rb', line 59

def self.environment
  @environment ||= ENV['SPAR_ENV'] || ENV['RACK_ENV'] || 'development'
end

.environment=(environment) ⇒ Object



55
56
57
# File 'lib/spar.rb', line 55

def self.environment=(environment)
  @environment = environment
end

.not_foundObject



71
72
73
# File 'lib/spar.rb', line 71

def self.not_found
  @not_found ||= Spar::NotFound.new
end

.rootObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spar.rb', line 37

def self.root
  @root ||= begin
    # Remove the line number from backtraces making sure we don't leave anything behind
    call_stack = caller.map { |p| p.sub(/:\d+.*/, '') }
    root_path = File.dirname(call_stack.detect { |p| p !~ %r[[\w.-]*/lib/spar|rack[\w.-]*/lib/rack] })

    while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/config.yml")
      parent = File.dirname(root_path)
      root_path = parent != root_path && parent
    end

    root = File.exist?("#{root_path}/config.yml") ? root_path : Dir.pwd
    raise "Could not find root path for #{self}" unless root

    RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? Pathname.new(root).expand_path : Pathname.new(root).realpath   
  end
end

.settingsObject



128
129
130
# File 'lib/spar.rb', line 128

def self.settings
  @settings ||= load_config
end

.sprocketsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/spar.rb', line 75

def self.sprockets
  @sprockets ||= begin
    env = Sprockets::Environment.new(root)

    HamlCoffeeAssets.config.namespace  = "window.HAML"
    HamlCoffeeAssets.config.escapeHtml = false

    Compass.configuration.project_path = "app"
    Compass.configuration.images_path  = "app/images"

    if settings['compress']
      env.js_compressor  = Compressor::JS.new
      env.css_compressor = Compressor::CSS.new
    end

    child_folders = ['javascripts', 'stylesheets', 'images', 'pages', 'fonts']

    for child_folder in child_folders
      env.append_path(root.join('app', child_folder))
      env.append_path(root.join('vendor', child_folder))
      Gem.loaded_specs.each do |name, gem|
        env.append_path(File.join(gem.full_gem_path, 'vendor', 'assets', child_folder))
        env.append_path(File.join(gem.full_gem_path, 'app', 'assets', child_folder))
      end
    end

    env.append_path(root.join('components'))

    for path in (settings['paths'] || [])
      env.append_path(root.join(*path.split('/')))
    end

    env.register_engine '.haml',    Tilt::HamlTemplate

    env.register_postprocessor 'text/css',               Spar::DirectiveProcessor
    env.register_postprocessor 'application/javascript', Spar::DirectiveProcessor
    env.register_postprocessor 'text/html',              Spar::DirectiveProcessor

    env
  end
end

.staticObject



67
68
69
# File 'lib/spar.rb', line 67

def self.static
  @static ||= Spar::Static.new
end