Sha256: 3c6d3513a1e5bbf9946fd57cef41a60612aa61f1afc437534f5858dc60ae999a

Contents?: true

Size: 1.29 KB

Versions: 34

Compression:

Stored size: 1.29 KB

Contents

module Berkshelf
  module Mixin
    module DSLEval
      class CleanRoom
        attr_reader :instance

        def initialize(instance)
          @instance = instance
        end
      end

      class << self
        def included(base)
          base.send(:extend, ClassMethods)
        end
      end

      module ClassMethods
        def clean_room
          @clean_room ||= begin
            exposed_methods = self.exposed_methods

            Class.new(DSLEval::CleanRoom) do
              exposed_methods.each do |exposed_method|
                define_method(exposed_method) do |*args, &block|
                  instance.send(exposed_method, *args, &block)
                end
              end
            end
          end
        end

        def expose_method(method)
          exposed_methods << method.to_sym
        end

        def exposed_methods
          @exposed_methods ||= Array.new
        end
      end

      # @return [Object]
      def dsl_eval(&block)
        self.class.clean_room.new(self).instance_eval(&block)
        self
      end

      # @param [String] filepath
      #
      # @return [Object]
      def dsl_eval_file(filepath)
        filepath = filepath.to_s
        contents = File.read(filepath)
        dsl_eval { eval(contents, binding, filepath, 1) }
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
berkshelf-3.1.5 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.1.4 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-2.0.18 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-2.0.17 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.1.3 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.1.2 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-2.0.16 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.1.1 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.1.0 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.0.1 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.0.0 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-2.0.15 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.0.0.rc1 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.0.0.beta9 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.0.0.beta8 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.0.0.beta7 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.0.0.beta6 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-2.0.14 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-3.0.0.beta5 lib/berkshelf/mixin/dsl_eval.rb
berkshelf-2.0.13 lib/berkshelf/mixin/dsl_eval.rb