Sha256: c2a4af947597bdfc19614c99942f8f3b3a92bfe0b4f84b4a3caa33b9cfc6b1ea

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

require 'ostruct'

module Teatime
  class Teafile

    def self.load(file_path)
      new(Context.new.tap do |context|
        Kernel.eval File.read(file_path), context.get_binding
      end).config
    end

    def initialize(context)
      @context = context
    end

    def config
      OpenStruct.new({
        :lib_dir => @context.lib_dir,
        :lib_files => @context.lib_files,
        :test_dir => @context.test_dir,
        :test_helpers => @context.test_helpers,
        :test_files => @context.test_files,
        :vendor_dir => File.expand_path(File.join('..', '..', '..', 'vendor', 'assets'), __FILE__)
      })
    end

    class Context
      attr_accessor :lib_dir, :lib_files, :test_dir, :test_helpers, :test_files

      def Teatime(&block)
        block.call(TeafileConfig.new self)
      end

      def get_binding
        return binding()
      end

      class TeafileConfig
        def self.delegates(*attrs)
          attrs.each do |attr|
            delegate attr
          end
        end

        def self.delegate(attr)
          define_method attr do
            @context.public_send attr
          end

          define_method "#{attr}=" do |arg|
            @context.public_send "#{attr}=", arg
          end
        end

        def initialize(context)
          @context = context
        end

        delegates :lib_dir, :lib_files, :test_dir, :test_helpers, :test_files
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
teatime-0.0.2 lib/teatime/teafile.rb
teatime-0.0.1 lib/teatime/teafile.rb