Sha256: 787b8675c63e4956dd1247632d563e36a1444a722cd6143da826645ae56a0bb1

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'yaml'
require 'pathname'
require 'erb'

module Minarai
  module Loaders
    class Base
      def initialize(path)
        @path = path
      end

      def load
        case
        when !existed_file?
          raise "Does not exist file: #{pathname}"
        when yml_file?
          load_yaml_file
        when erb_file?
          load_erb_file
        else
          raise 'inalid extname error'
        end
      end

      private

      def binding_for_erb
        TOPLEVEL_BINDING
      end

      def loaded_class
        raise NotImplementedError
      end

      def load_yaml_file
        loaded_class.new(load_file_from_yaml)
      end

      def load_erb_file
        self.class.new(parsed_erb_file.path).load
      end

      def parsed_erb
        ERB.new(pathname.read).result(binding_for_erb)
      end

      def parsed_erb_file
        @parsed_erb_file ||= Tempfile.open(['', '.yml']) do |tmp|
          tmp.puts parsed_erb
          tmp
        end
      end

      def existed_file?
        pathname.exist?
      end

      def yml_file?
        %w(.yml yaml).include?(pathname.extname)
      end

      def erb_file?
        pathname.extname == '.erb'
      end

      def load_file_from_yaml
        YAML.load_file(pathname)
      end

      def pathname
        @pathname ||= Pathname.new(@path)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
minarai-0.0.3 lib/minarai/loaders/base.rb
minarai-0.0.2 lib/minarai/loaders/base.rb
minarai-0.0.1 lib/minarai/loaders/base.rb