Class: Jazzy::SourceDeclaration::AccessControlLevel
- Inherits:
-
Object
- Object
- Jazzy::SourceDeclaration::AccessControlLevel
- Includes:
- Comparable
- Defined in:
- lib/jazzy/source_declaration/access_control_level.rb
Constant Summary collapse
- LEVELS =
Order matters
%i[private fileprivate internal package public open].freeze
- LEVELS_INDEX =
LEVELS.to_h { |i| [i, LEVELS.index(i)] }.freeze
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the value of attribute level.
Class Method Summary collapse
-
.from_accessibility(accessibility) ⇒ Object
From a SourceKit accessibility string.
-
.from_doc(doc) ⇒ Object
From a SourceKit declaration hash.
-
.from_doc_explicit_declaration(doc) ⇒ Object
From a Swift declaration.
-
.from_documentation_attribute(doc) ⇒ Object
From a @_documentation(visibility:) attribute.
-
.from_human_string(string) ⇒ Object
From a config instruction.
-
.implicit_deinit?(doc) ⇒ Boolean
Workaround ‘deinit` being always technically public.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Comparing access levels.
- #excluded_levels ⇒ Object
- #included_levels ⇒ Object
-
#initialize(level) ⇒ AccessControlLevel
constructor
A new instance of AccessControlLevel.
Constructor Details
#initialize(level) ⇒ AccessControlLevel
Returns a new instance of AccessControlLevel.
15 16 17 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 15 def initialize(level) @level = level end |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the value of attribute level.
13 14 15 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 13 def level @level end |
Class Method Details
.from_accessibility(accessibility) ⇒ Object
From a SourceKit accessibility string
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 20 def self.from_accessibility(accessibility) return nil if accessibility.nil? if accessibility =~ /^source\.lang\.swift\.accessibility\.(.*)$/ && (matched = Regexp.last_match(1).to_sym) && !LEVELS_INDEX[matched].nil? return new(matched) end raise "cannot initialize AccessControlLevel with '#{accessibility}'" end |
.from_doc(doc) ⇒ Object
From a SourceKit declaration hash
33 34 35 36 37 38 39 40 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 33 def self.from_doc(doc) return AccessControlLevel.internal if implicit_deinit?(doc) from_documentation_attribute(doc) || from_accessibility(doc['key.accessibility']) || from_doc_explicit_declaration(doc) || AccessControlLevel.internal # fallback on internal ACL end |
.from_doc_explicit_declaration(doc) ⇒ Object
From a Swift declaration
49 50 51 52 53 54 55 56 57 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 49 def self.from_doc_explicit_declaration(doc) declaration = doc['key.parsed_declaration'] LEVELS.each do |level| if declaration =~ /\b#{level}\b/ return send(level) end end nil end |
.from_documentation_attribute(doc) ⇒ Object
From a @_documentation(visibility:) attribute
70 71 72 73 74 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 70 def self.from_documentation_attribute(doc) if doc['key.annotated_decl'] =~ /@_documentation\(\s*visibility\s*:\s*(\w+)/ from_human_string(Regexp.last_match[1]) end end |
.from_human_string(string) ⇒ Object
From a config instruction
60 61 62 63 64 65 66 67 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 60 def self.from_human_string(string) normalized = string.to_s.downcase.to_sym if LEVELS_INDEX[normalized].nil? raise "cannot initialize AccessControlLevel with '#{string}'" end send(normalized) end |
.implicit_deinit?(doc) ⇒ Boolean
Workaround ‘deinit` being always technically public
43 44 45 46 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 43 def self.implicit_deinit?(doc) doc['key.name'] == 'deinit' && from_doc_explicit_declaration(doc).nil? end |
Instance Method Details
#<=>(other) ⇒ Object
Comparing access levels
86 87 88 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 86 def <=>(other) LEVELS_INDEX[level] <=> LEVELS_INDEX[other.level] end |
#excluded_levels ⇒ Object
94 95 96 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 94 def excluded_levels LEVELS_INDEX.select { |_, v| v < LEVELS_INDEX[level] }.keys end |
#included_levels ⇒ Object
90 91 92 |
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 90 def included_levels LEVELS_INDEX.select { |_, v| v >= LEVELS_INDEX[level] }.keys end |