Sha256: 941a90cf113905245f45e61f19ffb438bcb8b35cecf4811b482bff01df831d0c

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require "yaml"
require "fileutils"
require "forwardable"

module Licensed
  class License
    YAML_FRONTMATTER_PATTERN = /\A---\s*\n(.*?\n?)^---\s*$\n?(.*)\z/m
    LEGAL_FILES = /\A(COPYING|NOTICE|LEGAL)(?:\..*)?\z/i

    # Read an existing license file
    #
    # filename - A String path to the file
    #
    # Returns a Licensed::License
    def self.read(filename)
      match = File.read(filename).scrub.match(YAML_FRONTMATTER_PATTERN)
      new(YAML.load(match[1]), match[2])
    end

    extend Forwardable
    def_delegators :@metadata, :[], :[]=

    # The license text and other legal notices
    attr_accessor :text

    # Construct a new license
    #
    # filename - the String path of the file
    # metadata - a Hash of the metadata for the package
    # text     - a String of the license text and other legal notices
    def initialize(metadata = {}, text = nil)
      @metadata = metadata
      @text = text
    end

    # Save the metadata and license to a file
    def save(filename)
      FileUtils.mkdir_p(File.dirname(filename))
      File.write(filename, YAML.dump(@metadata) + "---\n#{text}")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
licensed-0.11.1 lib/licensed/license.rb
licensed-0.11.0 lib/licensed/license.rb
licensed-0.10.0 lib/licensed/license.rb