Module: Ridley::Mixin::FromFile

Included in:
Chef::Cookbook::Metadata
Defined in:
lib/ridley/mixin/from_file.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



14
15
16
# File 'lib/ridley/mixin/from_file.rb', line 14

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



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

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



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

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