Class: Jazzy::SymbolGraph::Constraint

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/jazzy/symbol_graph/constraint.rb

Overview

Constraint is a tidied-up JSON object, used by both Symbol and Relationship, and key to reconstructing extensions.

Constant Summary collapse

KIND_MAP =
{
  'conformance' => ':',
  'superclass' => ':',
  'sameType' => '==',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#kindObject

Returns the value of attribute kind.



8
9
10
# File 'lib/jazzy/symbol_graph/constraint.rb', line 8

def kind
  @kind
end

#lhsObject

Returns the value of attribute lhs.



9
10
11
# File 'lib/jazzy/symbol_graph/constraint.rb', line 9

def lhs
  @lhs
end

#rhsObject

Returns the value of attribute rhs.



10
11
12
# File 'lib/jazzy/symbol_graph/constraint.rb', line 10

def rhs
  @rhs
end

Class Method Details

.new_declaration(decl) ⇒ Object

Init from a Swift declaration fragment eg. ‘A : B’



39
40
41
42
43
44
# File 'lib/jazzy/symbol_graph/constraint.rb', line 39

def self.new_declaration(decl)
  decl =~ /^(.*?)\s*([:<=]+)\s*(.*)$/
  new(Regexp.last_match[2],
      Regexp.last_match[1],
      Regexp.last_match[3])
end

.new_hash(hash) ⇒ Object

Init from a JSON hash



29
30
31
32
33
34
35
36
# File 'lib/jazzy/symbol_graph/constraint.rb', line 29

def self.new_hash(hash)
  kind = KIND_MAP[hash[:kind]]
  raise "Unknown constraint kind '#{kind}'" unless kind

  lhs = hash[:lhs].sub(/^Self\./, '')
  rhs = hash[:rhs].sub(/^Self\./, '')
  new(kind, lhs, rhs)
end

.new_list(hash_list) ⇒ Object



55
56
57
# File 'lib/jazzy/symbol_graph/constraint.rb', line 55

def self.new_list(hash_list)
  hash_list.map { |h| Constraint.new_hash(h) }.sort.uniq
end

.new_list_for_symbol(hash_list, path_components) ⇒ Object

Swift protocols and reqs have an implementation/hidden conformance to their own protocol: we don’t want to think about this in docs.



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jazzy/symbol_graph/constraint.rb', line 61

def self.new_list_for_symbol(hash_list, path_components)
  hash_list.map do |hash|
    if hash[:lhs] == 'Self' &&
       hash[:kind] == 'conformance' &&
       path_components.include?(hash[:rhs])
      next nil
    end

    Constraint.new_hash(hash)
  end.compact
end

.new_list_from_declaration(decl) ⇒ Object

Workaround Swift 5.3 bug with missing constraint rels



74
75
76
# File 'lib/jazzy/symbol_graph/constraint.rb', line 74

def self.new_list_from_declaration(decl)
  decl.split(/\s*,\s*/).map { |cons| Constraint.new_declaration(cons) }
end

Instance Method Details

#<=>(other) ⇒ Object



81
82
83
# File 'lib/jazzy/symbol_graph/constraint.rb', line 81

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

#hashObject



87
88
89
# File 'lib/jazzy/symbol_graph/constraint.rb', line 87

def hash
  to_swift.hash
end

#to_swiftObject



46
47
48
# File 'lib/jazzy/symbol_graph/constraint.rb', line 46

def to_swift
  "#{lhs} #{kind} #{rhs}"
end

#type_namesObject

The first component of types in the constraint



51
52
53
# File 'lib/jazzy/symbol_graph/constraint.rb', line 51

def type_names
  Set.new([lhs, rhs].map { |n| n.sub(/\..*$/, '') })
end