Class: Licensed::DependencyRecord
- Inherits:
-
Object
- Object
- Licensed::DependencyRecord
- Extended by:
- Forwardable
- Includes:
- Licensee::ContentHelper
- Defined in:
- lib/licensed/dependency_record.rb
Defined Under Namespace
Constant Summary collapse
- EXTENSION =
"dep.yml".freeze
Instance Attribute Summary collapse
-
#licenses ⇒ Object
readonly
Returns the value of attribute licenses.
-
#notices ⇒ Object
readonly
Returns the value of attribute notices.
Class Method Summary collapse
-
.read(filename) ⇒ Object
Read an existing record file.
Instance Method Summary collapse
-
#content ⇒ Object
Returns the content used to compare two licenses using normalization from ‘Licensee::CotentHelper`.
-
#initialize(licenses: [], notices: [], metadata: {}) ⇒ DependencyRecord
constructor
Construct a new record.
-
#matches?(other) ⇒ Boolean
Returns whether two records match based on their contents.
-
#save(filename) ⇒ Object
Save the metadata and text to a file.
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
#licenses ⇒ Object (readonly)
Returns the value of attribute licenses.
64 65 66 |
# File 'lib/licensed/dependency_record.rb', line 64 def licenses @licenses end |
#notices ⇒ Object (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.) end |
Instance Method Details
#content ⇒ Object
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
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 |