Class: ObjectInspector::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/object_inspector/scope.rb

Overview

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 Also:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names = %w[self])) ⇒ Scope



20
21
22
# File 'lib/object_inspector/scope.rb', line 20

def initialize(names = %w[self])
  @names = Array(names).map { |name| String(name) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



80
81
82
83
84
85
86
87
# 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

Instance Attribute Details

#namesArray<#to_s>



17
18
19
# File 'lib/object_inspector/scope.rb', line 17

def names
  @names
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass Also known as: eql?

Compare self with the passed in object.



65
66
67
# File 'lib/object_inspector/scope.rb', line 65

def ==(other)
  @names.sort == Array(other).map(&:to_s).sort
end

#join_flags(flags, separator: ObjectInspector.configuration.flags_separator) ⇒ Object

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



37
38
39
40
# 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) ⇒ Object

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



55
56
57
58
# 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) ⇒ Object

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



46
47
48
49
# 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) ⇒ Object

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



28
29
30
31
# File 'lib/object_inspector/scope.rb', line 28

def join_name(parts,
              separator: ObjectInspector.configuration.name_separator)
  _join(parts, separator)
end

#to_aObject



74
75
76
# File 'lib/object_inspector/scope.rb', line 74

def to_a
  @names
end

#to_s(separator: ", ") ⇒ Object



70
71
72
# File 'lib/object_inspector/scope.rb', line 70

def to_s(separator: ", ")
  to_a.join(separator)
end