Class: FPM::Cookery::Book

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fpm/cookery/book.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBook

Returns a new instance of Book.



11
12
13
# File 'lib/fpm/cookery/book.rb', line 11

def initialize
  @recipe = nil
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/fpm/cookery/book.rb', line 9

def config
  @config
end

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/fpm/cookery/book.rb', line 9

def filename
  @filename
end

Instance Method Details

#add_recipe_class(klass) ⇒ Object



25
26
27
# File 'lib/fpm/cookery/book.rb', line 25

def add_recipe_class(klass)
  @recipe = klass
end

#inject_class_methods!(klass) ⇒ Object

Hijack the recipe singleton to make the filename and config objects available when a descendent of BaseRecipe is declared. This makes it possible to do Hiera lookups at class definition time.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fpm/cookery/book.rb', line 32

def inject_class_methods!(klass)
  # It's necessary to close over local variables because the receiver
  # changes within the scope of +define_method+, so +self.filename+ would
  # wrongly refer to +singleton_klass.filename+.
  filename = self.filename
  config = self.config

  singleton_klass = (class << klass ; self ; end)

  singleton_klass.send(:define_method, :filename) do
    filename
  end

  singleton_klass.send(:define_method, :config) do
    config
  end
end

#load_recipe(filename, config, &callback) ⇒ Object

Load the given file and instantiate an object. Wrap the class in an anonymous module to avoid namespace cluttering. (see Kernel.load)



17
18
19
20
21
22
23
# File 'lib/fpm/cookery/book.rb', line 17

def load_recipe(filename, config, &callback)
  @filename = FPM::Cookery::Path.new(filename).realpath
  @config = config

  Kernel.load(@filename.to_path, true)
  callback.call(@recipe.new)
end