Sha256: 8fef0615e26b4087bcfa263eb321a15ddda79d12bab66f28b58fe5fe5f84702a

Contents?: true

Size: 1.92 KB

Versions: 22

Compression:

Stored size: 1.92 KB

Contents

module Ridley::Mixin
  module FromFile
    module ClassMethods
      def from_file(filename, *args)
        new(*args).from_file(filename)
      end

      def class_from_file(filename, *args)
        new(*args).class_from_file(filename)
      end
    end

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

    # Loads the contents of a file within the context of the current object
    #
    # @param [#to_s] filename
    #   path to the file to load
    #
    # @raise [IOError] if the file does not exist or cannot be read
    def from_file(filename)
      filename = filename.to_s

      ensure_presence!(filename)

      with_error_handling(filename) do
        self.instance_eval(IO.read(filename), filename, 1)
        self
      end
    end

    # Loads the contents of a file within the context of the current object's class
    #
    # @param [#to_s] filename
    #   path to the file to load
    #
    # @raise [IOError] if the file does not exist or cannot be read
    def class_from_file(filename)
      filename = filename.to_s

      ensure_presence!(filename)

      with_error_handling(filename) do
        self.class_eval(IO.read(filename), filename, 1)
        self
      end
    end

    private

    # Ensure the given filename and path is readable
    #
    # @param [String] filename
    #
    # @raise [IOError]
    #   if the target file does not exist or is not readable
    def ensure_presence!(filename)
      unless File.exists?(filename) && File.readable?(filename)
        raise IOError, "Could not open or read: '#{filename}'"
      end
    end

    # Execute the given block, handling any exceptions that occur
    #
    # @param [String] filename
    #
    # @raise [Ridley::Errors::FromFileParserError]
    #   if any exceptions if raised
    def with_error_handling(filename)
      yield
    rescue => e
      raise Ridley::Errors::FromFileParserError.new(filename, e)
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
ridley-5.1.1 lib/ridley/mixin/from_file.rb
ridley-5.1.0 lib/ridley/mixin/from_file.rb
ridley-5.0.0 lib/ridley/mixin/from_file.rb
ridley-4.6.1 lib/ridley/mixin/from_file.rb
ridley-4.6.0 lib/ridley/mixin/from_file.rb
ridley-4.5.1 lib/ridley/mixin/from_file.rb
ridley-4.5.0 lib/ridley/mixin/from_file.rb
ridley-4.4.3 lib/ridley/mixin/from_file.rb
ridley-4.4.2 lib/ridley/mixin/from_file.rb
ridley-4.4.1 lib/ridley/mixin/from_file.rb
ridley-4.4.0 lib/ridley/mixin/from_file.rb
ridley-4.3.2 lib/ridley/mixin/from_file.rb
ridley-4.3.1 lib/ridley/mixin/from_file.rb
ridley-4.3.0 lib/ridley/mixin/from_file.rb
ridley-4.2.0 lib/ridley/mixin/from_file.rb
ridley-4.1.2 lib/ridley/mixin/from_file.rb
ridley-4.1.1 lib/ridley/mixin/from_file.rb
ridley-4.1.0 lib/ridley/mixin/from_file.rb
ridley-4.0.0 lib/ridley/mixin/from_file.rb
ridley-3.1.0 lib/ridley/mixin/from_file.rb