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         => "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

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.



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

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.



64
65
66
# File 'lib/machined/environment.rb', line 64

def config
  @config
end

#context_helpersObject (readonly)

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



68
69
70
# File 'lib/machined/environment.rb', line 68

def context_helpers
  @context_helpers
end

#output_pathObject (readonly)

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



81
82
83
# File 'lib/machined/environment.rb', line 81

def output_path
  @output_path
end

#rootObject (readonly)

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



77
78
79
# File 'lib/machined/environment.rb', line 77

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.



90
91
92
# File 'lib/machined/environment.rb', line 90

def server
  @server
end

#sprocketsObject (readonly)

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



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

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.



73
74
75
# File 'lib/machined/environment.rb', line 73

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


263
264
265
266
267
268
# File 'lib/machined/environment.rb', line 263

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`.



239
240
241
242
# File 'lib/machined/environment.rb', line 239

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.



246
247
248
249
# File 'lib/machined/environment.rb', line 246

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.



307
308
309
310
# File 'lib/machined/environment.rb', line 307

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


299
300
301
302
# File 'lib/machined/environment.rb', line 299

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.



272
273
274
275
276
277
# File 'lib/machined/environment.rb', line 272

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