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
  /(?<version>(?<major>\d)/(?<minor>\d)(/(?<patch>\d))?|(longer|shorter))
  /#{Coconductor::ProjectFiles::CodeOfConductFile::FILENAME_REGEX}
}ix
DEFAULT_LANGUAGE =
'en'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ CodeOfConduct

Returns a new instance of CodeOfConduct.



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

def initialize(key)
  @key = key
end

Instance Attribute Details

#contentObject Also known as: body



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/coconductor/code_of_conduct.rb', line 118

def content
  # See https://github.com/stumpsyn/policies/pull/21
  @content ||= if parts.nil?
                 nil
               elsif citizen_code_of_conduct?
                 fields = %w[COMMUNITY_NAME GOVERNING_BODY]
                 parts.last.gsub(/ (#{Regexp.union(fields)}) /, ' [\1] ')
               else
                 parts.last
               end
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

.allObject



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

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

.familiesObject



48
49
50
51
52
# File 'lib/coconductor/code_of_conduct.rb', line 48

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
21
# File 'lib/coconductor/code_of_conduct.rb', line 17

def find(key_or_family)
  return new(key_or_family) if new(key_or_family).pseudo?
  match = all.find { |coc| coc.key == key_or_family }
  match || latest_in_family(key_or_family)
end

.keysObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/coconductor/code_of_conduct.rb', line 29

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)
    next unless matches
    [
      matches['family'], 'version', matches['version'], matches['lang']
    ].compact.join('/')
  end.compact
end

.latestObject



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: DEFAULT_LANGUAGE) ⇒ Object



41
42
43
44
45
46
# File 'lib/coconductor/code_of_conduct.rb', line 41

def latest_in_family(family, language: DEFAULT_LANGUAGE)
  cocs = all.select do |coc|
    coc.language == language && coc.family == family
  end
  cocs.max_by { |c| c.geek_feminism? ? !c.version : c.version }
end

.vendor_dirObject



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

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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



158
159
160
# File 'lib/coconductor/code_of_conduct.rb', line 158

def ==(other)
  other.is_a?(CodeOfConduct) && key == other.key
end

#familyObject



135
136
137
# File 'lib/coconductor/code_of_conduct.rb', line 135

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

#fieldsObject



154
155
156
# File 'lib/coconductor/code_of_conduct.rb', line 154

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

#inspectObject



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

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

#languageObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/coconductor/code_of_conduct.rb', line 91

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

#nameObject



104
105
106
107
108
109
110
# File 'lib/coconductor/code_of_conduct.rb', line 104

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

#name_without_versionObject



112
113
114
115
116
# File 'lib/coconductor/code_of_conduct.rb', line 112

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

#none?Boolean

The “none” code of conduct represents the lack of a code of conduct

Returns:



145
146
147
# File 'lib/coconductor/code_of_conduct.rb', line 145

def none?
  key == 'none'
end

#other?Boolean

The “other” code of conduct represents an unidentifiable code of conduct

Returns:



140
141
142
# File 'lib/coconductor/code_of_conduct.rb', line 140

def other?
  key == 'other'
end

#pseudo?Boolean

Code of conduct is an pseudo code of conduct (e.g., none, other)

Returns:



150
151
152
# File 'lib/coconductor/code_of_conduct.rb', line 150

def pseudo?
  other? || none?
end

#versionObject



87
88
89
# File 'lib/coconductor/code_of_conduct.rb', line 87

def version
  toml['version']
end