diff --git a/doc-src/content/CHANGELOG.markdown b/doc-src/content/CHANGELOG.markdown index 0e2db03a..0b68404b 100644 --- a/doc-src/content/CHANGELOG.markdown +++ b/doc-src/content/CHANGELOG.markdown @@ -14,6 +14,12 @@ The Documentation for the [latest stable release](http://compass-style.org/docs/ The Documentation for the [latest preview release](http://beta.compass-style.org/) +0.11.beta.8 (UNRELEASED) +------------------------ + +* Created an official API to add configuration options to compass. + [More information](/help/tutorials/extending/). + 0.11.beta.7 (04/16/2011) ------------------------ * Added support for the new webkit gradient syntax that matches the css3 specification. diff --git a/doc-src/content/help/tutorials/extending.markdown b/doc-src/content/help/tutorials/extending.markdown index 4bc17e9b..9d7f4d88 100644 --- a/doc-src/content/help/tutorials/extending.markdown +++ b/doc-src/content/help/tutorials/extending.markdown @@ -12,7 +12,7 @@ classnames: The sprite engine is the work horse of sprite generation it's the interface for assembling and writing the image file to disk. -## Requirments +### Requirements A sprite engine requires only one method and that is `construct_sprite` which must return an object that responds to `save(filepath)` @@ -25,11 +25,10 @@ Since the Engine module extends base you also have access to all methods in [Com To enable your sprite engine from the config file set sprite_engine = : - + The example below will load `Compass::SassExtension::Sprites::ChunkyPngEngine` sprite_engine = :chunky_png - ### Class Definition @@ -47,3 +46,28 @@ The example below will load `Compass::SassExtension::Sprites::ChunkyPngEngine` end end end + + +## Adding Configuration Properties to Compass + +To add a new configuration property to Compass: + + Compass::Configuration.add_configuration_property(:foobar, "this is a foobar") do + if environment == :production + "foo" + else + "bar" + end + end + +This will do several things: + +1. make it possible for users to set the `foobar` configuration property in their + configuration file. +2. Ruby code can read and write the `foobar` attribute from any configuration object. +3. It will add the comment `# this is a foobar` above the property in the configuration file. + A comment is not required, you can simply omit this argument if you like. +4. The block of code provided allows you to assign a sensible default value according to other + settings in the configuration or by using arbitrary code to determine what the value should + be. For instance it could read from another configuration file or it could change based on + the user's operating system. \ No newline at end of file diff --git a/doc-src/content/help/tutorials/extensions.markdown b/doc-src/content/help/tutorials/extensions.markdown index a364d870..f18e4569 100644 --- a/doc-src/content/help/tutorials/extensions.markdown +++ b/doc-src/content/help/tutorials/extensions.markdown @@ -106,6 +106,10 @@ template directories are not at the top level, you can just do this instead: base_directory = File.join(File.dirname(__FILE__), '..', 'compass') Compass::Frameworks.register('my_extension', :path => base_directory) +### Adding Configuration Options to Compass + +For details on how to add new configuration options to compass [read this](/help/tutorials/extending/#adding-configuration-properties). + Conventions to Follow --------------------- diff --git a/lib/compass/configuration.rb b/lib/compass/configuration.rb index c4e35336..2990e85d 100644 --- a/lib/compass/configuration.rb +++ b/lib/compass/configuration.rb @@ -41,6 +41,40 @@ module Compass :sprite_engine ].flatten + # Registers a new configuration property. + # Extensions can use this to add new configuration options to compass. + # + # @param [Symbol] name The name of the property. + # @param [String] comment A comment for the property. + # @param [Proc] default A method to calculate the default value for the property. + # The proc is executed in the context of the project's configuration data. + def self.add_configuration_property(name, comment = nil, &default) + ATTRIBUTES << name + if comment.is_a?(String) + unless comment[0..0] == "#" + comment = "# #{comment}" + end + unless comment[-1..-1] == "\n" + comment = comment + "\n" + end + Data.class_eval <<-COMMENT + def comment_for_#{name} + #{comment.inspect} + end + COMMENT + end + Data.send(:define_method, :"default_#{name}", &default) if default + Data.inherited_accessor(name) + if name.to_s =~ /dir|path/ + strip_trailing_separator(name) + end + end + + # For testing purposes + def self.remove_configuration_property(name) + ATTRIBUTES.delete(name) + end + end end diff --git a/lib/compass/configuration/data.rb b/lib/compass/configuration/data.rb index 2dcfdca0..9cde724d 100644 --- a/lib/compass/configuration/data.rb +++ b/lib/compass/configuration/data.rb @@ -125,6 +125,7 @@ module Compass Compass::Frameworks.register_directory framework_dir end + # Finds all extensions within a directory and registers them. def discover(frameworks_dir) (self.framework_path ||= []) << frameworks_dir Compass::Frameworks.discover frameworks_dir diff --git a/test/configuration_test.rb b/test/configuration_test.rb index 4d605089..6e75a032 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -195,4 +195,44 @@ EXPECTED assert_equal "fonts", Compass.configuration.fonts_dir assert_equal "extensions", Compass.configuration.extensions_dir end + + def test_custom_configuration_properties + # Add a configuration property to compass. + Compass::Configuration.add_configuration_property(:foobar, "this is a foobar") do + if environment == :production + "foo" + else + "bar" + end + end + + contents = StringIO.new(<<-CONFIG) + foobar = "baz" + CONFIG + + Compass.add_configuration(contents, "test_strip_trailing_directory_separators") + + assert_equal "baz", Compass.configuration.foobar + expected_serialization = <