PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/callbacks.rb

http://github.com/scalate/scalate
Ruby | 52 lines | 19 code | 2 blank | 31 comment | 0 complexity | 239ef0ef3e0ab6237f7a5f8febaf0f61 MD5 | raw file
 1module Sass
 2  # A lightweight infrastructure for defining and running callbacks.
 3  # Callbacks are defined using \{#define\_callback\} at the class level,
 4  # and called using `run_#{name}` at the instance level.
 5  #
 6  # Clients can add callbacks by calling the generated `on_#{name}` method,
 7  # and passing in a block that's run when the callback is activated.
 8  #
 9  # @example Define a callback
10  # class Munger
11  #   extend Sass::Callbacks
12  #   define_callback :string_munged
13  #
14  #   def munge(str)
15  #     res = str.gsub(/[a-z]/, '\1\1')
16  #     run_string_munged str, res
17  #     res
18  #   end
19  # end
20  #
21  # @example Use a callback
22  # m = Munger.new
23  # m.on_string_munged {|str, res| puts "#{str} was munged into #{res}!"}
24  # m.munge "bar" #=> bar was munged into bbaarr!
25  module Callbacks
26    protected
27
28    # Define a callback with the given name.
29    # This will define an `on_#{name}` method
30    # that registers a block,
31    # and a `run_#{name}` method that runs that block
32    # (optionall with some arguments).
33    #
34    # @param name [Symbol] The name of the callback
35    # @return [void]
36    def define_callback(name)
37      class_eval <<RUBY
38def on_#{name}(&block)
39  @_sass_callbacks ||= {}
40  (@_sass_callbacks[#{name.inspect}] ||= []) << block
41end
42
43def run_#{name}(*args)
44  return unless @_sass_callbacks
45  return unless @_sass_callbacks[#{name.inspect}]
46  @_sass_callbacks[#{name.inspect}].each {|c| c[*args]}
47end
48private :run_#{name}
49RUBY
50    end
51  end
52end