Class: Machined::Sprocket

Inherits:
Sprockets::Environment
  • Object
show all
Defined in:
lib/machined/sprocket.rb

Constant Summary collapse

DEFAULT_OPTIONS =

Default options for a Machined sprocket.

{
  :root    => '.',
  :assets  => false,
  :compile => true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machined, options = {}) ⇒ Sprocket

Creates a new Machined sprocket. The API is a bit different than ‘Sprockets::Environment` to allow for per-Sprockets-environment configuration and to keep a reference to the Machined environment.



25
26
27
28
29
30
31
32
# File 'lib/machined/sprocket.rb', line 25

def initialize(machined, options = {})
  @machined = machined
  @config   = OpenStruct.new DEFAULT_OPTIONS.dup.merge(options)

  super config.root

  @context_class = Class.new Context
end

Instance Attribute Details

#configObject (readonly)

A reference to the configuration.



19
20
21
# File 'lib/machined/sprocket.rb', line 19

def config
  @config
end

#machinedObject (readonly)

A reference to the Machined environment which created this instance.



16
17
18
# File 'lib/machined/sprocket.rb', line 16

def machined
  @machined
end

Instance Method Details

#compile?Boolean

Returns true, if this sprocket should be compiled. Nine times out of ten, you will want your sprocket compiled, but sometimes - like the default views sprocket - it is used as a uncompiled resource.

Returns:

  • (Boolean)


39
40
41
# File 'lib/machined/sprocket.rb', line 39

def compile?
  config.compile && config.url
end

#extension_for_mime_type(type) ⇒ Object

Override the default Sprockets method which incorrectly returns ‘.htm’.



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

def extension_for_mime_type(type) # :nodoc:
  return '.html' if type == 'text/html'
  super
end

#indexObject

Override to use Machined’s Index



44
45
46
# File 'lib/machined/sprocket.rb', line 44

def index
  Index.new(self)
end

#use_all_templatesObject

Loops through the available Tilt templates and registers them as processor engines for Sprockets. By default, Sprockets cherry picks templates that work for web assets. We need to allow use of Haml, Markdown, etc.



53
54
55
56
57
58
# File 'lib/machined/sprocket.rb', line 53

def use_all_templates
  Utils.available_templates.each do |ext, template|
    next if engines(ext)
    register_engine ext, template
  end
end