Module: Nutils::Helpers::Rules

Defined in:
lib/nutils/helpers/rules.rb

Overview

Provides some helper functions to process Rules.

Author:

  • Arnau Siches

Version:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#load_rules(filepath) ⇒ Proc

Compiles the content as a set of Nanoc rules. Tries to use the rules_dir attribute from the Config.yaml. If rules_dir is not defined, it uses “.” as a base path.

Examples:

Config.yaml excerpt


rules_dir: ['lib/rules/**', 'lib/other_rules']

Rules excerpt


# Finds any foo.rule in all directories defined in the Config.yaml
load_rules('foo.rule')

Parameters:

  • filepath (String)

    The file path to load.

Returns:

  • (Proc)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nutils/helpers/rules.rb', line 27

def load_rules(filepath)
  raise "You should upgrade to nanoc 3.2.1 to run the load_rules helper properly" if Gem.loaded_specs["nanoc3"].version == Gem::Version.create('3.2')

  rules_dir = if Gem.loaded_specs["nanoc3"].version >= Gem::Version.create('3.1') and Gem.loaded_specs["nanoc3"].version < Gem::Version.create('3.2')
    (@site.config[:rules_dir] || ['.']) 
  else
    @config[:rules_dir] || ['.']
  end
  


  path = rules_dir.map do |dir|
    Dir[File.join(dir, filepath)].each { |filename| filename }
  end.flatten

  if path.length > 1
    puts "\e[1mwarning\e[0m Multiple matches for #{filepath}:",
         "  #{path.join("\n  ")}",
         "  \e[1mused\e[0m #{path[0]}"
  elsif path.length == 0
    raise Nutils::Errors::NoRulesFileFound.new(filepath)
  end

  instance_eval(File.read(path[0]), path[0])
end