Class: StaticSourceLoc::ModuleSource

Inherits:
Source
  • Object
show all
Defined in:
lib/static_source_loc.rb

Instance Attribute Summary collapse

Attributes inherited from Source

#name, #parent, #source_locs

Instance Method Summary collapse

Methods inherited from Source

#inspect, #new_loc, #source_loc, #to_s

Constructor Details

#initialize(name, parent, singleton = false) ⇒ ModuleSource

Returns a new instance of ModuleSource.



43
44
45
46
47
# File 'lib/static_source_loc.rb', line 43

def initialize(name, parent, singleton=false)
  super name, parent
  @singleton = singleton
  @submodules, @methods = {}, {}
end

Instance Attribute Details

#methodsObject (readonly)

Returns the value of attribute methods.



41
42
43
# File 'lib/static_source_loc.rb', line 41

def methods
  @methods
end

#submodulesObject (readonly)

Returns the value of attribute submodules.



41
42
43
# File 'lib/static_source_loc.rb', line 41

def submodules
  @submodules
end

Instance Method Details

#new_method(name) ⇒ Object



57
58
59
# File 'lib/static_source_loc.rb', line 57

def new_method(name)
  @methods[name] ||= MethodSource.new name, self
end

#new_submodule(name) ⇒ Object



53
54
55
# File 'lib/static_source_loc.rb', line 53

def new_submodule(name)
  @submodules[name] ||= ModuleSource.new name, self
end

#process_code(sexpr) ⇒ Object



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
# File 'lib/static_source_loc.rb', line 85

def process_code(sexpr)
  case sexpr.node_type
  when :block
    sexpr.values.each &method(:process_code)
  when :class, :module
    new_submodule(sexpr[1]).
      new_loc(sexpr.file, sexpr.line).
      process_code(s(:block).concat sexpr.drop(3))
  when :sclass
    if sexpr[1] == s(:self)
      singleton_class.
        new_loc(sexpr.file, sexpr.line).
        process_code(s(:block).concat sexpr.drop(2))
    end
  when :defn
    new_method(sexpr[1]).
      new_loc(sexpr.file, sexpr.line)
  when :defs
    if sexpr[1] == s(:self)
      singleton_class.
        new_method(sexpr[2]).
        new_loc(sexpr.file, sexpr.line)
    end
  end
  self
end

#qualnameObject



66
67
68
69
70
71
72
# File 'lib/static_source_loc.rb', line 66

def qualname
  unless singleton?
    "#{parent.qualname if parent}::#{name}"
  else
    "#{parent.qualname if parent}.#{name}"
  end
end

#singleton?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/static_source_loc.rb', line 49

def singleton?
  @singleton
end

#singleton_classObject



61
62
63
64
# File 'lib/static_source_loc.rb', line 61

def singleton_class
  @singleton_class ||=
    ModuleSource.new :singleton_class, self, true
end

#to_hashObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/static_source_loc.rb', line 74

def to_hash
  children = []
  children.concat(submodules.values)
  children.concat(methods.values)
  children << @singleton_class if @singleton_class
  children.each_with_object({qualname => self}) do |child, hash|
    hash[child.qualname] = child
    hash.merge! child.to_hash if child.respond_to? :to_hash
  end
end