Sha256: eb81ba3f549b2e7e057746ea5ec80c9cc900fce4630182a407657ff51be0f47e

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

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}?".to_sym }
      end
    end

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

    # Backward compatibalize `#["spdx-id"]` calls to avoid a breaking change
    def [](key)
      key = 'spdx_id' if key == 'spdx-id'
      super(key)
    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.11.0 lib/licensee/license_meta.rb
licensee-9.10.1 lib/licensee/license_meta.rb