Class: RubyLint::Definition::Registry
- Inherits:
-
Object
- Object
- RubyLint::Definition::Registry
- 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.
[ File.('../../definitions/core', __FILE__), File.('../../definitions/rails', __FILE__), File.('../../definitions/gems', __FILE__) ]
Instance Attribute Summary collapse
-
#load_path ⇒ Array
readonly
List of directories to search in for definitions.
-
#loaded_constants ⇒ Set
readonly
Set containing the constants loaded from the load path.
-
#registered ⇒ Hash
readonly
Returns the registered definitions as a Hash.
Instance Method Summary collapse
-
#apply(constant, definitions) ⇒ Object
Applies the definitions of a given name to the given RubyObject instance.
-
#file_for_constant(constant) ⇒ String
private
-
#get(constant) ⇒ Object
Gets the constant with the given name.
-
#include?(constant) ⇒ TrueClass|FalseClass
Returns
true
if the given constant has been registered. -
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#load(constant) ⇒ Object
Tries to find a definition in the current load path and loads it if found.
-
#register(name) {|defs| ... } ⇒ Object
Registers a new definition with the given name.
Constructor Details
#initialize ⇒ Registry
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_path ⇒ Array (readonly)
Returns 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.('../../definitions/core', __FILE__), File.('../../definitions/rails', __FILE__), File.('../../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_constants ⇒ Set (readonly)
Returns 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.('../../definitions/core', __FILE__), File.('../../definitions/rails', __FILE__), File.('../../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 |
#registered ⇒ Hash (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.
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.('../../definitions/core', __FILE__), File.('../../definitions/rails', __FILE__), File.('../../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.
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)
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.
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.
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.
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.
45 46 47 |
# File 'lib/ruby-lint/definition/registry.rb', line 45 def register(name, &block) registered[name] = block end |