Sha256: 33a52b4c783e6cd7fa2b9b8b6d3f92c0208684c00913303a56e051c999f206b4

Contents?: true

Size: 1.85 KB

Versions: 119

Compression:

Stored size: 1.85 KB

Contents

module Sass
  # A lightweight infrastructure for defining and running callbacks.
  # Callbacks are defined using \{#define\_callback\} at the class level,
  # and called using `run_#{name}` at the instance level.
  #
  # Clients can add callbacks by calling the generated `on_#{name}` method,
  # and passing in a block that's run when the callback is activated.
  #
  # @example Define a callback
  #   class Munger
  #     extend Sass::Callbacks
  #     define_callback :string_munged
  #
  #     def munge(str)
  #       res = str.gsub(/[a-z]/, '\1\1')
  #       run_string_munged str, res
  #       res
  #     end
  #   end
  #
  # @example Use a callback
  #   m = Munger.new
  #   m.on_string_munged {|str, res| puts "#{str} was munged into #{res}!"}
  #   m.munge "bar" #=> bar was munged into bbaarr!
  module Callbacks
    # Automatically includes {InstanceMethods}
    # when something extends this module.
    #
    # @param base [Module]
    def self.extended(base)
      base.send(:include, InstanceMethods)
    end
    protected

    module InstanceMethods
      # Removes all callbacks registered against this object.
      def clear_callbacks!
        @_sass_callbacks = {}
      end
    end

    # Define a callback with the given name.
    # This will define an `on_#{name}` method
    # that registers a block,
    # and a `run_#{name}` method that runs that block
    # (optionall with some arguments).
    #
    # @param name [Symbol] The name of the callback
    # @return [void]
    def define_callback(name)
      class_eval <<RUBY, __FILE__, __LINE__ + 1
def on_#{name}(&block)
  @_sass_callbacks ||= {}
  (@_sass_callbacks[#{name.inspect}] ||= []) << block
end

def run_#{name}(*args)
  return unless @_sass_callbacks
  return unless @_sass_callbacks[#{name.inspect}]
  @_sass_callbacks[#{name.inspect}].each {|c| c[*args]}
end
private :run_#{name}
RUBY
    end
  end
end

Version data entries

119 entries across 114 versions & 11 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/sass-3.2.19/lib/sass/callbacks.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/sass-3.2.19/lib/sass/callbacks.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/sass-3.2.5/lib/sass/callbacks.rb
xass-0.1.0 lib/sass/callbacks.rb
oreorenasass-3.4.14 lib/sass/callbacks.rb
oreorenasass-3.4.13 lib/sass/callbacks.rb
oreorenasass-3.4.12 lib/sass/callbacks.rb
oreorenasass-3.4.11 lib/sass/callbacks.rb
oreorenasass-3.4.9 lib/sass/callbacks.rb
oreorenasass-3.4.7 lib/sass/callbacks.rb
oreorenasass-3.4.5 lib/sass/callbacks.rb
sadui-0.0.4 vendor/bundle/ruby/2.0.0/gems/sass-3.2.14/lib/sass/callbacks.rb
sadui-0.0.4 vendor/bundle/ruby/2.1.0/gems/sass-3.2.14/lib/sass/callbacks.rb
sass-3.2.19 lib/sass/callbacks.rb
sass-3.2.18 lib/sass/callbacks.rb
sass-3.2.17 lib/sass/callbacks.rb
sass-3.2.16 lib/sass/callbacks.rb
sass-3.2.15 lib/sass/callbacks.rb
sass-3.2.14 lib/sass/callbacks.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/sass-3.2.13/lib/sass/callbacks.rb