Sha256: 0a0494bc39bb463d2dce2ed9762b9e2c5b2abc2942ddcfbdb542c26eddf5f61f

Contents?: true

Size: 875 Bytes

Versions: 16

Compression:

Stored size: 875 Bytes

Contents

module Generator
  module ClassMethods
    attr_reader :configurators

    def factory
      klass = Class.new(self)
      yield klass if block_given?
      klass
    end

    def call(*args, &block)
      new(*args, &block).tap { |object| object.call }
    end

    def generate(&block)
      factory.call(&block)
    end

    def mixin(mod)
      include mod
    end

    def configure(&block)
      configurators << block
    end

    def configurators
      @configurators ||= []
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end

  def initialize(*args)
    setup(*args)
    run_configurators
    configure(*args)
    yield self if block_given?
  end

  def setup(*args)
  end

  def configure(*args)
  end

  private

  def run_configurators
    self.class.configurators.each do |configurator|
      configurator.call(self)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rr-3.1.1 spec/support/generator.rb
rr-3.1.0 spec/support/generator.rb
rr-3.0.9 spec/support/generator.rb
rr-3.0.8 spec/support/generator.rb
rr-3.0.7 spec/support/generator.rb
rr-3.0.6 spec/support/generator.rb
rr-3.0.5 spec/support/generator.rb
rr-3.0.4 spec/support/generator.rb
rr-3.0.3 spec/support/generator.rb
rr-3.0.2 spec/support/generator.rb
rr-3.0.1 spec/support/generator.rb
rr-3.0.0 spec/support/generator.rb
rr-1.2.1 spec/support/generator.rb
rr-1.2.0 spec/support/generator.rb
rr-1.1.2 spec/support/generator.rb
rr-1.1.2.rc1 spec/support/generator.rb