Class: Licensed::DependencyRecord

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

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



38
39
40
41
42
# File 'lib/licensed/dependency_record.rb', line 38

def initialize(licenses: [], notices: [], metadata: {})
  @licenses = [licenses].flatten.compact
  @notices = [notices].flatten.compact
  @metadata = 
end

Instance Attribute Details

#licensesObject (readonly)

Returns the value of attribute licenses.



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

def licenses
  @licenses
end

#noticesObject (readonly)

Returns the value of attribute notices.



31
32
33
# File 'lib/licensed/dependency_record.rb', line 31

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



18
19
20
21
22
23
24
25
26
27
# File 'lib/licensed/dependency_record.rb', line 18

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
  )
end

Instance Method Details

#contentObject

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/licensed/dependency_record.rb', line 59

def content
  return if licenses.nil? || licenses.empty?
  licenses.map do |license|
    if license.is_a?(String)
      license
    elsif license.respond_to?(:[])
      license["text"]
    end
  end.join
end

#matches?(other) ⇒ Boolean

Returns whether two records match based on their contents

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/licensed/dependency_record.rb', line 71

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



47
48
49
50
51
52
53
54
55
# File 'lib/licensed/dependency_record.rb', line 47

def save(filename)
  data_to_save = @metadata.merge({
    "licenses" => licenses,
    "notices" => notices
  })

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