Class: Jazzy::SourceDeclaration::AccessControlLevel

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/jazzy/source_declaration/access_control_level.rb

Constant Summary collapse

ACCESSIBILITY_PRIVATE =
'source.lang.swift.accessibility.private'.freeze
ACCESSIBILITY_FILEPRIVATE =
'source.lang.swift.accessibility.fileprivate'.freeze
ACCESSIBILITY_INTERNAL =
'source.lang.swift.accessibility.internal'.freeze
ACCESSIBILITY_PUBLIC =
'source.lang.swift.accessibility.public'.freeze
ACCESSIBILITY_OPEN =
'source.lang.swift.accessibility.open'.freeze
LEVELS =
{
  private: 0,
  fileprivate: 1,
  internal: 2,
  public: 3,
  open: 4,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accessibility) ⇒ AccessControlLevel

Returns a new instance of AccessControlLevel.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 15

def initialize(accessibility)
  @level = case accessibility
           when ACCESSIBILITY_PRIVATE then :private
           when ACCESSIBILITY_FILEPRIVATE then :fileprivate
           when ACCESSIBILITY_INTERNAL then :internal
           when ACCESSIBILITY_PUBLIC then :public
           when ACCESSIBILITY_OPEN then :open
           else
             raise 'cannot initialize AccessControlLevel with ' \
               "'#{accessibility}'"
           end
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 6

def level
  @level
end

Class Method Details

.fileprivateObject



72
73
74
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 72

def self.fileprivate
  new(ACCESSIBILITY_FILEPRIVATE)
end

.from_doc(doc) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 28

def self.from_doc(doc)
  return AccessControlLevel.internal if implicit_deinit?(doc)

  accessibility = doc['key.accessibility']
  if accessibility
    acl = new(accessibility)
    if acl
      return acl
    end
  end
  acl = from_doc_explicit_declaration(doc)
  acl || AccessControlLevel.internal # fallback on internal ACL
end

.from_doc_explicit_declaration(doc) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 47

def self.from_doc_explicit_declaration(doc)
  case doc['key.parsed_declaration']
  when /private\ / then private
  when /fileprivate\ / then fileprivate
  when /public\ / then public
  when /open\ / then open
  when /internal\ / then internal
  end
end

.from_human_string(string) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 57

def self.from_human_string(string)
  case string.to_s.downcase
  when 'private' then private
  when 'fileprivate' then fileprivate
  when 'internal' then internal
  when 'public' then public
  when 'open' then open
  else raise "cannot initialize AccessControlLevel with '#{string}'"
  end
end

.implicit_deinit?(doc) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 42

def self.implicit_deinit?(doc)
  doc['key.name'] == 'deinit' &&
    from_doc_explicit_declaration(doc).nil?
end

.internalObject



76
77
78
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 76

def self.internal
  new(ACCESSIBILITY_INTERNAL)
end

.openObject



84
85
86
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 84

def self.open
  new(ACCESSIBILITY_OPEN)
end

.privateObject



68
69
70
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 68

def self.private
  new(ACCESSIBILITY_PRIVATE)
end

.publicObject



80
81
82
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 80

def self.public
  new(ACCESSIBILITY_PUBLIC)
end

Instance Method Details

#<=>(other) ⇒ Object



96
97
98
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 96

def <=>(other)
  LEVELS[level] <=> LEVELS[other.level]
end

#excluded_levelsObject



104
105
106
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 104

def excluded_levels
  LEVELS.select { |_, v| v < LEVELS[level] }.keys
end

#included_levelsObject



100
101
102
# File 'lib/jazzy/source_declaration/access_control_level.rb', line 100

def included_levels
  LEVELS.select { |_, v| v >= LEVELS[level] }.keys
end