Class: Jazzy::SymbolGraph::Constraint
- Inherits:
-
Object
- Object
- Jazzy::SymbolGraph::Constraint
- 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
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#lhs ⇒ Object
Returns the value of attribute lhs.
-
#rhs ⇒ Object
Returns the value of attribute rhs.
Class Method Summary collapse
-
.new_declaration(decl) ⇒ Object
Init from a Swift declaration fragment eg.
-
.new_hash(hash) ⇒ Object
Init from a JSON hash.
- .new_list(hash_list) ⇒ Object
-
.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.
-
.new_list_from_declaration(decl) ⇒ Object
Workaround Swift 5.3 bug with missing constraint rels.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #hash ⇒ Object
- #to_swift ⇒ Object
-
#type_names ⇒ Object
The first component of types in the constraint.
Instance Attribute Details
#kind ⇒ Object
Returns the value of attribute kind.
6 7 8 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 6 def kind @kind end |
#lhs ⇒ Object
Returns the value of attribute lhs.
7 8 9 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 7 def lhs @lhs end |
#rhs ⇒ Object
Returns the value of attribute rhs.
8 9 10 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 8 def rhs @rhs end |
Class Method Details
.new_declaration(decl) ⇒ Object
Init from a Swift declaration fragment eg. ‘A : B’
36 37 38 39 40 41 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 36 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
27 28 29 30 31 32 33 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 27 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
52 53 54 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 52 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.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 58 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
70 71 72 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 70 def self.new_list_from_declaration(decl) decl.split(/\s*,\s*/).map { |cons| Constraint.new_declaration(cons) } end |
Instance Method Details
#<=>(other) ⇒ Object
77 78 79 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 77 def <=>(other) to_swift <=> other.to_swift end |
#hash ⇒ Object
83 84 85 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 83 def hash to_swift.hash end |
#to_swift ⇒ Object
43 44 45 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 43 def to_swift "#{lhs} #{kind} #{rhs}" end |
#type_names ⇒ Object
The first component of types in the constraint
48 49 50 |
# File 'lib/jazzy/symbol_graph/constraint.rb', line 48 def type_names Set.new([lhs, rhs].map { |n| n.sub(/\..*$/, '') }) end |