Class: Massimo::Config

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/massimo/config.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :config_path     => 'config.rb',
  :source_path     => '.',
  :output_path     => 'public',
  :environment     => 'development',
  :resources_path  => '.',
  :base_url        => '/',
  :resources_url   => '/',
  :javascripts_url => '/javascripts',
  :stylesheets_url => '/stylesheets'
}.freeze
JS_COMPRESSORS =
{
  :jsmin    => Crush::JSMin,
  :packr    => Crush::Packr,
  :yui      => Crush::YUI::JavaScriptCompressor,
  :closure  => Crush::Closure::Compiler,
  :uglifier => Crush::Uglifier
}
CSS_COMPRESSORS =
{
  :cssmin    => Crush::CSSMin,
  :rainpress => Crush::Rainpress,
  :yui       => Crush::YUI::CssCompressor,
  :sass      => Crush::Sass::Engine
}

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Config

Creates a new configuration. Takes either a hash of options or a file path to a .yaml file.



39
40
41
42
43
# File 'lib/massimo/config.rb', line 39

def initialize(options = nil)
  hash = DEFAULT_OPTIONS.dup
  hash.merge!(options.symbolize_keys) if options.is_a? Hash
  super hash
end

Instance Method Details

#compress=(compress) ⇒ Object

Sets up Massimo to compress both JavaScript and CSS files.

Parameters:

  • compress (Boolean)

    Wether or not to compress.



69
70
71
# File 'lib/massimo/config.rb', line 69

def compress=(compress)
  Crush.register if compress
end

#compress_css=(compress) ⇒ Object

Sets up Massimo to compress CSS files. By default, whichever CSS compression library is available, is used. To set the one you want to use see #css_compressor=.

Parameters:

  • compress (Boolean)

    Wether or not to compress.



106
107
108
# File 'lib/massimo/config.rb', line 106

def compress_css=(compress)
  Crush.register_css if compress
end

#compress_js=(compress) ⇒ Object

Sets up Massimo to compress JavaScript files. By default, whichever JavaScript compression library is available, is used. To set the one you want to use see #js_compressor=.

Parameters:

  • compress (Boolean)

    Wether or not to compress.



78
79
80
# File 'lib/massimo/config.rb', line 78

def compress_js=(compress)
  Crush.register_js if compress
end

#config_pathObject

The full, expanded path to the config file



46
47
48
# File 'lib/massimo/config.rb', line 46

def config_path
  File.expand_path super
end

#css_compressor=(compressor) ⇒ Object

Sets the CSS compressor to use. The compressor can be either a symbol mapping to the recognized Crush::Engines (see CSS_COMPRESSORS) or any Tilt::Template.

Parameters:

  • compressor (Tilt::Template, Symbol)

    The compressor to use.



115
116
117
118
119
120
# File 'lib/massimo/config.rb', line 115

def css_compressor=(compressor)
  if compressor.respond_to?(:to_sym)
    compressor = CSS_COMPRESSORS[compressor.to_sym]
  end
  Tilt.prefer compressor, 'css'
end

#css_compressor_options=(options) ⇒ Object

Sets the options used by the CSS compressor.

Parameters:

  • options (Hash)

    The hash of options to use.



125
126
127
# File 'lib/massimo/config.rb', line 125

def css_compressor_options=(options)
  self.css_options = options
end

#environmentObject

Return the enviornment option wrapped by a StringInquirer, so you can query the environment like this: ‘config.environment.production?`



62
63
64
# File 'lib/massimo/config.rb', line 62

def environment
  ActiveSupport::StringInquirer.new(super)
end

#files_in(resource_name, extension = '*') ⇒ Object

Get an array of all the file paths found in the given resource name’s path, restricted to the given extension.



146
147
148
# File 'lib/massimo/config.rb', line 146

def files_in(resource_name, extension = '*')
  Dir.glob File.join(path_for(resource_name), "**/*.#{extension}")
end

#js_compressor=(compressor) ⇒ Object

Sets the JavaScript compressor to use. The compressor can be either a symbol mapping to the recognized Crush::Engines (see JS_COMPRESSORS) or any Tilt::Template.

Parameters:

  • compressor (Tilt::Template, Symbol)

    The compressor to use.



87
88
89
90
91
92
# File 'lib/massimo/config.rb', line 87

def js_compressor=(compressor)
  if compressor.respond_to?(:to_sym)
    compressor = JS_COMPRESSORS[compressor.to_sym]
  end
  Tilt.prefer compressor, 'js'
end

#js_compressor_options=(options) ⇒ Object

Sets the options used by the JavaScript compressor.

Parameters:

  • options (Hash)

    The hash of options to use.



97
98
99
# File 'lib/massimo/config.rb', line 97

def js_compressor_options=(options)
  self.js_options = options
end

#options_for(lib_name) ⇒ Object

Convience method for getting options for a given library name. For instance, this is how we get the options set for Haml or Sass during processing.



152
153
154
155
# File 'lib/massimo/config.rb', line 152

def options_for(lib_name)
  return options_for("sass") if lib_name == "scss"
  send("#{lib_name}_options") || send(lib_name) || {}
end

#output_pathObject

The full, expanded path to the output directory.



56
57
58
# File 'lib/massimo/config.rb', line 56

def output_path
  File.expand_path super
end

#path_for(resource_name) ⇒ Object

Get a full, expanded path for the given resource name. This is either set in the configuration or determined dynamically based on the name.



131
132
133
134
135
136
137
# File 'lib/massimo/config.rb', line 131

def path_for(resource_name)
  if resource_path = send("#{resource_name}_path")
    File.expand_path resource_path
  else
    File.join source_path, resource_name.to_s
  end
end

#source_pathObject

The full, expanded path to the source directory.



51
52
53
# File 'lib/massimo/config.rb', line 51

def source_path
  File.expand_path super
end

#url_for(resource_name) ⇒ Object

Get the configured URL for th given resource name.



140
141
142
# File 'lib/massimo/config.rb', line 140

def url_for(resource_name)
  File.join base_url, send("#{resource_name}_url") || resources_url
end