lib/pears/provider/builder.rb in pears-0.0.3 vs lib/pears/provider/builder.rb in pears-0.0.4
- old
+ new
@@ -1,45 +1,54 @@
require 'active_support/core_ext/module'
module Pears
- module Providers
+ 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 hash
- raise 'not yet implemented'
+ def freeze_layers
+ @skip_push = true
+ yield
+ @skip_push = false
end
- def loco
- raise 'not yet implemented'
+ def subject_name
+ @subject.name
end
- def env
- raise 'not yet implemented'
- end
-
- def local_file(file_path)
- register Provider::LocalFile.new(file_path)
- end
-
- def remote_file(file_url)
- register Provider::RemoteFile.new(file_url)
- end
-
- def subscription &block
- register Provider::Subscription.new(@subject.name, &block)
- end
-
private
- def register provider
+ def push provider
return provider unless @subject.is_a? Subject
- @subject.push_layer provider
+ @subject.push_layer provider unless @skip_push
end
end
end
end