Sha256: bab1b7d786498c5d5591de2c59a91069bf8efb7cad15f9b0629add0c9caad940

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 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

    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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
licensee-9.8.0 lib/licensee/license_meta.rb
licensee-9.7.0 lib/licensee/license_meta.rb
licensee-9.6.0 lib/licensee/license_meta.rb
licensee-9.5.0 lib/licensee/license_meta.rb
licensee-9.4.0 lib/licensee/license_meta.rb
licensee-9.3.1 lib/licensee/license_meta.rb
licensee-9.3.0 lib/licensee/license_meta.rb
licensee-9.2.1 lib/licensee/license_meta.rb
licensee-9.2.0 lib/licensee/license_meta.rb