Class: Ruby2JS::Namespace
- Inherits:
-
Object
- Object
- Ruby2JS::Namespace
- Defined in:
- lib/ruby2js/namespace.rb
Instance Method Summary collapse
-
#active ⇒ Object
return the active scope as a flat array of symbols.
-
#defineProps(props, namespace = active) ⇒ Object
add new props (and methods) to the current scope.
-
#enter(name) ⇒ Object
enter a new scope, which may be a nested subscope.
-
#find(name) ⇒ Object
find a named scope which may be relative to any point in the ancestry of the current scope.
-
#getOwnProps(name = nil) ⇒ Object
return the set of known properties (and methods) for either the current scope or a named subscope.
-
#initialize ⇒ Namespace
constructor
A new instance of Namespace.
-
#leave ⇒ Object
leave a given scope.
-
#resolve(token, result = []) ⇒ Object
convert an AST name which is represented as a set of nested s(:const, # …) into an array of symbols that represent the relative path.
Constructor Details
#initialize ⇒ Namespace
Returns a new instance of Namespace.
16 17 18 19 |
# File 'lib/ruby2js/namespace.rb', line 16 def initialize @active = [] # current scope @seen = {} # history of all definitions seen previously end |
Instance Method Details
#active ⇒ Object
return the active scope as a flat array of symbols
31 32 33 |
# File 'lib/ruby2js/namespace.rb', line 31 def active @active.flatten.compact end |
#defineProps(props, namespace = active) ⇒ Object
add new props (and methods) to the current scope.
52 53 54 55 |
# File 'lib/ruby2js/namespace.rb', line 52 def defineProps(props, namespace=active) @seen[namespace] ||= {} @seen[namespace].merge! props || {} end |
#enter(name) ⇒ Object
enter a new scope, which may be a nested subscope. Mark the new scope as seen, and return any previous definition that may have been seen before.
38 39 40 41 42 43 |
# File 'lib/ruby2js/namespace.rb', line 38 def enter(name) @active.push resolve(name) previous = @seen[active] @seen[active] ||= {} previous end |
#find(name) ⇒ Object
find a named scope which may be relative to any point in the ancestry of the current scope. Return the properties for that scope.
59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby2js/namespace.rb', line 59 def find(name) name = resolve(name) prefix = active while prefix.pop result = @seen[prefix + name] return result if result end {} end |
#getOwnProps(name = nil) ⇒ Object
return the set of known properties (and methods) for either the current scope or a named subscope.
47 48 49 |
# File 'lib/ruby2js/namespace.rb', line 47 def getOwnProps(name = nil) @seen[active + resolve(name)]&.dup || {} end |
#leave ⇒ Object
leave a given scope. Note that the scope may be compound (e.g., M::N), and if so, it will pop the entire resolved name.
71 72 73 |
# File 'lib/ruby2js/namespace.rb', line 71 def leave() @active.pop end |
#resolve(token, result = []) ⇒ Object
convert an AST name which is represented as a set of nested s(:const, # …) into an array of symbols that represent the relative path.
24 25 26 27 28 |
# File 'lib/ruby2js/namespace.rb', line 24 def resolve(token, result = []) return [] unless token&.type == :const resolve(token.children.first, result) result.push(token.children.last) end |