Class: Tumugi::Registry
- Inherits:
-
Object
- Object
- Tumugi::Registry
- Defined in:
- lib/tumugi/registry.rb
Constant Summary collapse
- DEFAULT_PLUGIN_PATH =
File.('plugin', __FILE__)
Instance Attribute Summary collapse
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
-
#initialize(kind, search_prefix) ⇒ Registry
constructor
A new instance of Registry.
- #lookup(type) ⇒ Object
- #register(type, value) ⇒ Object
- #search(type) ⇒ Object
Constructor Details
#initialize(kind, search_prefix) ⇒ Registry
5 6 7 8 9 10 |
# File 'lib/tumugi/registry.rb', line 5 def initialize(kind, search_prefix) @kind = kind @search_prefix = search_prefix @map = {} @paths = [DEFAULT_PLUGIN_PATH] end |
Instance Attribute Details
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
12 13 14 |
# File 'lib/tumugi/registry.rb', line 12 def kind @kind end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
12 13 14 |
# File 'lib/tumugi/registry.rb', line 12 def paths @paths end |
Instance Method Details
#lookup(type) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/tumugi/registry.rb', line 19 def lookup(type) t = type.to_sym return @map[t] if @map.has_key?(t) search(type) return @map[t] if @map.has_key?(t) raise "Unknown #{@kind} plugin '#{type}'" end |
#register(type, value) ⇒ Object
14 15 16 17 |
# File 'lib/tumugi/registry.rb', line 14 def register(type, value) type = type.to_sym @map[type] = value end |
#search(type) ⇒ Object
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 |
# File 'lib/tumugi/registry.rb', line 27 def search(type) path = "#{@search_prefix}#{type}" # prefer LOAD_PATH than gems [@paths, $LOAD_PATH].each do |paths| files = paths.map { |lp| lpath = File.(File.join(lp, "#{path}.rb")) File.exist?(lpath) ? lpath : nil }.compact unless files.empty? require files.sort.last return end end specs = Gem::Specification.find_all { |spec| spec.contains_requirable_file? path } # prefer newer version specs = specs.sort_by { |spec| spec.version } if spec = specs.last spec.require_paths.each do |lib| file = "#{spec.full_gem_path}/#{lib}/#{path}" return file end end end |