Class: FoodCritic::RuleDsl

Inherits:
Object
  • Object
show all
Includes:
Api
Defined in:
lib/foodcritic/dsl.rb

Overview

The DSL methods exposed for defining rules. A minimal example rule:

rule "FC123", "My rule name" do
  tags %w{style attributes}
    recipe do |ast|
      ## Rule implementation body
    end
end
  • Each rule is defined within a ‘rule` block that defines the code and name of the rule.

  • Each rule block may contain further nested blocks for the components of the cookbook that it is interested in linting. For example ‘cookbook`, `recipe` or `library`.

  • Further DSL methods are available to define the ‘tags` and Chef versions the rule `applies_to`.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Api

#ast_cache, #attribute_access, #cookbook_base_path, #cookbook_maintainer, #cookbook_maintainer_email, #cookbook_name, #declared_dependencies, #ensure_file_exists, #field, #field_value, #file_match, #find_resources, #gem_version, #included_recipes, #json_file_to_hash, #literal_searches, #match, #metadata_field, #read_ast, #resource_attribute, #resource_attributes, #resource_attributes_by_type, #resource_name, #resource_type, #resources_by_type, #ruby_code?, #searches, #standard_cookbook_subdirs, #supported_platforms, #template_file, #template_paths, #templates_included

Methods included from Notifications

#notifications

Methods included from Chef

#chef_dsl_methods, #chef_node_methods, #resource_action?, #resource_attribute?, #valid_query?

Constructor Details

#initialize(chef_version = Linter::DEFAULT_CHEF_VERSION) ⇒ RuleDsl

Returns a new instance of RuleDsl.



29
30
31
# File 'lib/foodcritic/dsl.rb', line 29

def initialize(chef_version = Linter::DEFAULT_CHEF_VERSION)
  @chef_version = chef_version
end

Instance Attribute Details

#chef_versionObject (readonly)

Returns the value of attribute chef_version.



25
26
27
# File 'lib/foodcritic/dsl.rb', line 25

def chef_version
  @chef_version
end

#rulesObject (readonly)

Returns the value of attribute rules.



24
25
26
# File 'lib/foodcritic/dsl.rb', line 24

def rules
  @rules
end

Class Method Details

.load(paths, chef_version = Linter::DEFAULT_CHEF_VERSION) ⇒ Object

Load the ruleset(s).



74
75
76
77
78
79
80
81
82
# File 'lib/foodcritic/dsl.rb', line 74

def self.load(paths, chef_version = Linter::DEFAULT_CHEF_VERSION)
  dsl = RuleDsl.new(chef_version)
  paths.map do |path|
    File.directory?(path) ? Dir["#{path}/**/*.rb"].sort : path
  end.flatten.each do |path|
    dsl.instance_eval(File.read(path), path)
  end
  dsl.rules
end

.rule_block(name) ⇒ Object



52
53
54
55
56
# File 'lib/foodcritic/dsl.rb', line 52

def self.rule_block(name)
  define_method(name) do |&block|
    rules.last.send("#{name}=".to_sym, block)
  end
end

Instance Method Details

#applies_to(&block) ⇒ Object

Alternative to tags. Commonly used to constrain a rule to run only when linting specific Chef versions.



48
49
50
# File 'lib/foodcritic/dsl.rb', line 48

def applies_to(&block)
  rules.last.applies_to = block
end

#rule(code, name) {|_self| ... } ⇒ Object

Define a new rule, the outer block of a rule definition.

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
38
# File 'lib/foodcritic/dsl.rb', line 34

def rule(code, name, &block)
  @rules = [] if @rules.nil?
  @rules << Rule.new(code, name)
  yield self
end

#tags(tags) ⇒ Object

Add tags to the rule which can be used by the end user to filter the rules to be applied.



42
43
44
# File 'lib/foodcritic/dsl.rb', line 42

def tags(tags)
  rules.last.tags += tags
end