Sha256: 22fe5bd904f8527d3a8882dea7b27c4078e4148aa78ac4ae88e2d23dacecc64c

Contents?: true

Size: 957 Bytes

Versions: 35

Compression:

Stored size: 957 Bytes

Contents

require 'active_support/core_ext/class/attribute'

module React
  module Callbacks
    def self.included(base)
      base.extend(ClassMethods)
    end

    def run_callback(name, *args)
      attribute_name = "_#{name}_callbacks"
      callbacks = self.class.send(attribute_name)
      callbacks.each do |callback|
        if callback.is_a?(Proc)
          instance_exec(*args, &callback)
        else
          send(callback, *args)
        end
      end
    end

    module ClassMethods
      def define_callback(callback_name)
        attribute_name = "_#{callback_name}_callbacks"
        class_attribute(attribute_name)
        self.send("#{attribute_name}=", [])
        define_singleton_method(callback_name) do |*args, &block|
          callbacks = self.send(attribute_name)
          callbacks.concat(args)
          callbacks.push(block) if block_given?
          self.send("#{attribute_name}=", callbacks)
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
react-opal-0.14.1 lib/react/opal/callbacks.rb
reactive-ruby-0.7.31 lib/react/callbacks.rb
reactive-ruby-0.7.30 lib/react/callbacks.rb
reactive-ruby-0.7.29 lib/react/callbacks.rb
reactive-ruby-0.7.28 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.27 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.26 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.25 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.24 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.23 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.22 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.21 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.20 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.19 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.18 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.17 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.16 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.15 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.14 lib/reactive-ruby/callbacks.rb
reactive-ruby-0.7.13 lib/reactive-ruby/callbacks.rb