Class: Machined::Environment

Inherits:
Object
  • Object
show all
Includes:
Initializable
Defined in:
lib/machined/environment.rb

Constant Summary collapse

DEFAULT_OPTIONS =

Default options for a Machined environment.

{
  # Global configuration
  :root           => ".",
  :config_path    => "machined.rb",
  :output_path    => "public",
  :environment    => "development",
  :skip_bundle    => false,
  :digest_assets  => false,
  :gzip_assets    => false,
  :layout         => "main",
  
  # Sprocket paths and URLs
  :assets_path    => "assets",
  :assets_paths   => %w(lib/assets vendor/assets),
  :assets_url     => "/assets",
  :pages_path     => "pages",
  :pages_url      => "/",
  :views_path     => "views",
  
  # Compression configuration
  :compress       => false,
  :compress_css   => false,
  :compress_js    => false,
  :css_compressor => nil,
  :js_compressor  => nil
}.freeze
JS_COMPRESSORS =

A hash of Javascript compressors. When ‘config.js_compressor` is set using a symbol, such as `:uglifier`, this is where we check which engine to use.

{
  :jsmin    => Crush::JSMin,
  :packr    => Crush::Packr,
  :yui      => Crush::YUI::JavaScriptCompressor,
  :closure  => Crush::Closure::Compiler,
  :uglifier => Crush::Uglifier
}
CSS_COMPRESSORS =

A hash of CSS compressors. When ‘config.css_compressor` is set using a symbol, such as `:sass`, this is where we check which engine to use.

{
  :cssmin    => Crush::CSSMin,
  :rainpress => Crush::Rainpress,
  :yui       => Crush::YUI::CssCompressor,
  :sass      => Crush::Sass::Engine
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Environment

Creates a new Machined environment. It sets up three default sprockets:

* An assets sprocket similar to what you would use

in a Rails 3.1 app.

* A pages sprocket which handles static HTML pages.
* A views sprocket, which is not compiled, which is where

layouts and partials go.



99
100
101
102
103
104
105
106
107
# File 'lib/machined/environment.rb', line 99

def initialize(options = {})
  @config          = OpenStruct.new DEFAULT_OPTIONS.dup.merge(options)
  @root            = Pathname.new(config.root).expand_path
  @output_path     = root.join config.output_path
  @sprockets       = []
  @context_helpers = []
  
  run_initializers self
end

Instance Attribute Details

#configObject (readonly)

The global configuration for the Machined environment.



62
63
64
# File 'lib/machined/environment.rb', line 62

def config
  @config
end

#context_helpersObject (readonly)

An array of the helpers added to the Context through the ‘#helpers` method.



66
67
68
# File 'lib/machined/environment.rb', line 66

def context_helpers
  @context_helpers
end

#output_pathObject (readonly)

A reference to the directory the Machined environment compiles its files to.



79
80
81
# File 'lib/machined/environment.rb', line 79

def output_path
  @output_path
end

#rootObject (readonly)

A reference to the root directory the Machined environment is run from.



75
76
77
# File 'lib/machined/environment.rb', line 75

def root
  @root
end

#serverObject (readonly)

When the Machined environment is used as a Rack server, this will reference the actual ‘Machined::Server` instance that handles the requests.



88
89
90
# File 'lib/machined/environment.rb', line 88

def server
  @server
end

#sprocketsObject (readonly)

An ‘Array` of the Sprockets environments (actually `Machined::Sprocket` instances) that are the core of a Machined environment.



83
84
85
# File 'lib/machined/environment.rb', line 83

def sprockets
  @sprockets
end

#static_compilerObject (readonly)

When the Machined environment is compiling static files, this will reference the ‘Machined::StaticCompiler` which handles looping through the available files and generating them.



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

def static_compiler
  @static_compiler
end

Instance Method Details

#append_sprocket(name, options = {}, &block) ⇒ Object

Creates a Machined sprocket with the given name and options and appends it to the #sprockets list. This will also create an accessor with the given name that references the created sprocket.

machined.append_sprocket :updates, :url => "/news" do |updates|
  updates.append_path "updates"
end

machined.updates            # => #<Machined::Sprocket...>
machined.updates.config.url # => "/news"
machined.updates.paths      # => [ ".../updates" ]


261
262
263
264
265
266
# File 'lib/machined/environment.rb', line 261

def append_sprocket(name, options = {}, &block)
  create_sprocket(name, options, &block).tap do |sprocket|
    sprockets.push(sprocket).uniq!
    server and server.remap
  end
end

#call(env) ⇒ Object

Handles Rack requests by passing the env to an instance of ‘Machined::Server`.



237
238
239
240
# File 'lib/machined/environment.rb', line 237

def call(env)
  @server ||= Server.new self
  server.call(env)
end

#compileObject

Loops through the available static files and generates them in the output path.



244
245
246
247
# File 'lib/machined/environment.rb', line 244

def compile
  @static_compiler ||= StaticCompiler.new self
  static_compiler.compile
end

#define_singleton_method(symbol, method = nil, &block) ⇒ Object

Add define_singleton_method for Ruby 1.8.7 This is used to define the sprocket accessor methods.



305
306
307
308
# File 'lib/machined/environment.rb', line 305

def define_singleton_method(symbol, method = nil, &block) # :nodoc:
  singleton_class = class << self; self; end
  singleton_class.__send__ :define_method, symbol, method || block
end

#helpers(*modules, &block) ⇒ Object

Adds helpers that can be used within asset files. There’s two supported ways to add helpers, the first of which is similar to how the ‘#helpers` DSL works in Sinatra:

helpers do
  def current_time
    Time.now
  end
end

The other way is to pass modules directly:

module CycleHelper
  def cycle(*args)
    # ...
  end
end

helpers CycleHelper


297
298
299
300
# File 'lib/machined/environment.rb', line 297

def helpers(*modules, &block)
  @context_helpers << block if block_given?
  @context_helpers.push *modules
end

#prepend_sprocket(name, options = {}, &block) ⇒ Object

Creates a Machined sprocket with the given name and options and prepends it to the #sprockets list.



270
271
272
273
274
275
# File 'lib/machined/environment.rb', line 270

def prepend_sprocket(name, options = {}, &block)
  create_sprocket(name, options, &block).tap do |sprocket|
    sprockets.unshift(sprocket).uniq!
    server and server.remap
  end
end