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.freeze
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.



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

def initialize(key)
  @key = key
end

Instance Attribute Details

#contentObject Also known as: body



122
123
124
125
126
127
128
129
# File 'lib/coconductor/code_of_conduct.rb', line 122

def content
  # See https://github.com/stumpsyn/policies/pull/21
  @content ||= if parts.nil?
                 nil
               else
                 parts.last
               end
end

#keyObject (readonly)

Returns the value of attribute key.



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

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



50
51
52
53
54
55
# File 'lib/coconductor/code_of_conduct.rb', line 50

def families
  @families ||= begin
    families = Dir["#{vendor_dir}/*"].map { |dir| File.basename(dir) }
    (families - ['bundle']).sort
  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
22
# 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



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

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.sort
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



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

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



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

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

Instance Method Details

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



172
173
174
# File 'lib/coconductor/code_of_conduct.rb', line 172

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

#familyObject



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

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

#fieldsObject

Returns all fields found in the code of conduct

Each field will have a unique raw_text, uniq-ing identical fields



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

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

#inspectObject



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

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

#languageObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/coconductor/code_of_conduct.rb', line 94

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



107
108
109
110
111
112
113
114
# File 'lib/coconductor/code_of_conduct.rb', line 107

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



116
117
118
119
120
# File 'lib/coconductor/code_of_conduct.rb', line 116

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:

  • (Boolean)


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

def none?
  key == 'none'
end

#other?Boolean

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

Returns:

  • (Boolean)


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

def other?
  key == 'other'
end

#pseudo?Boolean

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

Returns:

  • (Boolean)


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

def pseudo?
  other? || none?
end

#unique_fieldsObject

Returns all fields found in the code of conduct, uniq’d by normalized key

Where the same field exists twice, preferance will be given to the first occurance to contain a description



166
167
168
169
170
# File 'lib/coconductor/code_of_conduct.rb', line 166

def unique_fields
  @unique_fields ||= begin
    fields.sort_by { |f| f.description ? 0 : 1 }.uniq(&:key)
  end
end

#versionObject



90
91
92
# File 'lib/coconductor/code_of_conduct.rb', line 90

def version
  toml['version']
end