Sha256: cdf8edb700c6fef31fa7c8540e5123b533e278fd528418cf2021b46bb8f0cff0

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

module Hyperstack
  module Internal
    module Callbacks
      if RUBY_ENGINE != 'opal'
        class Hyperstack::Hotloader
          def self.record(*args); end
        end
      end
      def self.included(base)
        base.extend(ClassMethods)
      end

      def run_callback(name, *args)
        self.class.callbacks_for(name).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, &after_define_hook)
          wrapper_name = "_#{callback_name}_callbacks"
          define_singleton_method(wrapper_name) do
            Context.set_var(self, "@#{wrapper_name}", force: true) { [] }
          end
          define_singleton_method(callback_name) do |*args, &block|
            send(wrapper_name).concat(args)
            send(wrapper_name).push(block) if block_given?
            Hotloader.record(self, "@#{wrapper_name}", 4, *args, block)
            after_define_hook.call(*args, &block) if after_define_hook
          end
        end

        def callbacks_for(callback_name)
          wrapper_name = "_#{callback_name}_callbacks"
          if superclass.respond_to? :callbacks_for
            superclass.callbacks_for(callback_name)
          else
            []
          end + send(wrapper_name)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hyper-state-1.0.alpha1.2 lib/hyperstack/internal/callbacks.rb
hyper-state-1.0.alpha1.1 lib/hyperstack/internal/callbacks.rb
hyper-state-1.0.0.pre.alpha1 lib/hyperstack/internal/callbacks.rb
hyper-state-1.0.alpha1 lib/hyperstack/internal/callbacks.rb
hyper-state-1.0.pre.alpha1 lib/hyperstack/internal/callbacks.rb
hyper-state-0.1 lib/hyperstack/internal/callbacks.rb