Sha256: 9fbd5b6db26ed342fb823c1e04a4fbd6518d4dacc1eb385fa3944f4466a08685

Contents?: true

Size: 1.65 KB

Versions: 18

Compression:

Stored size: 1.65 KB

Contents

# -*- encoding : utf-8 -*-
module Dao
  class Api
    Initializers = {} unless defined?(Initializers)

    class << Api
      def new(*args, &block)
        allocate.instance_eval do
          before_initializers(*args, &block)
          initialize(*args, &block)
          after_initializers(*args, &block)
          self
        end
      end

      def initializers
        Initializers[self] ||= {:before => [], :after => []}
      end

      def before_initializers
        initializers[:before]
      end

      def before_initializer(&block)
        method_name = "before_initializer_#{ before_initializers.size }"
        define_method(method_name, &block)
        before_initializers.push(method_name)
      end

      def after_initializers
        initializers[:after]
      end

      def after_initializer(&block)
        after_initializers.push(block)
      end

      def superclasses
        @superclasses ||= ancestors.select{|ancestor| ancestor <= Dao::Api}
      end
    end

    def superclasses
      @superclasses ||= self.class.superclasses
    end

    def run_initializers(which, *args, &block)
      superclasses.each do |superclass|
        superclass.send("#{ which }_initializers").each do |method_name|
          send(method_name, *args, &block)
        end
      end
      send("#{ which }_initialize", *args, &block)
    end

    def before_initializers(*args, &block)
      run_initializers(:before, *args, &block)
    end

    def after_initializers(*args, &block)
      run_initializers(:after, *args, &block)
    end

    def before_initialize(*args, &block)
      :hook
    end

    def after_initialize(*args, &block)
      :hook
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
dao-8.0.1 lib/dao/api/initializers.rb
dao-8.0.0 lib/dao/api/initializers.rb
dao-7.0.0 lib/dao/api/initializers.rb
dao-5.6.1 lib/dao/api/initializers.rb
dao-5.5.2 lib/dao/api/initializers.rb
dao-5.5.1 lib/dao/api/initializers.rb
dao-5.5.0 lib/dao/api/initializers.rb
dao-5.4.0 lib/dao/api/initializers.rb
dao-5.3.3 lib/dao/api/initializers.rb
dao-5.3.2 lib/dao/api/initializers.rb
dao-5.3.0 lib/dao/api/initializers.rb
dao-5.2.1 lib/dao/api/initializers.rb
dao-5.1.2 lib/dao/api/initializers.rb
dao-5.1.1 lib/dao/api/initializers.rb
dao-5.0.0 lib/dao/api/initializers.rb
dao-4.6.4 lib/dao/api/initializers.rb
dao-4.4.4 lib/dao/api/initializers.rb
dao-4.4.2 lib/dao/api/initializers.rb