Class: Ssource::Source::Klass

Inherits:
Element
  • Object
show all
Defined in:
lib/ssource/source/klass.rb

Direct Known Subclasses

Extension

Instance Attribute Summary collapse

Attributes inherited from Element

#accessibility, #body_length, #body_offset, #elements, #kind, #length, #name, #name_length, #name_offset, #offset

Instance Method Summary collapse

Methods inherited from Element

#display_name

Constructor Details

#initialize(json) ⇒ Klass

Returns a new instance of Klass.



12
13
14
15
16
17
# File 'lib/ssource/source/klass.rb', line 12

def initialize(json)
  super
  inherited_types = json.fetch('inheritedtypes', []).map { |hash| hash['key.name'] }
  @superklass = inherited_types.first
  @protocols = inherited_types[1..-1] || []
end

Instance Attribute Details

#protocolsObject (readonly)

Returns the value of attribute protocols.



10
11
12
# File 'lib/ssource/source/klass.rb', line 10

def protocols
  @protocols
end

#superklassObject (readonly)

Returns the value of attribute superklass.



9
10
11
# File 'lib/ssource/source/klass.rb', line 9

def superklass
  @superklass
end

Instance Method Details

#class_methodsObject



31
32
33
# File 'lib/ssource/source/klass.rb', line 31

def class_methods
  methods.select(&:class?)
end

#elements_variablesObject



54
55
56
# File 'lib/ssource/source/klass.rb', line 54

def elements_variables
  %i[variables instance_methods static_methods class_methods]
end

#instance_methodsObject



23
24
25
# File 'lib/ssource/source/klass.rb', line 23

def instance_methods
  methods.select(&:instance?)
end

#methodsObject



19
20
21
# File 'lib/ssource/source/klass.rb', line 19

def methods
  elements.select { |element| element.is_a? Method }
end

#pretty_printObject



46
47
48
49
50
51
52
# File 'lib/ssource/source/klass.rb', line 46

def pretty_print
  result = elements_variables.each_with_object({}) do |method, hash|
    collections = send(method).map(&:pretty_print)
    hash[method.to_s.capitalize] = collections unless collections.empty?
  end
  { display_name => result }
end

#static_methodsObject



27
28
29
# File 'lib/ssource/source/klass.rb', line 27

def static_methods
  methods.select(&:static?)
end

#to_hashObject



39
40
41
42
43
44
# File 'lib/ssource/source/klass.rb', line 39

def to_hash
  elements_variables.each_with_object(super) do |method, hash|
    collections = send(method).map(&:to_hash)
    hash[instance_variable.to_s.capitalize] = collections unless collections.empty?
  end
end

#variablesObject



35
36
37
# File 'lib/ssource/source/klass.rb', line 35

def variables
  elements.select { |element| element.is_a? Variable }
end