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'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ CodeOfConduct

Returns a new instance of CodeOfConduct.



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

def initialize(key)
  @key = key
end

Instance Attribute Details

#contentObject Also known as: body



126
127
128
129
130
131
132
133
# File 'lib/coconductor/code_of_conduct.rb', line 126

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.



79
80
81
# File 'lib/coconductor/code_of_conduct.rb', line 79

def key
  @key
end

Class Method Details

.allObject



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

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

.familiesObject



53
54
55
56
57
58
# File 'lib/coconductor/code_of_conduct.rb', line 53

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



20
21
22
23
24
25
# File 'lib/coconductor/code_of_conduct.rb', line 20

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



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/coconductor/code_of_conduct.rb', line 33

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



14
15
16
# File 'lib/coconductor/code_of_conduct.rb', line 14

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

.latest_in_family(family, language: DEFAULT_LANGUAGE) ⇒ Object



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

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



29
30
31
# File 'lib/coconductor/code_of_conduct.rb', line 29

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

Instance Method Details

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



176
177
178
# File 'lib/coconductor/code_of_conduct.rb', line 176

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

#familyObject



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

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



162
163
164
# File 'lib/coconductor/code_of_conduct.rb', line 162

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

#inspectObject



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

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

#languageObject



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

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

#nameObject



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

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



120
121
122
123
124
# File 'lib/coconductor/code_of_conduct.rb', line 120

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)


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

def none?
  key == 'none'
end

#other?Boolean

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

Returns:

  • (Boolean)


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

def other?
  key == 'other'
end

#pseudo?Boolean

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

Returns:

  • (Boolean)


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

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



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

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

#versionObject



94
95
96
# File 'lib/coconductor/code_of_conduct.rb', line 94

def version
  toml['version']
end