Class: Licensed::License

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Licensee::ContentHelper
Defined in:
lib/licensed/license.rb

Direct Known Subclasses

Dependency

Constant Summary collapse

YAML_FRONTMATTER_PATTERN =
/\A---\s*\n(.*?\n?)^---\s*$\n?(.*)\z/m
TEXT_SEPARATOR =
("-" * 80).freeze
LICENSE_SEPARATOR =
("*" * 80).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata = {}, text = nil) ⇒ License

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



37
38
39
40
# File 'lib/licensed/license.rb', line 37

def initialize( = {}, text = nil)
   = 
  @text = text
end

Instance Attribute Details

#textObject

The license text and other legal notices



30
31
32
# File 'lib/licensed/license.rb', line 30

def text
  @text
end

Class Method Details

.read(filename) ⇒ Object

Read an existing license file

filename - A String path to the file

Returns a Licensed::License



21
22
23
24
25
# File 'lib/licensed/license.rb', line 21

def self.read(filename)
  return unless File.exist?(filename)
  match = File.read(filename).scrub.match(YAML_FRONTMATTER_PATTERN)
  new(YAML.load(match[1]), match[2])
end

Instance Method Details

#license_textObject Also known as: content

Returns the license text without any notices



49
50
51
52
53
54
55
56
57
58
# File 'lib/licensed/license.rb', line 49

def license_text
  return unless text

  # if the text contains the separator, the first string in the array
  # should always be the license text whether empty or not.
  # if the text didn't contain the separator, the text itself is the entirety
  # of the license text
  split = text.split(TEXT_SEPARATOR)
  split.length > 1 ? split.first.rstrip : text.rstrip
end

#license_text_match?(other) ⇒ Boolean

Returns whether the current license should be updated to ‘other` based on whether the normalized license content matches

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/licensed/license.rb', line 63

def license_text_match?(other)
  return false unless other.is_a?(License)
  self.content_normalized == other.content_normalized
end

#save(filename) ⇒ Object

Save the metadata and license to a file



43
44
45
46
# File 'lib/licensed/license.rb', line 43

def save(filename)
  FileUtils.mkdir_p(File.dirname(filename))
  File.write(filename, YAML.dump() + "---\n#{text}")
end