Class: Licensed::DependencyRecord

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

Defined Under Namespace

Classes: Error, License

Constant Summary collapse

EXTENSION =
"dep.yml".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(licenses: [], notices: [], metadata: {}) ⇒ DependencyRecord

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



72
73
74
75
76
# 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 = 
end

Instance Attribute Details

#licensesObject (readonly)

Returns the value of attribute licenses.



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

def licenses
  @licenses
end

#noticesObject (readonly)

Returns the value of attribute notices.



65
66
67
# File 'lib/licensed/dependency_record.rb', line 65

def notices
  @notices
end

Class Method Details

.read(filename) ⇒ Object

Read an existing record file

filename - A String path to the file

Returns a Licensed::DependencyRecord



50
51
52
53
54
55
56
57
58
59
60
61
# 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

Instance Method Details

#contentObject

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



93
94
95
96
# 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) ⇒ Boolean

Returns whether two records match based on their contents

Returns:

  • (Boolean)


99
100
101
102
# 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) ⇒ Object

Save the metadata and text to a file

filename - The destination file to save record contents at



81
82
83
84
85
86
87
88
89
# 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