Class: Licensee::LicenseField
- Inherits:
-
Struct
- Object
- Struct
- Licensee::LicenseField
- Defined in:
- lib/licensee/license_field.rb
Constant Summary collapse
- FIELD_REGEX =
/\[(#{Regexp.union(LicenseField.keys)})\]/.freeze
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
(also: #key)
Returns the value of attribute name.
Class Method Summary collapse
-
.all ⇒ Object
Returns an array of all known LicenseFields.
-
.find(key) ⇒ Object
Return a single license field.
-
.from_array(array) ⇒ Object
Given an array of keys, returns an array of coresponding LicenseFields.
-
.from_content(content) ⇒ Object
Given a license body, returns an array of included LicneseFields.
-
.from_hash(hash) ⇒ Object
Builds a LicenseField from a hash of properties.
-
.keys ⇒ Object
Returns an array of strings representing all field keys.
Instance Method Summary collapse
-
#label ⇒ Object
(also: #to_s)
The human-readable field name.
- #raw_text ⇒ Object
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description
2 3 4 |
# File 'lib/licensee/license_field.rb', line 2 def description @description end |
#name ⇒ Object Also known as: key
Returns the value of attribute name
2 3 4 |
# File 'lib/licensee/license_field.rb', line 2 def name @name end |
Class Method Details
.all ⇒ Object
Returns an array of all known LicenseFields
19 20 21 22 23 24 25 26 |
# File 'lib/licensee/license_field.rb', line 19 def all @all ||= begin path = '../../vendor/choosealicense.com/_data/fields.yml' path = File. path, __dir__ fields = YAML.safe_load File.read(path) fields.map { |field| LicenseField.from_hash(field) } end end |
.find(key) ⇒ Object
Return a single license field
key - string representing the field’s text
Returns a LicenseField
9 10 11 |
# File 'lib/licensee/license_field.rb', line 9 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
35 36 37 |
# File 'lib/licensee/license_field.rb', line 35 def from_array(array) array.map { |key| LicenseField.find(key) } end |
.from_content(content) ⇒ Object
Given a license body, returns an array of included LicneseFields
40 41 42 43 44 |
# File 'lib/licensee/license_field.rb', line 40 def from_content(content) return [] unless content LicenseField.from_array content.scan(FIELD_REGEX).flatten end |
.from_hash(hash) ⇒ Object
Builds a LicenseField from a hash of properties
29 30 31 32 |
# File 'lib/licensee/license_field.rb', line 29 def from_hash(hash) ordered_array = hash.values_at(*members.map(&:to_s)) new(*ordered_array) end |
.keys ⇒ Object
Returns an array of strings representing all field keys
14 15 16 |
# File 'lib/licensee/license_field.rb', line 14 def keys @keys ||= LicenseField.all.map(&:key) end |
Instance Method Details
#label ⇒ Object Also known as: to_s
The human-readable field name
51 52 53 |
# File 'lib/licensee/license_field.rb', line 51 def label key.sub('fullname', 'full name').capitalize end |
#raw_text ⇒ Object
56 57 58 |
# File 'lib/licensee/license_field.rb', line 56 def raw_text "[#{key}]" end |