Sha256: 414a12ea8741a31e543f2987f4845ff12e827ad5a3c5351533cfd0f9b1411dd7

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module Remarkable
  module DSL
    module Callbacks

      def self.included(base) #:nodoc:
        base.extend ClassMethods
      end

      module ClassMethods
        protected
          # Class method that accepts a block or a symbol which is called after initialization.
          #
          def after_initialize(symbol=nil, &block)
            if block_given?
              @after_initialize_callbacks << block
            elsif symbol
              @after_initialize_callbacks << symbol
            end
          end

          # Class method that accepts a block or a symbol which is called before assertion.
          #
          def before_assert(symbol=nil, &block)
            if block_given?
              @before_assert_callbacks << block
            elsif symbol
              @before_assert_callbacks << symbol
            end
          end
      end

      def run_after_initialize_callbacks #:nodoc:
        self.class.after_initialize_callbacks.each do |method|
          if method.is_a?(Proc)
            instance_eval &method
          elsif method.is_a?(Symbol)
            send(method)
          end
        end
      end

      def run_before_assert_callbacks #:nodoc:
        self.class.before_assert_callbacks.each do |method|
          if method.is_a?(Proc)
            instance_eval &method
          elsif method.is_a?(Symbol)
            send(method)
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remarkable-3.0.1 lib/remarkable/dsl/callbacks.rb