Class: Coconductor::CodeOfConduct

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

Constant Summary collapse

VENDORED_CODES_OF_CONDUCT =
%w[
  citizen-code-of-conduct
  contributor-covenant
].freeze
KEY_REGEX =
%r{
  (?<family>#{Regexp.union(VENDORED_CODES_OF_CONDUCT)})
  /version
  /(?<major>\d)/(?<minor>\d)(/(?<patch>\d))?
  /#{Coconductor::ProjectFiles::CodeOfConductFile::FILENAME_REGEX}
}ix
FIELD_REGEX =
/\[(?<name>[A-Z_ ]{2,}).*\]/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ CodeOfConduct

Returns a new instance of CodeOfConduct.



58
59
60
# File 'lib/coconductor/code_of_conduct.rb', line 58

def initialize(key)
  @key = key
end

Instance Attribute Details

#content ⇒ Object



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

def content
  @content ||= parts.last
end

#key ⇒ Object (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

.all ⇒ Object



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

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

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



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

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

.keys ⇒ Object



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

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

.vendor_dir ⇒ Object



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

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

Instance Method Details

#citizen_code_of_conduct? ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/coconductor/code_of_conduct.rb', line 103

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

#contributor_covenant? ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/coconductor/code_of_conduct.rb', line 99

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

#family ⇒ Object



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

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

#fields ⇒ Object



107
108
109
# File 'lib/coconductor/code_of_conduct.rb', line 107

def fields
  content.scan(FIELD_REGEX).flatten
end

#inspect ⇒ Object



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

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

#language ⇒ Object



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

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

#name ⇒ Object



73
74
75
76
77
78
79
# File 'lib/coconductor/code_of_conduct.rb', line 73

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



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

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

#version ⇒ Object



62
63
64
# File 'lib/coconductor/code_of_conduct.rb', line 62

def version
  toml['version']
end