coconductor 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 295cf8fe4384c753c323d0617abf9b74436ad0887ae3558b4684b90819aabad1
4
- data.tar.gz: cb50009fe71804e04ab800c3ab9e1d1e3d7490002d06d1e4f9d36121c67eab52
3
+ metadata.gz: 05bb04085f2577f27816fad8a5c2fe21772401516f3e245d238aa8bf97437f31
4
+ data.tar.gz: 5cd56a968556c68e4c527ca53cced7a20e2869d3cfd5d70dd02d7d37b432d543
5
5
  SHA512:
6
- metadata.gz: dfa120239578da0cc879d7029c54e3c2420270d3e07482113cba126bf8acaaa16d7d6222af94f00da03b0e685a27b2376c086d721db2fba821e7210d17ac9ab3
7
- data.tar.gz: '081cc970eb3bfa46838849cc5e504d3af04dc2b8d007c8b8d4114993011fd862859392121ad3d25373c8cf84d8a41f838e5261799742752e2e2974cfcd17ffa9'
6
+ metadata.gz: 3815b1990aca73057b3442225327a6002772bbc8b5e02f2fab024346166c92de713b64befd5bacdf3e215048cb9407df1ebdf748c8efaeb3f1cfefd250084c9b
7
+ data.tar.gz: c2714f37217cd29b5b2e0efb794c06fe4d5e86bd2ed4f4b64f0af16124688c5a7870fa950f08bd06b0f203d4f159c7aeaf63e89a58028ff6cedf65eb74a0f53c
@@ -64,7 +64,6 @@ module Coconductor
64
64
  /(?<major>\d)/(?<minor>\d)(/(?<patch>\d))?
65
65
  /#{Coconductor::ProjectFiles::CodeOfConductFile::FILENAME_REGEX}
66
66
  }ix
67
- FIELD_REGEX = /\[(?<name>[A-Z_ ]{2,}).*\]/
68
67
 
69
68
  attr_reader :key
70
69
  attr_writer :content
@@ -125,7 +124,7 @@ module Coconductor
125
124
  end
126
125
 
127
126
  def fields
128
- content.scan(FIELD_REGEX).flatten
127
+ @fields ||= Field.from_code_of_conduct(self)
129
128
  end
130
129
 
131
130
  private
@@ -0,0 +1,78 @@
1
+ module Coconductor
2
+ # Represents a fillable field in a code of conduct
3
+ class Field
4
+ REGEX = /\[(?<name>[A-Z_ ]{2,}):?\s?(?<description>.*)\]/
5
+
6
+ # the matchable raw text within the code of conduct including brackets
7
+ attr_reader :raw_text
8
+
9
+ class << self
10
+ # Returns an array of Fields for the given code of conduct
11
+ def from_code_of_conduct(code_of_conduct)
12
+ matches = []
13
+
14
+ code_of_conduct.content.scan(REGEX) do |_m|
15
+ matches << Regexp.last_match
16
+ end
17
+
18
+ fields = matches.map do |m|
19
+ new(m[0], name: m[:name], description: m[:description])
20
+ end
21
+
22
+ fields.uniq(&:key)
23
+ end
24
+
25
+ # Returns all fields accross all vendored codes of conduct
26
+ def all
27
+ @all ||= CodeOfConduct.latest.map(&:fields).flatten.uniq(&:key)
28
+ end
29
+ end
30
+
31
+ def initialize(raw_text, name: nil, description: nil)
32
+ @raw_text = raw_text
33
+ @name = name
34
+ @description = description
35
+ end
36
+
37
+ # The unformatted field name as found in the code of conduct text
38
+ def name
39
+ @name ||= parts ? parts[:name].strip : nil
40
+ end
41
+
42
+ # human readable label
43
+ def label
44
+ @label ||= normalized_name.downcase.tr('_', ' ').capitalize
45
+ end
46
+
47
+ # machine readable key
48
+ def key
49
+ @key ||= normalized_name.downcase
50
+ end
51
+
52
+ def description
53
+ @description ||= parts ? parts[:description] : nil
54
+ end
55
+
56
+ def inspect
57
+ methods = %w[name key label]
58
+ kvs = methods.map { |method| [method, public_send(method)] }.to_h
59
+ kvs = kvs.map { |k, v| "#{k}=\"#{v}\"" }.join(' ')
60
+ "<Coconductor::Field #{kvs}>"
61
+ end
62
+
63
+ private
64
+
65
+ def parts
66
+ @parts ||= raw_text.match(REGEX)
67
+ end
68
+
69
+ def normalized_name
70
+ @normalized_name ||= begin
71
+ normalized_name = name.tr(' ', '_')
72
+ normalized_name = normalized_name.gsub(/\A(YOUR|INSERT)_/i, '')
73
+ normalized_name = normalized_name.gsub(/_?HERE\z/i, '')
74
+ normalized_name.strip
75
+ end
76
+ end
77
+ end
78
+ end
@@ -23,7 +23,7 @@ module Coconductor
23
23
  coc = code_of_conduct.dup
24
24
  coc.instance_variable_set '@content_normalized', nil
25
25
  coc.instance_variable_set '@content_without_title_and_version', nil
26
- field_regex = /\[?#{Regexp.union(coc.fields)}\]?/
26
+ field_regex = /#{Regexp.union(coc.fields.map(&:raw_text))}/i
27
27
  coc.content = coc.content.gsub(field_regex, FIELD_PLACEHOLDER)
28
28
  regex = Regexp.escape(coc.content_normalized)
29
29
  regex = regex.gsub(FIELD_PLACEHOLDER_REGEX, '([a-z ]+?)')
@@ -1,3 +1,3 @@
1
1
  module Coconductor
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
data/lib/coconductor.rb CHANGED
@@ -3,6 +3,7 @@ require 'licensee'
3
3
 
4
4
  module Coconductor
5
5
  autoload :CodeOfConduct, 'coconductor/code_of_conduct'
6
+ autoload :Field, 'coconductor/field'
6
7
  autoload :Matchers, 'coconductor/matchers'
7
8
  autoload :Projects, 'coconductor/projects'
8
9
  autoload :ProjectFiles, 'coconductor/project_files'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coconductor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
@@ -160,6 +160,7 @@ files:
160
160
  - lib/coconductor/commands/detect.rb
161
161
  - lib/coconductor/commands/diff.rb
162
162
  - lib/coconductor/commands/version.rb
163
+ - lib/coconductor/field.rb
163
164
  - lib/coconductor/matchers.rb
164
165
  - lib/coconductor/matchers/dice.rb
165
166
  - lib/coconductor/matchers/exact.rb