/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

  1. module 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. # Define a callback with the given name.
  28. # This will define an `on_#{name}` method
  29. # that registers a block,
  30. # and a `run_#{name}` method that runs that block
  31. # (optionall with some arguments).
  32. #
  33. # @param name [Symbol] The name of the callback
  34. # @return [void]
  35. def define_callback(name)
  36. class_eval <<RUBY
  37. def on_#{name}(&block)
  38. @_sass_callbacks ||= {}
  39. (@_sass_callbacks[#{name.inspect}] ||= []) << block
  40. end
  41. def run_#{name}(*args)
  42. return unless @_sass_callbacks
  43. return unless @_sass_callbacks[#{name.inspect}]
  44. @_sass_callbacks[#{name.inspect}].each {|c| c[*args]}
  45. end
  46. private :run_#{name}
  47. RUBY
  48. end
  49. end
  50. end