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.

TODO: rename this to a Locator or Loader or something

See Also:

Defined Under Namespace

Classes: Error, NoCustomGuardfile, NoGuardfileError, NoPluginsError

Constant Summary collapse

DEFAULT_GUARDFILES =
%w(
  guardfile.rb
  Guardfile
  ~/.Guardfile
).freeze
ERROR_NO_GUARDFILE =
"No Guardfile found,"\
" please create one with `guard init`."
ERROR_NO_PLUGINS =
"No Guard plugins found in Guardfile,"\
" please add at least one."

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Evaluator

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

content of a valid Guardfile

Parameters:

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

    a customizable set of options

Options Hash (opts):

  • guardfile (String)

    the path to a valid Guardfile

  • contents (String)

    a string representing the


58
59
60
61
# File 'lib/guard/guardfile/evaluator.rb', line 58

def initialize(opts = {})
  @guardfile_path = nil
  @opts = self.class.from_deprecated(opts)
end

Instance Attribute Details

#guardfile_pathObject (readonly)

Returns the value of attribute guardfile_path


29
30
31
# File 'lib/guard/guardfile/evaluator.rb', line 29

def guardfile_path
  @guardfile_path
end

Class Method Details

.from_deprecated(opts) ⇒ Object


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

def self.from_deprecated(opts)
  opts.dup.tap do |hash|
    hash[:contents] = hash.delete(:guardfile_contents) if hash.key?(:guardfile_contents)
  end
end

Instance Method Details

#custom?Boolean

Returns:

  • (Boolean)

109
110
111
# File 'lib/guard/guardfile/evaluator.rb', line 109

def custom?
  opts.key?(:guardfile)
end

#evaluateObject

Evaluates the DSL methods in the Guardfile.

path

options = { guardfile: '/Users/guardfile/MyAwesomeGuardfile' } Guard::Guardfile::Evaluator.new(options).evaluate

Examples:

Programmatically evaluate a Guardfile

Guard::Guardfile::Evaluator.new.evaluate

Programmatically evaluate a Guardfile with a custom Guardfile

Programmatically evaluate a Guardfile with an inline Guardfile


options = { contents: 'guard :rspec' }
Guard::Guardfile::Evaluator.new(options).evaluate

79
80
81
82
83
84
85
86
# File 'lib/guard/guardfile/evaluator.rb', line 79

def evaluate
  _use_inline || _use_custom || _use_default

  contents = guardfile_contents
  fail NoPluginsError, ERROR_NO_PLUGINS unless /guard/m =~ contents

  Dsl.new.evaluate(contents, guardfile_path || "", 1)
end

#guardfile_contentsObject


117
118
119
# File 'lib/guard/guardfile/evaluator.rb', line 117

def guardfile_contents
  [@contents, _user_config].compact.join("\n")
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

TODO: rename this method to it matches RSpec examples better

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


103
104
105
106
107
# File 'lib/guard/guardfile/evaluator.rb', line 103

def guardfile_include?(plugin_name)
  reader = DslReader.new
  reader.evaluate(@contents, guardfile_path || "", 1)
  reader.plugin_names.include?(plugin_name)
end

#inline?Boolean

Returns:

  • (Boolean)

113
114
115
# File 'lib/guard/guardfile/evaluator.rb', line 113

def inline?
  opts.key?(:contents)
end