class Licensed::DependencyRecord

Constants

EXTENSION

Attributes

licenses[R]
notices[R]

Public Class Methods

new(licenses: [], notices: [], metadata: {}) click to toggle source

Construct a new record

licenses - a string, or array of strings, representing the content of each license notices - a string, or array of strings, representing the content of each legal notice metadata - a Hash of the metadata for the package

# File lib/licensed/dependency_record.rb, line 72
def initialize(licenses: [], notices: [], metadata: {})
  @licenses = [licenses].flatten.compact.map { |l| DependencyRecord::License.new(l) }
  @notices = [notices].flatten.compact
  @metadata = metadata
end
read(filename) click to toggle source

Read an existing record file

filename - A String path to the file

Returns a Licensed::DependencyRecord

# File lib/licensed/dependency_record.rb, line 50
def self.read(filename)
  return unless File.exist?(filename)
  data = YAML.load_file(filename)
  return if data.nil? || data.empty?
  new(
    licenses: data.delete("licenses"),
    notices: data.delete("notices"),
    metadata: data
  )
rescue Psych::SyntaxError => e
  raise Licensed::DependencyRecord::Error.new(e.message)
end

Public Instance Methods

content() click to toggle source

Returns the content used to compare two licenses using normalization from ‘Licensee::CotentHelper`

# File lib/licensed/dependency_record.rb, line 93
def content
  return if licenses.nil? || licenses.empty?
  licenses.sort_by(&:key).map(&:text).compact.join
end
matches?(other) click to toggle source

Returns whether two records match based on their contents

# File lib/licensed/dependency_record.rb, line 99
def matches?(other)
  return false unless other.is_a?(DependencyRecord)
  self.content_normalized == other.content_normalized
end
save(filename) click to toggle source

Save the metadata and text to a file

filename - The destination file to save record contents at

# File lib/licensed/dependency_record.rb, line 81
def save(filename)
  data_to_save = @metadata.merge({
    "licenses" => licenses.map(&:to_cache),
    "notices" => notices
  })

  FileUtils.mkdir_p(File.dirname(filename))
  File.write(filename, data_to_save.to_yaml)
end