Class: ObjectInspector::Scope
- Inherits:
-
Object
- Object
- ObjectInspector::Scope
- 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.
Instance Attribute Summary collapse
-
#names ⇒ Array<#to_s>
The current value of names.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
(also: #eql?)
Compare self with the passed in object.
-
#initialize(names = %w[self])) ⇒ Scope
constructor
A new instance of Scope.
-
#join_flags(flags, separator: ObjectInspector.configuration.flags_separator) ⇒ Object
Join the passed-in flags with the passed in separator.
-
#join_info(items, separator: ObjectInspector.configuration.info_separator) ⇒ Object
Join the passed-in items with the passed in separator.
-
#join_issues(issues, separator: ObjectInspector.configuration.issues_separator) ⇒ Object
Join the passed-in issues with the passed in separator.
-
#join_name(parts, separator: ObjectInspector.configuration.name_separator) ⇒ Object
Join the passed-in name parts with the passed in separator.
- #to_a ⇒ Object
- #to_s(separator: ", ") ⇒ Object
Constructor Details
#initialize(names = %w[self])) ⇒ Scope
Returns a new instance of 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)
84 85 86 87 88 89 90 91 |
# File 'lib/object_inspector/scope.rb', line 84 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
#names ⇒ Array<#to_s>
Returns the current value of names.
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.
69 70 71 |
# File 'lib/object_inspector/scope.rb', line 69 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.
38 39 40 41 42 |
# File 'lib/object_inspector/scope.rb', line 38 def join_flags(flags, separator: ObjectInspector.configuration.flags_separator) the_flags = Array(flags).tap(&:compact!) the_flags.join(separator) if the_flags.any? end |
#join_info(items, separator: ObjectInspector.configuration.info_separator) ⇒ Object
Join the passed-in items with the passed in separator.
58 59 60 61 62 |
# File 'lib/object_inspector/scope.rb', line 58 def join_info(items, separator: ObjectInspector.configuration.info_separator) the_items = Array(items).tap(&:compact!) the_items.join(separator) if the_items.any? end |
#join_issues(issues, separator: ObjectInspector.configuration.issues_separator) ⇒ Object
Join the passed-in issues with the passed in separator.
48 49 50 51 52 |
# File 'lib/object_inspector/scope.rb', line 48 def join_issues(issues, separator: ObjectInspector.configuration.issues_separator) the_issues = Array(issues).tap(&:compact!) the_issues.join(separator) if the_issues.any? 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 32 |
# File 'lib/object_inspector/scope.rb', line 28 def join_name(parts, separator: ObjectInspector.configuration.name_separator) the_parts = Array(parts).tap(&:compact!) the_parts.join(separator) if the_parts.any? end |
#to_a ⇒ Object
78 79 80 |
# File 'lib/object_inspector/scope.rb', line 78 def to_a @names end |
#to_s(separator: ", ") ⇒ Object
74 75 76 |
# File 'lib/object_inspector/scope.rb', line 74 def to_s(separator: ", ") to_a.join(separator) end |