Class: Coconductor::CodeOfConduct

Inherits:
Object
  • Object
show all
Includes:
Licensee::ContentHelper
Defined in:
lib/coconductor/code_of_conduct.rb

Constant Summary collapse

KEY_REGEX =
%r{
  (?<family>#{Regexp.union(CodeOfConduct.families)})
  /version
  /(?<major>\d)/(?<minor>\d)(/(?<patch>\d))?
  /#{Coconductor::ProjectFiles::CodeOfConductFile::FILENAME_REGEX}
}ix

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ CodeOfConduct

Returns a new instance of CodeOfConduct.



72
73
74
# File 'lib/coconductor/code_of_conduct.rb', line 72

def initialize(key)
  @key = key
end

Instance Attribute Details

#content ⇒ Object Also known as: body



101
102
103
104
105
106
107
108
# File 'lib/coconductor/code_of_conduct.rb', line 101

def content
  # See https://github.com/stumpsyn/policies/pull/21
  @content ||= if citizen_code_of_conduct?
                 parts.last.gsub(' COMMUNITY_NAME ', ' [COMMUNITY_NAME] ')
               else
                 parts.last
               end
end

#key ⇒ Object (readonly)

Returns the value of attribute key.



68
69
70
# File 'lib/coconductor/code_of_conduct.rb', line 68

def key
  @key
end

Class Method Details

.all ⇒ Object



7
8
9
# File 'lib/coconductor/code_of_conduct.rb', line 7

def all
  @all ||= keys.map { |key| new(key) }
end

.families ⇒ Object



44
45
46
47
48
# File 'lib/coconductor/code_of_conduct.rb', line 44

def families
  @families ||= Dir["#{vendor_dir}/*"].map do |dir|
    File.basename(dir)
  end
end

.find(key_or_family) ⇒ Object Also known as: [], find_by_key

Returns the code of conduct specified by the key with version, or the latest in the family if only the family is specified



17
18
19
20
# File 'lib/coconductor/code_of_conduct.rb', line 17

def find(key_or_family)
  match = all.find { |coc| coc.key == key_or_family }
  match || latest_in_family(key_or_family)
end

.keys ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/coconductor/code_of_conduct.rb', line 28

def keys
  @keys ||= vendored_codes_of_conduct.map do |path|
    path = Pathname.new(path)
    key = path.relative_path_from(vendor_dir)
    matches = KEY_REGEX.match(key.to_path)
    matches.to_a.compact[1..-1].insert(1, 'version').join('/') if matches
  end
end

.latest ⇒ Object



11
12
13
# File 'lib/coconductor/code_of_conduct.rb', line 11

def latest
  @latest ||= families.map { |family| find(family) }
end

.latest_in_family(family, language: nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/coconductor/code_of_conduct.rb', line 37

def latest_in_family(family, language: nil)
  cocs = all.select do |coc|
    coc.language == language && coc.family == family
  end
  cocs.max_by(&:version)
end

.vendor_dir ⇒ Object



24
25
26
# File 'lib/coconductor/code_of_conduct.rb', line 24

def vendor_dir
  @vendor_dir ||= Pathname.new File.expand_path '../../vendor', __dir__
end

Instance Method Details

#citizen_code_of_conduct? ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/coconductor/code_of_conduct.rb', line 123

def citizen_code_of_conduct?
  family == 'citizen-code-of-conduct'
end

#contributor_covenant? ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/coconductor/code_of_conduct.rb', line 119

def contributor_covenant?
  family == 'contributor-covenant'
end

#family ⇒ Object



115
116
117
# File 'lib/coconductor/code_of_conduct.rb', line 115

def family
  @family ||= key.split('/').first
end

#fields ⇒ Object



131
132
133
# File 'lib/coconductor/code_of_conduct.rb', line 131

def fields
  @fields ||= Field.from_code_of_conduct(self)
end

#inspect ⇒ Object



111
112
113
# File 'lib/coconductor/code_of_conduct.rb', line 111

def inspect
  "#<Licensee::CodeOfConduct key=#{key}>"
end

#language ⇒ Object



80
81
82
83
84
85
# File 'lib/coconductor/code_of_conduct.rb', line 80

def language
  @language ||= begin
    parts = key.split('/')
    parts.last if parts.last =~ /^[a-z-]{2,5}$/
  end
end

#name ⇒ Object



87
88
89
90
91
92
93
# File 'lib/coconductor/code_of_conduct.rb', line 87

def name
  return @name if defined? @name
  @name = name_without_version.dup
  @name << " (#{language.upcase})" if language
  @name << " v#{version}" if version
  @name
end

#name_without_version ⇒ Object



95
96
97
98
99
# File 'lib/coconductor/code_of_conduct.rb', line 95

def name_without_version
  @name_without_version ||= begin
    key.split('/').first.split('-').map(&:capitalize).join(' ')
  end
end

#no_code_of_conduct? ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/coconductor/code_of_conduct.rb', line 127

def no_code_of_conduct?
  family == 'no-code-of-conduct'
end

#version ⇒ Object



76
77
78
# File 'lib/coconductor/code_of_conduct.rb', line 76

def version
  toml['version']
end