Skip to content

Instantly share code, notes, and snippets.

@fredrikhenne
Forked from jnunemaker/stylesheet.rb
Created May 17, 2010 22:23
Show Gist options
  • Save fredrikhenne/404314 to your computer and use it in GitHub Desktop.
Save fredrikhenne/404314 to your computer and use it in GitHub Desktop.
class Stylesheet
include MongoMapper::Document
class Processor
def self.errors
[]
end
attr_reader :contents
def initialize(contents)
@contents = contents
end
end
class PlainProcessor < Processor
def process
contents
end
end
class SassProcessor < Processor
def self.errors
[Sass::SyntaxError]
end
def self.options
@options ||= {}
end
def process
Sass::Engine.new(contents, self.class.options).render
end
end
class ScssProcessor < SassProcessor
def self.options
@options ||= {:syntax => :scss}
end
end
class LessProcessor < Processor
def self.errors
[
Less::MixedUnitsError,
Less::PathError,
Less::VariableNameError,
Less::MixinNameError,
Less::SyntaxError,
Less::ImportError,
Less::CompileError
]
end
def process
Less.parse(contents)
end
end
Processors = {
'plain' => PlainProcessor,
'sass' => SassProcessor,
'scss' => ScssProcessor,
'less' => LessProcessor,
}
def self.processors
Processors
end
key :processor, String, :default => 'plain'
validates_inclusion_of :processor, :within => Stylesheet.processors.keys
validate :no_processor_parse_error
def processed_contents
processor_class.new(contents).process
end
private
def processor_class
self.class.processors[processor]
end
def no_processor_parse_error
return if contents.blank?
begin
processed_contents
rescue *processor_class.errors => e
errors.add(:base, "#{processor.capitalize} parse error: #{e.message}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment