module Ridley::Mixin::FromFile

Public Class Methods

included(base) click to toggle source
# File lib/ridley/mixin/from_file.rb, line 14
def included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

class_from_file(filename) click to toggle source

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

# File lib/ridley/mixin/from_file.rb, line 42
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
from_file(filename) click to toggle source

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

# File lib/ridley/mixin/from_file.rb, line 25
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

Private Instance Methods

ensure_presence!(filename) click to toggle source

Ensure the given filename and path is readable

@param [String] filename

@raise [IOError]

if the target file does not exist or is not readable
# File lib/ridley/mixin/from_file.rb, line 61
def ensure_presence!(filename)
  unless File.exists?(filename) && File.readable?(filename)
    raise IOError, "Could not open or read: '#{filename}'"
  end
end
with_error_handling(filename) { || ... } click to toggle source

Execute the given block, handling any exceptions that occur

@param [String] filename

@raise [Ridley::Errors::FromFileParserError]

if any exceptions if raised
# File lib/ridley/mixin/from_file.rb, line 73
def with_error_handling(filename)
  yield
rescue => e
  raise Ridley::Errors::FromFileParserError.new(filename, e)
end