Sha256: 9fd1c5e2e10e999d8711c6b8445747d8b4bae66f33982fdf5c394cf53e910eb9

Contents?: true

Size: 1.18 KB

Versions: 13

Compression:

Stored size: 1.18 KB

Contents

require 'active_support/core_ext/module'

module Pears
  module Provider
    class Builder
      # Evil meta programming.
      def self.enable_provider(provider, yielder: false)
        method_name = provider.name.split('::').last.underscore.to_sym
        if yielder
          define_method(method_name) do |*args, &block|
            pr = provider.new(builder: self, &block)
            push pr
            pr
          end
        else
          define_method(method_name) do |*args, **opts, &block|
            pr = provider.new(*args, **opts, &block)
            push pr
            pr
          end
        end
      end

      # define Providers
      enable_provider LocalFile
      enable_provider RemoteFile
      enable_provider Subscription, yielder: true

      delegate :name, to: :subject

      def initialize(subject)
        @subject = subject
      end

      def freeze_layers
        @skip_push = true
        yield
        @skip_push = false
      end

      def subject_name
        @subject.name
      end

      private

      def push provider
        return provider unless @subject.is_a? Subject

        @subject.push_layer provider unless @skip_push
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
pears-0.1.4 lib/pears/provider/builder.rb
pears-0.1.3 lib/pears/provider/builder.rb
pears-0.1.2 lib/pears/provider/builder.rb
pears-0.1.1 lib/pears/provider/builder.rb
pears-0.1.0 lib/pears/provider/builder.rb
pears-0.0.11 lib/pears/provider/builder.rb
pears-0.0.10 lib/pears/provider/builder.rb
pears-0.0.9 lib/pears/provider/builder.rb
pears-0.0.8 lib/pears/provider/builder.rb
pears-0.0.7 lib/pears/provider/builder.rb
pears-0.0.6 lib/pears/provider/builder.rb
pears-0.0.5 lib/pears/provider/builder.rb
pears-0.0.4 lib/pears/provider/builder.rb