Class: Guard::Guardfile::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/guardfile/evaluator.rb

Overview

This class is responsible for evaluating the Guardfile. It delegates to Guard::Dsl for the actual objects generation from the Guardfile content.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Evaluator

Initializes a new Guard::Guardfile::Evaluator object.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • guardfile (String)

    the path to a valid Guardfile

  • guardfile_contents (String)

    a string representing the content of a valid Guardfile



20
21
22
# File 'lib/guard/guardfile/evaluator.rb', line 20

def initialize(opts = {})
  @options = ::Guard::Options.new(opts.select { |k, _| [:guardfile, :guardfile_contents].include?(k.to_sym) })
end

Instance Attribute Details

#guardfile_pathObject (readonly)

Returns the value of attribute guardfile_path



13
14
15
# File 'lib/guard/guardfile/evaluator.rb', line 13

def guardfile_path
  @guardfile_path
end

#guardfile_sourceObject (readonly)

Returns the value of attribute guardfile_source



13
14
15
# File 'lib/guard/guardfile/evaluator.rb', line 13

def guardfile_source
  @guardfile_source
end

#optionsObject (readonly)

Returns the value of attribute options



13
14
15
# File 'lib/guard/guardfile/evaluator.rb', line 13

def options
  @options
end

Instance Method Details

#evaluate_guardfileObject

Evaluates the DSL methods in the `Guardfile`.

Examples:

Programmatically evaluate a Guardfile

Guard::Guardfile::Evaluator.new.evaluate_guardfile

Programmatically evaluate a Guardfile with a custom Guardfile path

Guard::Guardfile::Evaluator.new(guardfile: '/Users/guardfile/MyAwesomeGuardfile').evaluate_guardfile

Programmatically evaluate a Guardfile with an inline Guardfile

Guard::Guardfile::Evaluator.new(guardfile_contents: 'guard :rspec').evaluate_guardfile


35
36
37
38
# File 'lib/guard/guardfile/evaluator.rb', line 35

def evaluate_guardfile
  _fetch_guardfile_contents
  _instance_eval_guardfile(guardfile_contents)
end

#guardfile_contentsString

Gets the content of the `Guardfile` concatenated with the global user configuration file.

Examples:

Programmatically get the content of the current Guardfile

Guard::Guardfile::Evaluator.new.guardfile_contents
=> "guard :rspec"

Returns:

  • (String)

    the Guardfile content



77
78
79
80
# File 'lib/guard/guardfile/evaluator.rb', line 77

def guardfile_contents
  config = File.read(_user_config_path) if File.exist?(_user_config_path)
  [_guardfile_contents_without_user_config, config].compact.join("\n")
end

#guardfile_include?(plugin_name) ⇒ Boolean

Tests if the current `Guardfile` contains a specific Guard plugin.

Examples:

Programmatically test if a Guardfile contains a specific Guard plugin

File.read('Guardfile')
=> "guard :rspec"

Guard::Guardfile::Evaluator.new.guardfile_include?('rspec)
=> true

Parameters:

  • plugin_name (String)

    the name of the Guard

Returns:

  • (Boolean)

    whether the Guard plugin has been declared



64
65
66
# File 'lib/guard/guardfile/evaluator.rb', line 64

def guardfile_include?(plugin_name)
  _guardfile_contents_without_user_config.match(/^guard\s*\(?\s*['":]#{ plugin_name }['"]?/)
end

#reevaluate_guardfileObject

Re-evaluates the `Guardfile` to update the current Guard configuration.



43
44
45
46
47
48
49
50
# File 'lib/guard/guardfile/evaluator.rb', line 43

def reevaluate_guardfile
  # Don't re-evaluate inline Guardfile
  return if @guardfile_source == :inline

  _before_reevaluate_guardfile
  evaluate_guardfile
  _after_reevaluate_guardfile
end