Class: Guard::Internals::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/internals/session.rb

Overview

TODO: split into a commandline class and session (plugins, groups) TODO: swap session and metadata

Constant Summary collapse

DEFAULT_OPTIONS =
{
  clear: false,
  debug: false,
  no_bundler_warning: false,

  # User defined scopes
  group: [],
  plugin: [],

  # Notifier
  notify: true,

  # Interactor
  no_interactions: false,

  # Guardfile options:
  # guardfile_contents
  guardfile: nil,

  # Listener options
  # TODO: rename to watchdirs?
  watchdir: Dir.pwd,
  latency: nil,
  force_polling: false,
  wait_for_delay: nil,
  listen_on: nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_options) ⇒ Session

Returns a new instance of Session.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/guard/internals/session.rb', line 51

def initialize(new_options)
  @options = Options.new(new_options, DEFAULT_OPTIONS)

  @plugins = Plugins.new
  @groups = Groups.new

  @cmdline_groups = @options[:group]
  @cmdline_plugins = @options[:plugin]

  @clear = @options[:clear]
  @debug = @options[:debug]
  @watchdirs = Array(@options[:watchdir])
  @notify = @options[:notify]
  @interactor_name = @options[:no_interactions] ? :sleep : :pry_wrapper

  @guardfile_plugin_scope = []
  @guardfile_group_scope = []
  @guardfile_ignore = []
  @guardfile_ignore_bang = []

  @guardfile_notifier_options = {}
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



13
14
15
# File 'lib/guard/internals/session.rb', line 13

def groups
  @groups
end

#guardfile_group_scopeObject (readonly)

TODO: create a EvaluatorResult class?



89
90
91
# File 'lib/guard/internals/session.rb', line 89

def guardfile_group_scope
  @guardfile_group_scope
end

#guardfile_ignoreObject

Returns the value of attribute guardfile_ignore.



93
94
95
# File 'lib/guard/internals/session.rb', line 93

def guardfile_ignore
  @guardfile_ignore
end

#guardfile_ignore_bangObject

Returns the value of attribute guardfile_ignore_bang.



91
92
93
# File 'lib/guard/internals/session.rb', line 91

def guardfile_ignore_bang
  @guardfile_ignore_bang
end

#guardfile_plugin_scopeObject (readonly)

Returns the value of attribute guardfile_plugin_scope.



90
91
92
# File 'lib/guard/internals/session.rb', line 90

def guardfile_plugin_scope
  @guardfile_plugin_scope
end

#interactor_nameObject (readonly)

Returns the value of attribute interactor_name.



159
160
161
# File 'lib/guard/internals/session.rb', line 159

def interactor_name
  @interactor_name
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



12
13
14
# File 'lib/guard/internals/session.rb', line 12

def plugins
  @plugins
end

Instance Method Details

#clearing(on) ⇒ Object



98
99
100
# File 'lib/guard/internals/session.rb', line 98

def clearing(on)
  @clear = on
end

#clearing?Boolean Also known as: clear?

Returns:

  • (Boolean)


102
103
104
# File 'lib/guard/internals/session.rb', line 102

def clearing?
  @clear
end

#cmdline_groupsObject



43
44
45
# File 'lib/guard/internals/session.rb', line 43

def cmdline_groups
  @cmdline_groups.dup.freeze
end

#cmdline_pluginsObject



47
48
49
# File 'lib/guard/internals/session.rb', line 47

def cmdline_plugins
  @cmdline_plugins.dup.freeze
end

#convert_scope(entries) ⇒ Object

TODO: call this from within action, not within interactor command



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/guard/internals/session.rb', line 162

def convert_scope(entries)
  scopes = { plugins: [], groups: [] }
  unknown = []

  entries.each do |entry|
    if plugin = plugins.all(entry).first
      scopes[:plugins] << plugin
    elsif group = groups.all(entry).first
      scopes[:groups] << group
    else
      unknown << entry
    end
  end

  [scopes, unknown]
end

#debug?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/guard/internals/session.rb', line 108

def debug?
  @debug
end

#evaluator_optionsObject



136
137
138
139
140
141
142
143
# File 'lib/guard/internals/session.rb', line 136

def evaluator_options
  opts = { guardfile: @options[:guardfile] }
  # TODO: deprecate :guardfile_contents
  if @options[:guardfile_contents]
    opts[:contents] = @options[:guardfile_contents]
  end
  opts
end

#guardfile_notification=(config) ⇒ Object



155
156
157
# File 'lib/guard/internals/session.rb', line 155

def guardfile_notification=(config)
  @guardfile_notifier_options.merge!(config)
end

#guardfile_scope(scope) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/guard/internals/session.rb', line 74

def guardfile_scope(scope)
  opts = scope.dup

  groups = Array(opts.delete(:groups))
  group = Array(opts.delete(:group))
  @guardfile_group_scope = Array(groups) + Array(group)

  plugins = Array(opts.delete(:plugins))
  plugin = Array(opts.delete(:plugin))
  @guardfile_plugin_scope = Array(plugins) + Array(plugin)

  fail "Unknown options: #{opts.inspect}" unless opts.empty?
end

#listener_argsObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/guard/internals/session.rb', line 123

def listener_args
  if @options[:listen_on]
    [:on, @options[:listen_on]]
  else
    listener_options = {}
    [:latency, :force_polling, :wait_for_delay].each do |option|
      listener_options[option] = @options[option] if @options[option]
    end
    expanded_watchdirs = watchdirs.map { |dir| File.expand_path dir }
    [:to, *expanded_watchdirs, listener_options]
  end
end

#notify_optionsObject



145
146
147
148
149
150
151
152
153
# File 'lib/guard/internals/session.rb', line 145

def notify_options
  names = @guardfile_notifier_options.keys
  return { notify: false } if names.include?(:off)

  {
    notify: @options[:notify],
    notifiers: @guardfile_notifier_options
  }
end

#watchdirsObject



112
113
114
115
# File 'lib/guard/internals/session.rb', line 112

def watchdirs
  @watchdirs_from_guardfile ||= nil
  @watchdirs_from_guardfile || @watchdirs
end

#watchdirs=(dirs) ⇒ Object

set by Dsl with :directories() command



118
119
120
121
# File 'lib/guard/internals/session.rb', line 118

def watchdirs=(dirs)
  dirs = [Dir.pwd] if dirs.empty?
  @watchdirs_from_guardfile = dirs.map { |dir| File.expand_path dir }
end