Module: Chozo::Mixin::FromFile

Defined in:
lib/chozo/mixin/from_file.rb

Overview

Author:

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
# File 'lib/chozo/mixin/from_file.rb', line 15

def included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#class_from_file(filename) ⇒ Object

Loads the contents of a file within the context of the current object’s class

Parameters:

  • filename (#to_s)

    path to the file to load

Raises:

  • (IOError)

    if the file does not exist or cannot be read



43
44
45
46
47
48
49
50
51
52
# File 'lib/chozo/mixin/from_file.rb', line 43

def class_from_file(filename)
  filename = filename.to_s

  if File.exists?(filename) && File.readable?(filename)
    self.class_eval(IO.read(filename), filename, 1)
    self
  else
    raise IOError, "Could not open or read: '#{filename}'"
  end
end

#from_file(filename) ⇒ Object

Loads the contents of a file within the context of the current object

Parameters:

  • filename (#to_s)

    path to the file to load

Raises:

  • (IOError)

    if the file does not exist or cannot be read



26
27
28
29
30
31
32
33
34
35
# File 'lib/chozo/mixin/from_file.rb', line 26

def from_file(filename)
  filename = filename.to_s

  if File.exists?(filename) && File.readable?(filename)
    self.instance_eval(IO.read(filename), filename, 1)
    self
  else
    raise IOError, "Could not open or read: '#{filename}'"
  end
end