class Guard::Internals::Scope

Public Class Methods

new() click to toggle source
# File lib/guard/internals/scope.rb, line 7
def initialize
  @interactor_plugin_scope = []
  @interactor_group_scope = []
end

Public Instance Methods

from_interactor(scope) click to toggle source
# File lib/guard/internals/scope.rb, line 51
def from_interactor(scope)
  @interactor_plugin_scope = Array(scope[:plugins])
  @interactor_group_scope = Array(scope[:groups])
end
grouped_plugins(scope = { plugins: [], groups: [] }) click to toggle source

TODO: refactor

# File lib/guard/internals/scope.rb, line 20
def grouped_plugins(scope = { plugins: [], groups: [] })
  items = nil
  plugins = _find_non_empty_scope(:plugins, scope)
  if plugins
    items = Array(plugins).map { |plugin| _instantiate(:plugin, plugin) }
  end

  unless items
    # TODO: no coverage here!!
    found = _find_non_empty_scope(:groups, scope)
    found ||= Guard.state.session.groups.all
    groups = Array(found).map { |group| _instantiate(:group, group) }
    if groups.any? { |g| g.name == :common }
      items = groups
    else
      items = ([_instantiate(:group, :common)] + Array(found)).compact
    end
  end

  items.map do |plugin_or_group|
    group = nil
    plugins = [plugin_or_group]
    if plugin_or_group.is_a?(Group)
      # TODO: no coverage here!
      group = plugin_or_group
      plugins = Guard.state.session.plugins.all(group: group.name)
    end
    [group, plugins]
  end
end
titles(scope = nil) click to toggle source
# File lib/guard/internals/scope.rb, line 56
def titles(scope = nil)
  hash = scope || to_hash
  plugins = hash[:plugins]
  groups = hash[:groups]
  return plugins.map(&:title) unless plugins.nil? || plugins.empty?
  return hash[:groups].map(&:title) unless groups.nil? || groups.empty?
  ["all"]
end
to_hash() click to toggle source
# File lib/guard/internals/scope.rb, line 12
def to_hash
  {
    plugins: _hashify_scope(:plugin),
    groups: _hashify_scope(:group)
  }.dup.freeze
end

Private Instance Methods

_find_non_empty_scope(type, local_scope) click to toggle source
# File lib/guard/internals/scope.rb, line 108
def _find_non_empty_scope(type, local_scope)
  [Array(local_scope[type]), to_hash[type]].map(&:compact).detect(&:any?)
end
_groups() click to toggle source
# File lib/guard/internals/scope.rb, line 112
def _groups
  Guard.state.session.groups
end
_hashify_scope(type) click to toggle source

TODO: let the Plugins and Groups classes handle this? TODO: why even instantiate?? just to check if it exists?

# File lib/guard/internals/scope.rb, line 77
def _hashify_scope(type)
  # TODO: get cmdline passed to initialize above?
  cmdline = Array(Guard.state.session.send("cmdline_#{type}s"))
  guardfile = Guard.state.session.send(:"guardfile_#{type}_scope")
  interactor = instance_variable_get(:"@interactor_#{type}_scope")

  # TODO: session should decide whether to use cmdline or guardfile -
  # since it has access to both variables
  items = [interactor, cmdline, guardfile].detect do |source|
    !source.empty?
  end

  # TODO: not tested when groups/plugins given don't exist

  # TODO: should already be instantiated
  Array(items).map do |obj|
    if obj.respond_to?(:name)
      obj
    else
      name = obj
      (type == :group ? _groups : _plugins).all(name).first
    end
  end.compact
end
_instantiate(meth, obj) click to toggle source
# File lib/guard/internals/scope.rb, line 102
def _instantiate(meth, obj)
  # TODO: no coverage
  return obj unless obj.is_a?(Symbol) || obj.is_a?(String)
  Guard.state.session.send("#{meth}s".to_sym).all(obj).first
end
_plugins() click to toggle source
# File lib/guard/internals/scope.rb, line 116
def _plugins
  Guard.state.session.plugins
end
_scope_names(new_scope, name) click to toggle source

TODO: move to session

# File lib/guard/internals/scope.rb, line 68
def _scope_names(new_scope, name)
  items = Array(new_scope[:"#{name}s"] || new_scope[name]) if items.empty?

  # Convert objects to names
  items.map { |p| p.respond_to?(:name) ? p.name : p }
end