Class: RubyLint::Definition::Registry

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

Overview

The Registry class is used to register and store definitions that have to be applied to instances of VirtualMachine. It can also be used to load constant definition files from a custom load path.

Constant Summary

DEFAULT_LOAD_PATH =

The default load path to use.

Returns:

  • (Array)
[
  File.expand_path('../../definitions/core', __FILE__),
  File.expand_path('../../definitions/rails', __FILE__),
  File.expand_path('../../definitions/gems', __FILE__)
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry



33
34
35
36
37
# File 'lib/ruby-lint/definition/registry.rb', line 33

def initialize
  @registered       = {}
  @load_path        = DEFAULT_LOAD_PATH.dup
  @loaded_constants = Set.new
end

Instance Attribute Details

#load_pathArray (readonly)

Returns List of directories to search in for definitions.

Returns:

  • (Array)

    List of directories to search in for definitions.



19
20
21
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby-lint/definition/registry.rb', line 19

class Registry
  attr_reader :load_path, :loaded_constants, :registered

  ##
  # The default load path to use.
  #
  # @return [Array]
  #
  DEFAULT_LOAD_PATH = [
    File.expand_path('../../definitions/core', __FILE__),
    File.expand_path('../../definitions/rails', __FILE__),
    File.expand_path('../../definitions/gems', __FILE__)
  ]

  def initialize
    @registered       = {}
    @load_path        = DEFAULT_LOAD_PATH.dup
    @loaded_constants = Set.new
  end

  ##
  # Registers a new definition with the given name.
  #
  # @param [String] name The name of the constant.
  # @yieldparam [RubyLint::Definition::RubyObject] defs
  #
  def register(name, &block)
    registered[name] = block
  end

  ##
  # Gets the constant with the given name.
  #
  # @param [String] constant
  # @raise [ArgumentError] Raised if the given constant doesn't exist.
  #
  def get(constant)
    found = registered[constant]

    if found
      return found
    else
      raise ArgumentError, "The constant #{constant} does not exist"
    end
  end

  ##
  # Returns `true` if the given constant has been registered.
  #
  # @param [String] constant
  # @return [TrueClass|FalseClass]
  #
  def include?(constant)
    return registered.key?(constant) || loaded_constants.include?(constant)
  end

  ##
  # Applies the definitions of a given name to the given
  # {RubyLint::Definition::RubyObject} instance.
  #
  # @param [String] constant
  # @param [RubyLint::Definition::RubyObject] definitions
  #
  def apply(constant, definitions)
    unless definitions.defines?(:const, constant)
      get(constant).call(definitions)
    end
  end

  ##
  # Tries to find a definition in the current load path and loads it if
  # found.
  #
  # @param [String] constant The name of the top level constant.
  #
  def load(constant)
    return if include?(constant)

    filename = file_for_constant(constant)

    load_path.each do |dir|
      filepath = File.join(dir, filename)

      if File.file?(filepath)
        require(filepath)

        # Only update the path if we actually found the right constant
        # file.
        if registered.key?(constant)
          loaded_constants << constant
        end

        break
      end
    end
  end

  private

  ##
  # @param [String] constant
  # @return [String]
  #
  def file_for_constant(constant)
    return constant.snake_case + '.rb'
  end
end

#loaded_constantsSet (readonly)

Returns Set containing the constants loaded from the load path.

Returns:

  • (Set)

    Set containing the constants loaded from the load path.



19
20
21
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby-lint/definition/registry.rb', line 19

class Registry
  attr_reader :load_path, :loaded_constants, :registered

  ##
  # The default load path to use.
  #
  # @return [Array]
  #
  DEFAULT_LOAD_PATH = [
    File.expand_path('../../definitions/core', __FILE__),
    File.expand_path('../../definitions/rails', __FILE__),
    File.expand_path('../../definitions/gems', __FILE__)
  ]

  def initialize
    @registered       = {}
    @load_path        = DEFAULT_LOAD_PATH.dup
    @loaded_constants = Set.new
  end

  ##
  # Registers a new definition with the given name.
  #
  # @param [String] name The name of the constant.
  # @yieldparam [RubyLint::Definition::RubyObject] defs
  #
  def register(name, &block)
    registered[name] = block
  end

  ##
  # Gets the constant with the given name.
  #
  # @param [String] constant
  # @raise [ArgumentError] Raised if the given constant doesn't exist.
  #
  def get(constant)
    found = registered[constant]

    if found
      return found
    else
      raise ArgumentError, "The constant #{constant} does not exist"
    end
  end

  ##
  # Returns `true` if the given constant has been registered.
  #
  # @param [String] constant
  # @return [TrueClass|FalseClass]
  #
  def include?(constant)
    return registered.key?(constant) || loaded_constants.include?(constant)
  end

  ##
  # Applies the definitions of a given name to the given
  # {RubyLint::Definition::RubyObject} instance.
  #
  # @param [String] constant
  # @param [RubyLint::Definition::RubyObject] definitions
  #
  def apply(constant, definitions)
    unless definitions.defines?(:const, constant)
      get(constant).call(definitions)
    end
  end

  ##
  # Tries to find a definition in the current load path and loads it if
  # found.
  #
  # @param [String] constant The name of the top level constant.
  #
  def load(constant)
    return if include?(constant)

    filename = file_for_constant(constant)

    load_path.each do |dir|
      filepath = File.join(dir, filename)

      if File.file?(filepath)
        require(filepath)

        # Only update the path if we actually found the right constant
        # file.
        if registered.key?(constant)
          loaded_constants << constant
        end

        break
      end
    end
  end

  private

  ##
  # @param [String] constant
  # @return [String]
  #
  def file_for_constant(constant)
    return constant.snake_case + '.rb'
  end
end

#registeredHash (readonly)

Returns the registered definitions as a Hash. The keys are set to the constant names, the values to Proc instances that, when evaluated, create the corresponding definitions.

Returns:

  • (Hash)

    Returns the registered definitions as a Hash. The keys are set to the constant names, the values to Proc instances that, when evaluated, create the corresponding definitions.



19
20
21
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby-lint/definition/registry.rb', line 19

class Registry
  attr_reader :load_path, :loaded_constants, :registered

  ##
  # The default load path to use.
  #
  # @return [Array]
  #
  DEFAULT_LOAD_PATH = [
    File.expand_path('../../definitions/core', __FILE__),
    File.expand_path('../../definitions/rails', __FILE__),
    File.expand_path('../../definitions/gems', __FILE__)
  ]

  def initialize
    @registered       = {}
    @load_path        = DEFAULT_LOAD_PATH.dup
    @loaded_constants = Set.new
  end

  ##
  # Registers a new definition with the given name.
  #
  # @param [String] name The name of the constant.
  # @yieldparam [RubyLint::Definition::RubyObject] defs
  #
  def register(name, &block)
    registered[name] = block
  end

  ##
  # Gets the constant with the given name.
  #
  # @param [String] constant
  # @raise [ArgumentError] Raised if the given constant doesn't exist.
  #
  def get(constant)
    found = registered[constant]

    if found
      return found
    else
      raise ArgumentError, "The constant #{constant} does not exist"
    end
  end

  ##
  # Returns `true` if the given constant has been registered.
  #
  # @param [String] constant
  # @return [TrueClass|FalseClass]
  #
  def include?(constant)
    return registered.key?(constant) || loaded_constants.include?(constant)
  end

  ##
  # Applies the definitions of a given name to the given
  # {RubyLint::Definition::RubyObject} instance.
  #
  # @param [String] constant
  # @param [RubyLint::Definition::RubyObject] definitions
  #
  def apply(constant, definitions)
    unless definitions.defines?(:const, constant)
      get(constant).call(definitions)
    end
  end

  ##
  # Tries to find a definition in the current load path and loads it if
  # found.
  #
  # @param [String] constant The name of the top level constant.
  #
  def load(constant)
    return if include?(constant)

    filename = file_for_constant(constant)

    load_path.each do |dir|
      filepath = File.join(dir, filename)

      if File.file?(filepath)
        require(filepath)

        # Only update the path if we actually found the right constant
        # file.
        if registered.key?(constant)
          loaded_constants << constant
        end

        break
      end
    end
  end

  private

  ##
  # @param [String] constant
  # @return [String]
  #
  def file_for_constant(constant)
    return constant.snake_case + '.rb'
  end
end

Instance Method Details

#apply(constant, definitions) ⇒ Object

Applies the definitions of a given name to the given RubyLint::Definition::RubyObject instance.

Parameters:



82
83
84
85
86
# File 'lib/ruby-lint/definition/registry.rb', line 82

def apply(constant, definitions)
  unless definitions.defines?(:const, constant)
    get(constant).call(definitions)
  end
end

#file_for_constant(constant) ⇒ String (private)

Parameters:

Returns:



122
123
124
# File 'lib/ruby-lint/definition/registry.rb', line 122

def file_for_constant(constant)
  return constant.snake_case + '.rb'
end

#get(constant) ⇒ Object

Gets the constant with the given name.

Parameters:

Raises:

  • (ArgumentError)

    Raised if the given constant doesn’t exist.



55
56
57
58
59
60
61
62
63
# File 'lib/ruby-lint/definition/registry.rb', line 55

def get(constant)
  found = registered[constant]

  if found
    return found
  else
    raise ArgumentError, "The constant #{constant} does not exist"
  end
end

#include?(constant) ⇒ TrueClass|FalseClass

Returns true if the given constant has been registered.

Parameters:

Returns:

  • (TrueClass|FalseClass)


71
72
73
# File 'lib/ruby-lint/definition/registry.rb', line 71

def include?(constant)
  return registered.key?(constant) || loaded_constants.include?(constant)
end

#load(constant) ⇒ Object

Tries to find a definition in the current load path and loads it if found.

Parameters:

  • constant (String)

    The name of the top level constant.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ruby-lint/definition/registry.rb', line 94

def load(constant)
  return if include?(constant)

  filename = file_for_constant(constant)

  load_path.each do |dir|
    filepath = File.join(dir, filename)

    if File.file?(filepath)
      require(filepath)

      # Only update the path if we actually found the right constant
      # file.
      if registered.key?(constant)
        loaded_constants << constant
      end

      break
    end
  end
end

#register(name) {|defs| ... } ⇒ Object

Registers a new definition with the given name.

Parameters:

  • name (String)

    The name of the constant.

Yield Parameters:



45
46
47
# File 'lib/ruby-lint/definition/registry.rb', line 45

def register(name, &block)
  registered[name] = block
end