Class: RubyLint::Definition::ConstantProxy

Inherits:
Object
  • Object
show all
Includes:
VariablePredicates
Defined in:
lib/ruby-lint/definition/constant_proxy.rb

Overview

ConstantProxy is a proxy class for constant definitions. The primary use case for this class is inheriting constants in the pre-generated definitions found in the definitions directory. By using this class when creating definitions the load order doesn’t matter, as long as the data is there at some point in time.

Constant Summary

Constants included from VariablePredicates

VariablePredicates::PREDICATE_METHODS, VariablePredicates::RUBY_CLASSES, VariablePredicates::VARIABLE_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VariablePredicates

#constant?, #constant_path?, #ruby_class, #variable?

Constructor Details

#initialize(source, name, registry = nil) ⇒ ConstantProxy

Returns a new instance of ConstantProxy

Parameters:

  • source (RubyLint::Definition::RubyObject)

    The source definition to use for looking up the definition associated with the current proxy.

  • name (String)

    The name/constant path of the constant that this proxy belongs to.

  • registry (RubyLint::Registry) (defaults to: nil)

    The registry to use when trying to autoload a constant.



38
39
40
41
42
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 38

def initialize(source, name, registry = nil)
  @proxy_source = source
  @proxy_name   = name
  @registry     = registry
end

Instance Attribute Details

#proxy_definitionRubyLint::Definition::RubyObject (readonly)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 22

class ConstantProxy
  include VariablePredicates

  attr_reader :proxy_source, :proxy_name, :proxy_definition, :registry

  ##
  # @param [RubyLint::Definition::RubyObject] source The source definition
  #  to use for looking up the definition associated with the current
  #  proxy.
  #
  # @param [String] name The name/constant path of the constant that this
  #  proxy belongs to.
  #
  # @param [RubyLint::Registry] registry The registry to use when trying
  #  to autoload a constant.
  #
  def initialize(source, name, registry = nil)
    @proxy_source = source
    @proxy_name   = name
    @registry     = registry
  end

  # Pre-define all the methods of the definition, this is faster than
  # having to rely on method_missing.
  RubyObject.instance_methods(false).each do |method|
    define_method(method) do |*args, &block|
      lookup_proxy_definition

      proxy_definition.send(method, *args, &block) if proxy_definition
    end
  end

  ##
  # @return [String]
  #
  def inspect
    lookup_proxy_definition

    return proxy_definition ? proxy_definition.inspect : super
  end

  private

  ##
  # Looks up the associated definition and stores it if it exists.
  #
  def lookup_proxy_definition
    return if proxy_definition

    found = lookup_constant
    root  = root_constant

    if !found and use_registry?(root)
      registry.apply(root, proxy_source)

      found = lookup_constant
    end

    @proxy_definition = found
  end

  ##
  # @param [String] constant
  # @return [TrueClass|FalseClass]
  #
  def use_registry?(constant)
    return false unless registry

    # Don't load the constant if we already have it.
    return true if registry.include?(constant)

    registry.load(constant)

    return registry.include?(constant)
  end

  ##
  # @return [String]
  #
  def root_constant
    return proxy_name.split(RubyObject::PATH_SEPARATOR)[0]
  end

  ##
  # @return [RubyLint::Definition::RubyObject]
  #
  def lookup_constant
    return proxy_source.lookup_constant_path(proxy_name)
  end
end

#proxy_nameString (readonly)

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 22

class ConstantProxy
  include VariablePredicates

  attr_reader :proxy_source, :proxy_name, :proxy_definition, :registry

  ##
  # @param [RubyLint::Definition::RubyObject] source The source definition
  #  to use for looking up the definition associated with the current
  #  proxy.
  #
  # @param [String] name The name/constant path of the constant that this
  #  proxy belongs to.
  #
  # @param [RubyLint::Registry] registry The registry to use when trying
  #  to autoload a constant.
  #
  def initialize(source, name, registry = nil)
    @proxy_source = source
    @proxy_name   = name
    @registry     = registry
  end

  # Pre-define all the methods of the definition, this is faster than
  # having to rely on method_missing.
  RubyObject.instance_methods(false).each do |method|
    define_method(method) do |*args, &block|
      lookup_proxy_definition

      proxy_definition.send(method, *args, &block) if proxy_definition
    end
  end

  ##
  # @return [String]
  #
  def inspect
    lookup_proxy_definition

    return proxy_definition ? proxy_definition.inspect : super
  end

  private

  ##
  # Looks up the associated definition and stores it if it exists.
  #
  def lookup_proxy_definition
    return if proxy_definition

    found = lookup_constant
    root  = root_constant

    if !found and use_registry?(root)
      registry.apply(root, proxy_source)

      found = lookup_constant
    end

    @proxy_definition = found
  end

  ##
  # @param [String] constant
  # @return [TrueClass|FalseClass]
  #
  def use_registry?(constant)
    return false unless registry

    # Don't load the constant if we already have it.
    return true if registry.include?(constant)

    registry.load(constant)

    return registry.include?(constant)
  end

  ##
  # @return [String]
  #
  def root_constant
    return proxy_name.split(RubyObject::PATH_SEPARATOR)[0]
  end

  ##
  # @return [RubyLint::Definition::RubyObject]
  #
  def lookup_constant
    return proxy_source.lookup_constant_path(proxy_name)
  end
end

#proxy_sourceRubyLint::Definition::RubyObject (readonly)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 22

class ConstantProxy
  include VariablePredicates

  attr_reader :proxy_source, :proxy_name, :proxy_definition, :registry

  ##
  # @param [RubyLint::Definition::RubyObject] source The source definition
  #  to use for looking up the definition associated with the current
  #  proxy.
  #
  # @param [String] name The name/constant path of the constant that this
  #  proxy belongs to.
  #
  # @param [RubyLint::Registry] registry The registry to use when trying
  #  to autoload a constant.
  #
  def initialize(source, name, registry = nil)
    @proxy_source = source
    @proxy_name   = name
    @registry     = registry
  end

  # Pre-define all the methods of the definition, this is faster than
  # having to rely on method_missing.
  RubyObject.instance_methods(false).each do |method|
    define_method(method) do |*args, &block|
      lookup_proxy_definition

      proxy_definition.send(method, *args, &block) if proxy_definition
    end
  end

  ##
  # @return [String]
  #
  def inspect
    lookup_proxy_definition

    return proxy_definition ? proxy_definition.inspect : super
  end

  private

  ##
  # Looks up the associated definition and stores it if it exists.
  #
  def lookup_proxy_definition
    return if proxy_definition

    found = lookup_constant
    root  = root_constant

    if !found and use_registry?(root)
      registry.apply(root, proxy_source)

      found = lookup_constant
    end

    @proxy_definition = found
  end

  ##
  # @param [String] constant
  # @return [TrueClass|FalseClass]
  #
  def use_registry?(constant)
    return false unless registry

    # Don't load the constant if we already have it.
    return true if registry.include?(constant)

    registry.load(constant)

    return registry.include?(constant)
  end

  ##
  # @return [String]
  #
  def root_constant
    return proxy_name.split(RubyObject::PATH_SEPARATOR)[0]
  end

  ##
  # @return [RubyLint::Definition::RubyObject]
  #
  def lookup_constant
    return proxy_source.lookup_constant_path(proxy_name)
  end
end

#registryRubyLint::Definition::Registry (readonly)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 22

class ConstantProxy
  include VariablePredicates

  attr_reader :proxy_source, :proxy_name, :proxy_definition, :registry

  ##
  # @param [RubyLint::Definition::RubyObject] source The source definition
  #  to use for looking up the definition associated with the current
  #  proxy.
  #
  # @param [String] name The name/constant path of the constant that this
  #  proxy belongs to.
  #
  # @param [RubyLint::Registry] registry The registry to use when trying
  #  to autoload a constant.
  #
  def initialize(source, name, registry = nil)
    @proxy_source = source
    @proxy_name   = name
    @registry     = registry
  end

  # Pre-define all the methods of the definition, this is faster than
  # having to rely on method_missing.
  RubyObject.instance_methods(false).each do |method|
    define_method(method) do |*args, &block|
      lookup_proxy_definition

      proxy_definition.send(method, *args, &block) if proxy_definition
    end
  end

  ##
  # @return [String]
  #
  def inspect
    lookup_proxy_definition

    return proxy_definition ? proxy_definition.inspect : super
  end

  private

  ##
  # Looks up the associated definition and stores it if it exists.
  #
  def lookup_proxy_definition
    return if proxy_definition

    found = lookup_constant
    root  = root_constant

    if !found and use_registry?(root)
      registry.apply(root, proxy_source)

      found = lookup_constant
    end

    @proxy_definition = found
  end

  ##
  # @param [String] constant
  # @return [TrueClass|FalseClass]
  #
  def use_registry?(constant)
    return false unless registry

    # Don't load the constant if we already have it.
    return true if registry.include?(constant)

    registry.load(constant)

    return registry.include?(constant)
  end

  ##
  # @return [String]
  #
  def root_constant
    return proxy_name.split(RubyObject::PATH_SEPARATOR)[0]
  end

  ##
  # @return [RubyLint::Definition::RubyObject]
  #
  def lookup_constant
    return proxy_source.lookup_constant_path(proxy_name)
  end
end

Instance Method Details

#inspectString

Returns:



57
58
59
60
61
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 57

def inspect
  lookup_proxy_definition

  return proxy_definition ? proxy_definition.inspect : super
end

#lookup_constantRubyLint::Definition::RubyObject (private)



108
109
110
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 108

def lookup_constant
  return proxy_source.lookup_constant_path(proxy_name)
end

#lookup_proxy_definitionObject (private)

Looks up the associated definition and stores it if it exists.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 68

def lookup_proxy_definition
  return if proxy_definition

  found = lookup_constant
  root  = root_constant

  if !found and use_registry?(root)
    registry.apply(root, proxy_source)

    found = lookup_constant
  end

  @proxy_definition = found
end

#root_constantString (private)

Returns:



101
102
103
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 101

def root_constant
  return proxy_name.split(RubyObject::PATH_SEPARATOR)[0]
end

#use_registry?(constant) ⇒ TrueClass|FalseClass (private)

Parameters:

Returns:

  • (TrueClass|FalseClass)


87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby-lint/definition/constant_proxy.rb', line 87

def use_registry?(constant)
  return false unless registry

  # Don't load the constant if we already have it.
  return true if registry.include?(constant)

  registry.load(constant)

  return registry.include?(constant)
end