Sha256: 903c286722ae83ada382b62257bb8f73147e343da52a178739d32abf84793236

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Licensee
  class LicenseMeta < Struct.new(
    :title, :spdx_id, :source, :description, :how, :conditions, :permissions,
    :limitations, :using, :featured, :hidden, :nickname, :note
  )

    # These should be in sync with choosealicense.com's collection defaults
    DEFAULTS = {
      'featured' => false,
      'hidden'   => true
    }.freeze

    PREDICATE_FIELDS = %i[featured hidden].freeze

    include Licensee::HashHelper
    HASH_METHODS = members - %i[conditions permissions limitations spdx_id]

    class << self
      # Create a new LicenseMeta from YAML
      #
      # yaml - the raw YAML string
      #
      # returns a LicenseMeta with defaults set
      def from_yaml(yaml)
        return from_hash({}) if yaml.nil? || yaml.to_s.empty?

        from_hash YAML.safe_load(yaml)
      end

      # Create a new LicenseMeta from a hash
      #
      # hash - the hash of key/value meta pairs
      #
      # returns a LicenseMeta with defaults set
      def from_hash(hash)
        hash = DEFAULTS.merge(hash)
        hash['spdx_id'] = hash.delete('spdx-id')
        ordered_array = hash.values_at(*members.map(&:to_s))
        new(*ordered_array)
      end

      # Array of symbolized helper methods to expose on the License class
      def helper_methods
        members - PREDICATE_FIELDS + PREDICATE_FIELDS.map { |f| :"#{f}?" }
      end
    end

    PREDICATE_FIELDS.each do |field|
      alias_method :"#{field}?", field
    end

    # Backward compatibalize `#["spdx-id"]` calls to avoid a breaking change
    def [](key)
      key = 'spdx_id' if key == 'spdx-id'
      super
    end

    def source
      "https://spdx.org/licenses/#{spdx_id}.html" if spdx_id
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
licensee-9.18.0 lib/licensee/license_meta.rb
licensee-9.17.1 lib/licensee/license_meta.rb