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>.*?)\]/
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.



40
41
42
43
44
# File 'lib/coconductor/field.rb', line 40

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



7
8
9
# File 'lib/coconductor/field.rb', line 7

def raw_text
  @raw_text
end

Class Method Details

.allObject

Returns all fields accross all vendored codes of conduct



35
36
37
# File 'lib/coconductor/field.rb', line 35

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



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

def from_code_of_conduct(code_of_conduct)
  matches = []
  return [] unless code_of_conduct && 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



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

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

    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



57
58
59
# File 'lib/coconductor/field.rb', line 57

def key
  @key ||= normalized_name.downcase
end

#labelObject

human readable label



52
53
54
# File 'lib/coconductor/field.rb', line 52

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

#nameObject

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



47
48
49
# File 'lib/coconductor/field.rb', line 47

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