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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/guard/guardfile/evaluator.rb', line 57

def initialize(opts = {})
  @type = nil
  @path = nil
  @user_config = nil

  opts = _from_deprecated(opts)

  if opts[:contents]
    @type = :inline
    @contents = opts[:contents]
  elsif opts[:guardfile]
    @type = :custom
    @path = Pathname.new(opts[:guardfile]) # may be updated by _read
  end
end

Instance Attribute Details

#guardfile_pathObject (readonly)

Returns the value of attribute guardfile_path.



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

def guardfile_path
  @guardfile_path
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



119
120
121
# File 'lib/guard/guardfile/evaluator.rb', line 119

def path
  @path
end

Instance Method Details

#custom?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/guard/guardfile/evaluator.rb', line 121

def custom?
  @type == :custom
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


89
90
91
92
93
94
95
96
# File 'lib/guard/guardfile/evaluator.rb', line 89

def evaluate
  inline? || _use_provided || _use_default!

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

  Dsl.new.evaluate(contents, @path || "", 1)
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



134
135
136
137
# File 'lib/guard/guardfile/evaluator.rb', line 134

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.

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



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

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

#guardfile_sourceObject



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

def guardfile_source
  @source
end

#inline?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/guard/guardfile/evaluator.rb', line 139

def inline?
  @type == :inline
end