Class: Coconductor::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/coconductor/field.rb

Overview

Represents a fillable field in a code of conduct

Constant Summary collapse

REGEX =
/\[(?<name>[A-Z_ ]{2,})[\s:-]*(?<description>.*?)\]/.freeze
DESCRIPTIONS =

Hard coded field descriptions in the form of key => description

{
  'link_to_reporting_guidelines' => 'An optional link to guidelines for ' \
    'how reports of unacceptable behavior will be handled.',
  'link_to_policy' => 'An optional link to guidelines for how warnings ' \
    'and expulsions of community members will be handled.'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_text, name: nil, description: nil) ⇒ Field

Returns a new instance of Field.



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

def initialize(raw_text, name: nil, description: nil)
  @raw_text = raw_text
  @name = name
  @description = description.capitalize if description && description != ''
end

Instance Attribute Details

#raw_textObject (readonly)

the matchable raw text within the code of conduct including brackets



9
10
11
# File 'lib/coconductor/field.rb', line 9

def raw_text
  @raw_text
end

Class Method Details

.allObject

Returns all fields accross all vendored codes of conduct



37
38
39
# File 'lib/coconductor/field.rb', line 37

def all
  @all ||= CodeOfConduct.latest.map(&:fields).flatten.uniq(&:key)
end

.from_code_of_conduct(code_of_conduct) ⇒ Object

Returns an array of Fields for the given code of conduct



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/coconductor/field.rb', line 21

def from_code_of_conduct(code_of_conduct)
  matches = []
  return [] unless code_of_conduct&.content

  code_of_conduct.content.scan(REGEX) do |_m|
    matches << Regexp.last_match
  end

  fields = matches.map do |m|
    new(m[0], name: m[:name], description: m[:description])
  end

  fields.uniq(&:raw_text)
end

Instance Method Details

#descriptionObject



63
64
65
66
67
68
69
# File 'lib/coconductor/field.rb', line 63

def description
  @description ||= begin
    return parts[:description].capitalize if parts && parts[:description] && parts[:description] != ''

    DESCRIPTIONS[key]
  end
end

#inspectObject



71
72
73
74
75
76
# File 'lib/coconductor/field.rb', line 71

def inspect
  methods = %w[name key label]
  kvs = methods.map { |method| [method, public_send(method)] }.to_h
  kvs = kvs.map { |k, v| "#{k}=\"#{v}\"" }.join(' ')
  "<Coconductor::Field #{kvs}>"
end

#keyObject

machine readable key



59
60
61
# File 'lib/coconductor/field.rb', line 59

def key
  @key ||= normalized_name.downcase
end

#labelObject

human readable label



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

def label
  @label ||= normalized_name.downcase.tr('_', ' ').capitalize
end

#nameObject

The unformatted field name as found in the code of conduct text



49
50
51
# File 'lib/coconductor/field.rb', line 49

def name
  @name ||= parts ? parts[:name].strip : nil
end