Class: ObjectInspector::Scope

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

Overview

Defines a predicate method that matches #names 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.

Examples:

ObjectInspector::Scope.new
# => <ObjectInspector::Scope :: ["self"]>

ObjectInspector::Scope.new(:my_custom_scope)
# => <ObjectInspector::Scope :: ["my_custom_scope"]>

ObjectInspector::Scope.new(%w[verbose complex])
# => <ObjectInspector::Scope :: ["complex", "verbose"]>

ObjectInspector::Scope.new(:verbose, :self)
# => <ObjectInspector::Scope :: ["self", "verbose"]>

See Also:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*names) ⇒ Scope

:reek:FeatureEnvy



37
38
39
40
# File 'lib/object_inspector/scope.rb', line 37

def initialize(*names)
  names = names.empty? ? %w[self] : names.flatten
  @names = names.map! { |name| String(name) }.sort!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



113
114
115
116
117
118
119
120
# File 'lib/object_inspector/scope.rb', line 113

def method_missing(method_name, *args, &)
  if method_name[-1] == "?"
    scope_name = method_name[0..-2]
    evaluate_match(scope_name, &)
  else
    super
  end
end

Instance Attribute Details

#namesArray<#to_s>

Returns the current value of names.

Returns:

  • (Array<#to_s>)

    the current value of names



32
33
34
# File 'lib/object_inspector/scope.rb', line 32

def names
  @names
end

Instance Method Details

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

Compare self with the passed in object.

Returns:

  • (TrueClass)

    If self and other resolve to the same set of objects.

  • (FalseClass)

    If self and other resolve to a different set of objects.



91
92
93
# File 'lib/object_inspector/scope.rb', line 91

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

#inspectString

Returns A pretty representation of the scope and its #names.

Returns:

  • (String)

    A pretty representation of the scope and its #names.



107
108
109
# File 'lib/object_inspector/scope.rb', line 107

def inspect
  "<#{self.class.name} :: #{names.inspect}>"
end

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

Join the passed in flags with the passed in separator.

Parameters:

  • flags (Array<#to_s>)
  • separator (#to_s) (defaults to: ObjectInspector.configuration.flags_separator)

    (ObjectInspector.configuration.flags_separator)



57
58
59
60
61
62
# File 'lib/object_inspector/scope.rb', line 57

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.

Parameters:

  • items (Array<#to_s>)
  • separator (#to_s) (defaults to: ObjectInspector.configuration.info_separator)

    (ObjectInspector.configuration.info_separator)



79
80
81
82
83
84
# File 'lib/object_inspector/scope.rb', line 79

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.

Parameters:

  • issues (Array<#to_s>)
  • separator (#to_s) (defaults to: ObjectInspector.configuration.issues_separator)

    (ObjectInspector.configuration.issues_separator)



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

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.

Parameters:

  • parts (Array<#to_s>)
  • separator (#to_s) (defaults to: ObjectInspector.configuration.name_separator)

    (ObjectInspector.configuration.name_separator)



46
47
48
49
50
51
# File 'lib/object_inspector/scope.rb', line 46

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

#to_aArray<#to_s>

Returns the current value of names.

Returns:

  • (Array<#to_s>)

    the current value of names



102
103
104
# File 'lib/object_inspector/scope.rb', line 102

def to_a
  names
end

#to_s(separator: ", ") ⇒ String

Returns the contents of #names, joined by , .

Returns:

  • (String)

    the contents of #names, joined by , .



97
98
99
# File 'lib/object_inspector/scope.rb', line 97

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