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