Class: YARD::CodeObjects::NamespaceObject
- Inherits:
-
Base
- Object
- Base
- YARD::CodeObjects::NamespaceObject
- Defined in:
- lib/yard/code_objects/namespace_object.rb
Overview
A “namespace” is any object that can store other objects within itself. The two main Ruby objects that can act as namespaces are modules (ModuleObject) and classes (ClassObject).
Instance Attribute Summary
- - (Hash) aliases readonly A hash containing two keys, :class and :instance, each containing a hash of objects and their alias names.
- - (Hash) attributes readonly A hash containing two keys, class and instance, each containing the attribute name with a { :read, :write } hash for the read and write objects respectively.
- - (Base?) child(opts = {}) readonly Looks for a child that matches the attributes specified by opts.
- - (Array<Base>) children readonly The list of objects defined in this namespace.
- - (Hash) class_attributes readonly Only the class attributes.
- - (Array<ModuleObject>) class_mixins readonly Class mixins.
- - (Array<ConstantObject>) constants(opts = {}) readonly Returns all constants in the namespace.
- - (Array<ClassVariableObject>) cvars readonly Returns class variables defined in this namespace.
- - (Array<ConstantObject>) included_constants readonly Returns constants included from any mixins.
- - (Object) included_meths(opts = {}) readonly Returns methods included from any mixins that match the attributes specified by opts.
- - (Hash) instance_attributes readonly Only the instance attributes.
- - (Array<ModuleObject>) instance_mixins readonly Instance mixins.
- - (Array<MethodObject>) meths(opts = {}) readonly Returns all methods that match the attributes specified by opts.
- - (Array<ModuleObject>) mixins(*scopes) readonly Returns for specific scopes.
Instance Method Summary
- - (NamespaceObject) initialize(namespace, name, *args, &block) constructor Creates a new namespace object inside namespace with name.
Methods inherited from Base
#==, ===, #[], #[]=, #add_file, #dynamic?, #file, #format, #format_source, #has_tag?, #inspect, #line, #method_missing, #name, new, #parent, #parent=, #path, #relative_path, #root?, #sep, #tag, #tags, #to_s, #type
Constructor Details
- (NamespaceObject) initialize(namespace, name, *args, &block)
Creates a new namespace object inside namespace with name.
52 53 54 55 56 57 58 59 |
# File 'lib/yard/code_objects/namespace_object.rb', line 52 def initialize(namespace, name, *args, &block) @children = CodeObjectList.new(self) @class_mixins = CodeObjectList.new(self) @instance_mixins = CodeObjectList.new(self) @attributes = SymbolHash[:class => SymbolHash.new, :instance => SymbolHash.new] @aliases = {} super end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class YARD::CodeObjects::Base
Instance Attribute Details
- (Hash) aliases (readonly)
A hash containing two keys, :class and :instance, each containing a hash of objects and their alias names.
40 41 42 |
# File 'lib/yard/code_objects/namespace_object.rb', line 40 def aliases @aliases end |
- (Hash) attributes (readonly)
A hash containing two keys, class and instance, each containing the attribute name with a { :read, :write } hash for the read and write objects respectively.
35 36 37 |
# File 'lib/yard/code_objects/namespace_object.rb', line 35 def attributes @attributes end |
- (Base?) child(opts = {}) (readonly)
Looks for a child that matches the attributes specified by opts.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/yard/code_objects/namespace_object.rb', line 81 def child(opts = {}) if !opts.is_a?(Hash) children.find {|o| o.name == opts.to_sym } else opts = SymbolHash[opts] children.find do |obj| opts.each do |meth, value| break false if !(value.is_a?(Array) ? value.include?(obj[meth]) : obj[meth] == value) end end end end |
- (Array<Base>) children (readonly)
The list of objects defined in this namespace
12 13 14 |
# File 'lib/yard/code_objects/namespace_object.rb', line 12 def children @children end |
- (Hash) class_attributes (readonly)
Only the class attributes
64 65 66 |
# File 'lib/yard/code_objects/namespace_object.rb', line 64 def class_attributes attributes[:class] end |
- (Array<ModuleObject>) class_mixins (readonly)
Class mixins
44 45 46 |
# File 'lib/yard/code_objects/namespace_object.rb', line 44 def class_mixins @class_mixins end |
- (Array<ConstantObject>) constants(opts = {}) (readonly)
Returns all constants in the namespace
158 159 160 161 162 |
# File 'lib/yard/code_objects/namespace_object.rb', line 158 def constants(opts = {}) opts = SymbolHash[:included => true].update(opts) consts = children.select {|o| o.is_a? ConstantObject } consts + (opts[:included] ? included_constants : []) end |
- (Array<ClassVariableObject>) cvars (readonly)
Returns class variables defined in this namespace.
180 181 182 |
# File 'lib/yard/code_objects/namespace_object.rb', line 180 def cvars children.select {|o| o.is_a? ClassVariableObject } end |
- (Array<ConstantObject>) included_constants (readonly)
Returns constants included from any mixins
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/yard/code_objects/namespace_object.rb', line 166 def included_constants instance_mixins.reverse.inject([]) do |list, mixin| if mixin.respond_to? :constants list += mixin.constants.reject do |o| child(:name => o.name) || list.find {|o2| o2.name == o.name } end else list end end end |
- (Object) included_meths(opts = {}) (readonly)
Returns methods included from any mixins that match the attributes specified by opts. If no options are specified, returns all included methods.
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/yard/code_objects/namespace_object.rb', line 139 def included_meths(opts = {}) opts = SymbolHash[:scope => [:instance, :class]].update(opts) [opts[:scope]].flatten.map do |scope| mixins(scope).reverse.inject([]) do |list, mixin| next list if mixin.is_a?(Proxy) arr = mixin.meths(opts.merge(:scope => :instance)).reject do |o| child(:name => o.name, :scope => scope) || list.find {|o2| o2.name == o.name } end arr.map! {|o| ExtendedMethodObject.new(o) } if scope == :class list + arr end end.flatten end |
- (Hash) instance_attributes (readonly)
Only the instance attributes
71 72 73 |
# File 'lib/yard/code_objects/namespace_object.rb', line 71 def instance_attributes attributes[:instance] end |
- (Array<ModuleObject>) instance_mixins (readonly)
Instance mixins
48 49 50 |
# File 'lib/yard/code_objects/namespace_object.rb', line 48 def instance_mixins @instance_mixins end |
- (Array<MethodObject>) meths(opts = {}) (readonly)
Returns all methods that match the attributes specified by opts. If no options are provided, returns all methods.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/yard/code_objects/namespace_object.rb', line 108 def meths(opts = {}) opts = SymbolHash[ :visibility => [:public, :private, :protected], :scope => [:class, :instance], :included => true ].update(opts) opts[:visibility] = [opts[:visibility]].flatten opts[:scope] = [opts[:scope]].flatten ourmeths = children.select do |o| o.is_a?(MethodObject) && opts[:visibility].include?(o.visibility) && opts[:scope].include?(o.scope) end ourmeths + (opts[:included] ? included_meths(opts) : []) end |
- (Array<ModuleObject>) mixins(*scopes) (readonly)
Returns for specific scopes. If no scopes are provided, returns all mixins.
188 189 190 191 192 |
# File 'lib/yard/code_objects/namespace_object.rb', line 188 def mixins(*scopes) return class_mixins if scopes == [:class] return instance_mixins if scopes == [:instance] class_mixins | instance_mixins end |