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, **opts, &block| # Skip if Rails environment should not use this perticular setting if defined?(Rails) return if Array.wrap(opts[:skip_env]).include?(Rails.env.to_sym) end pr = provider.new(builder: self, &block) push pr pr end else define_method(method_name) do |*args, **opts, &block| # Skip if Rails environment should not use this perticular setting if defined?(Rails) return if Array.wrap(opts[:skip_env]).include?(Rails.env.to_sym) end 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 if defined?(Rails) == "constant" enable_provider RailsConfig end 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