class ObjectInspector::Scope

ObjectInspector::Scope defines a predicate method that matches {#name} and responds with `true`. This is a prettier way to test for a given type of “scope” within objects.

It is possible to pass in multiple scope names to match on. `:all` is a “wild card” scope name, and will match on all scope names. Passing a block to a scope predicate falls back to the out-of-scope placeholder (`*` by default) if the scope does not match.

@see ActiveSupport::StringInquirer

http://api.rubyonrails.org/classes/ActiveSupport/StringInquirer.html

@attr names [Array<#to_s>]

Attributes

names[R]

Public Class Methods

new(names = %w[self]) click to toggle source
# File lib/object_inspector/scope.rb, line 20
def initialize(names = %w[self])
  @names = Array(names).map { |name| String(name) }
end

Public Instance Methods

==(other) click to toggle source

Compare self with the passed in object.

@return [TrueClass] if self and `other` resolve to the same set of objects @return [FalseClass] if self and `other` resolve to a different set of

objects
# File lib/object_inspector/scope.rb, line 65
def ==(other)
  @names.sort == Array(other).map(&:to_s).sort
end
Also aliased as: eql?
eql?(other)
Alias for: ==
join_flags(flags, separator: ObjectInspector.configuration.flags_separator) click to toggle source

Join the passed-in flags with the passed in separator.

@param flags [Array<#to_s>] @param separator [#to_s] (ObjectInspector.configuration.flags_separator)

# File lib/object_inspector/scope.rb, line 37
def join_flags(flags,
               separator: ObjectInspector.configuration.flags_separator)
  _join(flags, separator)
end
join_info(items, separator: ObjectInspector.configuration.info_separator) click to toggle source

Join the passed-in items with the passed in separator.

@param items [Array<#to_s>] @param separator [#to_s] (ObjectInspector.configuration.info_separator)

# File lib/object_inspector/scope.rb, line 55
def join_info(items,
              separator: ObjectInspector.configuration.info_separator)
  _join(items, separator)
end
join_issues(issues, separator: ObjectInspector.configuration.issues_separator) click to toggle source

Join the passed-in issues with the passed in separator.

@param issues [Array<#to_s>] @param separator [#to_s] (ObjectInspector.configuration.issues_separator)

# File lib/object_inspector/scope.rb, line 46
def join_issues(issues,
                separator: ObjectInspector.configuration.issues_separator)
  _join(issues, separator)
end
join_name(parts, separator: ObjectInspector.configuration.name_separator) click to toggle source

Join the passed-in name parts with the passed in separator.

@param parts [Array<#to_s>] @param separator [#to_s] (ObjectInspector.configuration.flags_separator)

# File lib/object_inspector/scope.rb, line 28
def join_name(parts,
              separator: ObjectInspector.configuration.name_separator)
  _join(parts, separator)
end
to_a() click to toggle source
# File lib/object_inspector/scope.rb, line 74
def to_a
  @names
end
to_s(separator: ", ") click to toggle source
# File lib/object_inspector/scope.rb, line 70
def to_s(separator: ", ")
  to_a.join(separator)
end

Private Instance Methods

_join(items, separator) click to toggle source
# File lib/object_inspector/scope.rb, line 107
def _join(items, separator)
  items = Array(items)
  items.flatten!
  items.compact!
  items.join(separator) unless items.empty?
end
any_names_match?(other_name) click to toggle source
# File lib/object_inspector/scope.rb, line 124
def any_names_match?(other_name)
  @names.any? { |name| name == other_name }
end
evaluate_block_if(condition) { |self| ... } click to toggle source
# File lib/object_inspector/scope.rb, line 99
def evaluate_block_if(condition)
  if condition
    yield(self)
  else
    ObjectInspector.configuration.out_of_scope_placeholder
  end
end
evaluate_match(scope_name, &block) click to toggle source
# File lib/object_inspector/scope.rb, line 89
def evaluate_match(scope_name, &block)
  is_a_match = match?(scope_name)

  if block
    evaluate_block_if(is_a_match, &block)
  else
    is_a_match
  end
end
match?(scope_name) click to toggle source
# File lib/object_inspector/scope.rb, line 114
def match?(scope_name)
  any_names_match?(scope_name) ||
    wild_card_scope?
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/object_inspector/scope.rb, line 80
def method_missing(method_name, *args, &block)
  if method_name[-1] == "?"
    scope_name = method_name[0..-2]
    evaluate_match(scope_name, &block)
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/object_inspector/scope.rb, line 128
def respond_to_missing?(method_name, include_private = false)
  method_name[-1] == "?" || super
end
wild_card_scope?() click to toggle source
# File lib/object_inspector/scope.rb, line 119
def wild_card_scope?
  @wild_card_scope ||=
    any_names_match?(ObjectInspector.configuration.wild_card_scope)
end