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



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

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.



56
57
58
# File 'lib/licensed/dependency_record.rb', line 56

def licenses
  @licenses
end

#noticesObject (readonly)

Returns the value of attribute notices.



57
58
59
# File 'lib/licensed/dependency_record.rb', line 57

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



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/licensed/dependency_record.rb', line 42

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`



85
86
87
88
# File 'lib/licensed/dependency_record.rb', line 85

def content
  return if licenses.nil? || licenses.empty?
  licenses.map(&:text).compact.join
end

#matches?(other) ⇒ Boolean

Returns whether two records match based on their contents

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/licensed/dependency_record.rb', line 91

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



73
74
75
76
77
78
79
80
81
# File 'lib/licensed/dependency_record.rb', line 73

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