Class: ApiDiff::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/api_diff/type.rb

Direct Known Subclasses

Class, Enum, Interface, Struct

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, fully_qualified_name) ⇒ Type

Returns a new instance of Type.



10
11
12
13
14
15
16
# File 'lib/api_diff/type.rb', line 10

def initialize(name, fully_qualified_name)
  @name = name
  @fully_qualified_name = fully_qualified_name
  @parents = []
  @functions = []
  @properties = []
end

Instance Attribute Details

#fully_qualified_nameObject (readonly)

Returns the value of attribute fully_qualified_name.



7
8
9
# File 'lib/api_diff/type.rb', line 7

def fully_qualified_name
  @fully_qualified_name
end

#functionsObject

Returns the value of attribute functions.



8
9
10
# File 'lib/api_diff/type.rb', line 8

def functions
  @functions
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/api_diff/type.rb', line 7

def name
  @name
end

#parentsObject

Returns the value of attribute parents.



8
9
10
# File 'lib/api_diff/type.rb', line 8

def parents
  @parents
end

#propertiesObject

Returns the value of attribute properties.



8
9
10
# File 'lib/api_diff/type.rb', line 8

def properties
  @properties
end

Class Method Details

.type_nameObject



3
4
5
# File 'lib/api_diff/type.rb', line 3

def self.type_name
  name.split('::').last.downcase
end

Instance Method Details

#<=>(other) ⇒ Object



40
41
42
# File 'lib/api_diff/type.rb', line 40

def <=>(other)
  name <=> other.name
end

#declaration(fully_qualified_name: false) ⇒ Object



22
23
24
25
26
27
# File 'lib/api_diff/type.rb', line 22

def declaration(fully_qualified_name: false)
  name = fully_qualified_name ? self.fully_qualified_name : self.name
  result = "#{self.class.type_name} #{name}"
  result += " : #{parents.join(", ")}" if has_parents?
  result
end

#has_parents?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/api_diff/type.rb', line 29

def has_parents?
  not @parents.empty?
end

#packageObject



18
19
20
# File 'lib/api_diff/type.rb', line 18

def package
  fully_qualified_name.split(".")[0..-2].join(".")
end

#sectionsObject



33
34
35
36
37
38
# File 'lib/api_diff/type.rb', line 33

def sections
  [
    properties.sort,
    functions.sort
  ]
end

#to_s(fully_qualified_name: true) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/api_diff/type.rb', line 44

def to_s(fully_qualified_name: true)
  body = sections.map { |s| s.empty? ? nil : s }.compact # remove empty sections
  body.map! { |s| s.map { |entry| "    #{entry}" } }  # convert every entry in every section into a string and indent it
  body.map! { |s| s.join("\n") }  # join all entries into a long string
  [
    "#{declaration(fully_qualified_name: fully_qualified_name)} {",
    body.join("\n\n"),
    "}"
  ].join("\n")
end