Sha256: f4fa1bc32affe3d595b0ece4b27f0484b48652efdc14866c23539f073a3c9113

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 KB

Contents

module Licensee
  class LicenseField < Struct.new(:name, :description)
    class << self
      # Return a single license field
      #
      # key - string representing the field's text
      #
      # Returns a LicenseField
      def find(key)
        @all.find { |f| f.key == key }
      end

      # Returns an array of strings representing all field keys
      def keys
        @keys ||= LicenseField.all.map(&:key)
      end

      # Returns an array of all known LicenseFields
      def all
        @all ||= begin
          path   = '../../vendor/choosealicense.com/_data/fields.yml'
          path   = File.expand_path path, __dir__
          fields = YAML.safe_load File.read(path)
          fields.map { |field| LicenseField.from_hash(field) }
        end
      end

      # Builds a LicenseField from a hash of properties
      def from_hash(hash)
        ordered_array = hash.values_at(*members.map(&:to_s))
        new(*ordered_array)
      end

      # Given an array of keys, returns an array of coresponding LicenseFields
      def from_array(array)
        array.map { |key| LicenseField.find(key) }
      end

      # Given a license body, returns an array of included LicneseFields
      def from_content(content)
        return [] unless content
        LicenseField.from_array content.scan(FIELD_REGEX).flatten
      end
    end

    alias key name
    FIELD_REGEX = /\[(#{Regexp.union(LicenseField.keys)})\]/

    # The human-readable field name
    def label
      key.sub('fullname', 'full name').capitalize
    end
    alias to_s label
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
licensee-9.9.4 lib/licensee/license_field.rb
licensee-9.9.3 lib/licensee/license_field.rb
licensee-9.9.2 lib/licensee/license_field.rb
licensee-9.9.1 lib/licensee/license_field.rb
licensee-9.9.0 lib/licensee/license_field.rb
licensee-9.9.0.beta.3 lib/licensee/license_field.rb
licensee-9.9.0.beta.2 lib/licensee/license_field.rb
licensee-9.8.0 lib/licensee/license_field.rb
licensee-9.7.0 lib/licensee/license_field.rb
licensee-9.6.0 lib/licensee/license_field.rb
licensee-9.5.0 lib/licensee/license_field.rb
licensee-9.4.0 lib/licensee/license_field.rb