Class: Peruby::MRO
- Inherits:
-
Object
- Object
- Peruby::MRO
- Defined in:
- lib/peruby/runtime/mro.rb
Overview
Perl package method lookup over runtime @ISA values.
Instance Method Summary collapse
-
#initialize(stash) ⇒ MRO
constructor
A new instance of MRO.
- #isa?(package, parent) ⇒ Boolean
- #lineage(package) ⇒ Object
- #resolve(package, method, super_from: nil, autoload: true) ⇒ Object
- #set(package, mode) ⇒ Object
Constructor Details
#initialize(stash) ⇒ MRO
Returns a new instance of MRO.
6 7 8 9 10 |
# File 'lib/peruby/runtime/mro.rb', line 6 def initialize(stash) @stash = stash @cache = {} @modes = Hash.new(:dfs) end |
Instance Method Details
#isa?(package, parent) ⇒ Boolean
35 36 37 |
# File 'lib/peruby/runtime/mro.rb', line 35 def isa?(package, parent) package == parent || linearized(package).include?(parent) end |
#lineage(package) ⇒ Object
39 |
# File 'lib/peruby/runtime/mro.rb', line 39 def lineage(package) = linearized(package) |
#resolve(package, method, super_from: nil, autoload: true) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/peruby/runtime/mro.rb', line 19 def resolve(package, method, super_from: nil, autoload: true) start = super_from ? parents(super_from) : [package] order = start.flat_map { |candidate| linearized(candidate) }.uniq order << 'UNIVERSAL' unless order.include?('UNIVERSAL') found = order.filter_map { |candidate| method_entry(candidate, method) }.first return found if found return nil if method == 'DESTROY' || !autoload autoload = order.filter_map { |candidate| method_entry(candidate, 'AUTOLOAD') }.first if autoload owner, = autoload @stash.glob("#{owner}::AUTOLOAD").scalar.set("#{package}::#{method}") end autoload end |
#set(package, mode) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/peruby/runtime/mro.rb', line 12 def set(package, mode) raise PerlError, "Invalid MRO #{mode}" unless %i[dfs c3].include?(mode.to_sym) @modes[package] = mode.to_sym @cache.clear end |