Class: Licensee::LicenseField

Inherits:
Struct
  • Object
show all
Defined in:
lib/licensee/license_field.rb,
lib/licensee/license_field.rb

Overview

Represents a templated field placeholder used in license text.

Constant Summary collapse

FIELD_REGEX =
/\[(#{Regexp.union(keys)})\]/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



4
5
6
# File 'lib/licensee/license_field.rb', line 4

def description
  @description
end

#nameObject Also known as: key

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



4
5
6
# File 'lib/licensee/license_field.rb', line 4

def name
  @name
end

Class Method Details

.allObject

Returns an array of all known LicenseFields



24
25
26
27
28
29
30
31
# File 'lib/licensee/license_field.rb', line 24

def all
  @all ||= begin
    path   = '../../vendor/choosealicense.com/_data/fields.yml'
    path   = File.expand_path path, __dir__
    fields = YAML.safe_load_file(path)
    fields.map { |field| from_hash(field) }
  end
end

.find(key) ⇒ Object

Return a single license field

key - string representing the field’s text

Returns a LicenseField



14
15
16
# File 'lib/licensee/license_field.rb', line 14

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

.from_array(array) ⇒ Object

Given an array of keys, returns an array of coresponding LicenseFields



40
41
42
# File 'lib/licensee/license_field.rb', line 40

def from_array(array)
  array.map { |key| find(key) }
end

.from_content(content) ⇒ Object

Given a license body, returns an array of included LicneseFields



45
46
47
48
49
# File 'lib/licensee/license_field.rb', line 45

def from_content(content)
  return [] unless content

  from_array content.scan(FIELD_REGEX).flatten
end

.from_hash(hash) ⇒ Object

Builds a LicenseField from a hash of properties



34
35
36
37
# File 'lib/licensee/license_field.rb', line 34

def from_hash(hash)
  ordered_array = hash.values_at(*members.map(&:to_s))
  new(*ordered_array)
end

.keysObject

Returns an array of strings representing all field keys



19
20
21
# File 'lib/licensee/license_field.rb', line 19

def keys
  @keys ||= all.map(&:key)
end

Instance Method Details

#labelObject Also known as: to_s

The human-readable field name



56
57
58
# File 'lib/licensee/license_field.rb', line 56

def label
  key.sub('fullname', 'full name').capitalize
end

#raw_textObject



61
62
63
# File 'lib/licensee/license_field.rb', line 61

def raw_text
  "[#{key}]"
end