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:

  • Dsl

Defined Under Namespace

Classes: NoCustomGuardfile, NoGuardfileError

Constant Summary collapse

DEFAULT_GUARDFILES =
%w(
  guardfile.rb
  Guardfile
  ~/.Guardfile
).freeze
EVALUATOR_OPTIONS =
%i[guardfile inline].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Evaluator

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

content of a valid Guardfile

Parameters:

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

    a customizable set of options

Options Hash (options):

  • guardfile (String)

    the path to a valid Guardfile



42
43
44
45
46
# File 'lib/guard/guardfile/evaluator.rb', line 42

def initialize(options = {})
  @guardfile_path = nil
  @options = Options.new(options.slice(*Guard::Guardfile::Evaluator::EVALUATOR_OPTIONS))
  @dsl = Dsl.new
end

Instance Attribute Details

#guardfile_pathObject (readonly)

Returns the value of attribute guardfile_path.



25
26
27
# File 'lib/guard/guardfile/evaluator.rb', line 25

def guardfile_path
  @guardfile_path
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/guard/guardfile/evaluator.rb', line 25

def options
  @options
end

Instance Method Details

#custom?Boolean

Returns:

  • (Boolean)


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

def custom?
  !!options[:guardfile]
end

#evaluateObject

Evaluates the DSL methods in the Guardfile.

Returns:

  • Guard::Guardfile::Result



52
53
54
55
56
57
# File 'lib/guard/guardfile/evaluator.rb', line 52

def evaluate
  @evaluate ||= begin
    dsl.evaluate(guardfile_contents, guardfile_path || "", 1)
    dsl.result
  end
end

#guardfile_contentsObject



85
86
87
88
89
90
91
# File 'lib/guard/guardfile/evaluator.rb', line 85

def guardfile_contents
  @guardfile_contents ||= begin
    _use_inline || _use_custom || _use_default

    [@contents, _user_config].compact.join("\n")
  end
end

#guardfile_include?(plugin_name) ⇒ Boolean

Tests if the current Guardfile contains a specific Guard plugin.

plugin

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

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

Examples:

Programmatically test if a Guardfile contains a specific Guard

Parameters:

  • plugin_name (String)

    the name of the Guard

Returns:

  • (Boolean)

    whether the Guard plugin has been declared



73
74
75
# File 'lib/guard/guardfile/evaluator.rb', line 73

def guardfile_include?(plugin_name)
  evaluate.plugin_names.include?(plugin_name.to_sym)
end

#inline?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/guard/guardfile/evaluator.rb', line 81

def inline?
  !!options[:inline]
end