1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Sass] Add support for dumping certain Tree::Node options.

This commit is contained in:
Nathan Weizenbaum 2010-09-05 17:38:27 -07:00
parent e0ff354c68
commit b935a70694
5 changed files with 71 additions and 4 deletions

View file

@ -269,6 +269,35 @@ module Haml
version_gt(v1, v2) || !version_gt(v2, v1)
end
# A wrapper for `Marshal.dump` that calls `#_before_dump` on the object
# before dumping it, `#_after_dump` afterwards.
# It also calls `#_around_dump` and passes it a block in which the object is dumped.
#
# If any of these methods are undefined, they are not called.
#
# @param obj [Object] The object to dump.
# @return [String] The dumped data.
def dump(obj)
obj._before_dump if obj.respond_to?(:_before_dump)
return Marshal.dump(obj) unless obj.respond_to?(:_around_dump)
res = nil
obj._around_dump {res = Marshal.dump(obj)}
res
ensure
obj._after_dump if obj.respond_to?(:_after_dump)
end
# A wrapper for `Marshal.load` that calls `#_after_load` on the object
# after loading it, if it's defined.
#
# @param data [String] The data to load.
# @return [Object] The loaded object.
def load(data)
obj = Marshal.load(data)
obj._after_load if obj.respond_to?(:_after_load)
obj
end
# Silence all output to STDERR within a block.
#
# @yield A block in which no output will be printed to STDERR

View file

@ -49,8 +49,7 @@ module Sass
def store(key, sha, root)
orig_options = root.options
begin
root.options = {}
_store_(key, Sass::VERSION, sha, Marshal.dump(root))
_store_(key, Sass::VERSION, sha, Haml::Util.dump(root))
ensure
root.options = orig_options
end
@ -63,7 +62,7 @@ module Sass
# @return [Sass::Tree::RootNode] The root node.
def retrieve(key, sha)
contents = _retrieve_(key, Sass::VERSION, sha)
Marshal.load(contents) if contents
Haml::Util.load(contents) if contents
rescue EOFError, TypeError, ArgumentError => e
raise
Haml::Util.haml_warn "Warning. Error encountered while reading cache #{path_to(key)}: #{e}"

View file

@ -29,7 +29,8 @@ module Sass
sha = Digest::SHA1.hexdigest(sass_file.contents)
if root = options[:cache_store].retrieve(key, sha)
root.options = options.merge(:filename => sass_file.filename)
root.options = root.options.merge(
options.merge(:filename => sass_file.filename))
return root
end
end

View file

@ -244,6 +244,23 @@ module Sass
to_src(tabs, opts, :scss)
end
# Names of options that are saved when the node is serialized and cached.
#
# @type [Array<Symbol>]
SAVED_OPTIONS = []
# Ensures that only {SAVED_OPTIONS} get saved.
def _around_dump
old_options = @options
@options = {}
SAVED_OPTIONS.each do |opt|
@options[opt] = old_options[opt]
end
yield
ensure
options = old_options
end
protected
# Computes the CSS corresponding to this particular Sass node.

View file

@ -5,6 +5,19 @@ require 'pathname'
class UtilTest < Test::Unit::TestCase
include Haml::Util
class Dumpable
attr_reader :arr
def initialize; @arr = []; end
def _before_dump; @arr << :before; end
def _after_dump; @arr << :after; end
def _around_dump
@arr << :around_before
yield
@arr << :around_after
end
def _after_load; @arr << :loaded; end
end
def test_scope
assert(File.exist?(scope("Rakefile")))
end
@ -240,6 +253,14 @@ class UtilTest < Test::Unit::TestCase
assert(!version_gt(v2, v1), "Expected #{v2} = #{v1}")
end
def test_dump_and_load
obj = Dumpable.new
data = dump(obj)
assert_equal([:before, :around_before, :around_after, :after], obj.arr)
obj2 = load(data)
assert_equal([:before, :around_before, :loaded], obj2.arr)
end
def test_def_static_method
klass = Class.new
def_static_method(klass, :static_method, [:arg1, :arg2],