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',
  :lib_path       => 'lib',
  :environment    => 'development',
  :cache          => nil,
  :skip_bundle    => false,
  :assets_only    => false,
  :digest_assets  => false,
  :gzip_assets    => false,
  :layout         => 'application',

  # Sprocket paths and URLs
  :assets_path    => 'assets',
  :assets_paths   => %w(app/assets lib/assets vendor/assets),
  :assets_url     => '/assets',
  :pages_path     => 'pages',
  :pages_paths    => %w(app/pages),
  :pages_url      => '/',
  :views_path     => 'views',
  :views_paths    => %w(app/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

Methods included from Initializable

#run_initializers

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.



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/machined/environment.rb', line 104

def initialize(options = {})
  @config            = OpenStruct.new DEFAULT_OPTIONS.dup.merge(options)
  @sprockets         = []
  @context_helpers   = []
  config.root        = Pathname.new(config.root).expand_path
  config.config_path = root.join config.config_path
  config.output_path = root.join config.output_path
  config.lib_path    = root.join config.lib_path
  config.environment = ActiveSupport::StringInquirer.new(config.environment)

  run_initializers self
end

Instance Attribute Details

#configObject (readonly)

The global configuration for the Machined environment.



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

def config
  @config
end

#context_helpersObject (readonly)

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



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

def context_helpers
  @context_helpers
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.



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

def server
  @server
end

#sprocketsObject (readonly)

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



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

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.



93
94
95
# File 'lib/machined/environment.rb', line 93

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' ]


336
337
338
339
340
# File 'lib/machined/environment.rb', line 336

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

#call(env) ⇒ Object

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



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

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.



303
304
305
306
# File 'lib/machined/environment.rb', line 303

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.



387
388
389
390
# File 'lib/machined/environment.rb', line 387

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


379
380
381
382
# File 'lib/machined/environment.rb', line 379

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.



344
345
346
347
348
# File 'lib/machined/environment.rb', line 344

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

#reloadObject

Reloads the environment. This will re-evaluate the config file & clear the current cache.



310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/machined/environment.rb', line 310

def reload
  # Make sure we have a fresh cache after we reload
  config.cache.clear if config.cache.respond_to?(:clear)

  # Reload dependencies as well, if necessary
  ActiveSupport::Dependencies.clear if defined?(ActiveSupport::Dependencies)

  # Use current configuration, but skip bundle and autoloading initializers
  current_config = config.marshal_dump.dup
  current_config.merge! :skip_bundle => true#, :skip_autoloading => true)

  initialize current_config
end

#remove_sprocket(name) ⇒ Object

Removes the sprocket with the given name. This is useful if you don’t need one of the default Sprockets.



352
353
354
355
356
357
# File 'lib/machined/environment.rb', line 352

def remove_sprocket(name)
  if sprocket = get_sprocket(name)
    sprockets.delete sprocket
    set_sprocket(name, nil)
  end
end