Class: SBOM::CycloneDX::Record::Base

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/sbom/cyclone_dx/record/base.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

Advisory, Annotation, Annotation::Annotator, Attachment, CO2Measure, CipherSuite, Command, Commit, Component, Component::Pedigree, ComponentData, ComponentData::Content, ComponentEvidence, ComponentEvidence::Callstack, ComponentEvidence::Callstack::Frame, ComponentEvidence::Occurrence, ComponentIdentityEvidence, ComponentIdentityEvidence::Method, Composition, Condition, Copyright, CryptoProperties, CryptoProperties::AlgorithmProperties, CryptoProperties::CertificateProperties, CryptoProperties::ProtocolProperties, CryptoProperties::ProtocolProperties::IKEv2TransformType, CryptoProperties::RelatedCryptoMaterialProperties, DataGovernance, DataGovernanceResponsibleParty, Declarations, Declarations::Affirmation, Declarations::Affirmation::Signatory, Declarations::Assessor, Declarations::Attestation, Declarations::Attestation::Map, Declarations::Attestation::Map::Confidence, Declarations::Attestation::Map::Conformance, Declarations::Claim, Declarations::Evidence, Declarations::Evidence::EvidenceData, Declarations::Evidence::EvidenceData::Contents, Declarations::Target, Definitions, Dependency, Diff, EnergyConsumption, EnergyMeasure, EnergyProvider, EnvironmentalConsideration, Event, ExternalReference, FairnessAssessment, Formula, Graphic, GraphicsCollection, HashData, IdentifiableAction, Input, InputOutputMLParameter, Issue, Issue::Source, License, License::Licensing, License::Licensing::Licensee, License::Licensing::Licensor, License::Licensing::Purchaser, LicenseChoice::LicenseExpression, LicenseChoice::WrappedLicense, Metadata, Metadata::CustomPhase, Metadata::PreDefinedPhase, ModelCard, ModelCard::Considerations, ModelCard::ModelParameters, ModelCard::ModelParameters::Approach, ModelCard::ModelParameters::DataReference, ModelCard::QuantitativeAnalysis, Note, OrganizationalContact, OrganizationalEntity, Output, Parameter, Patch, PerformanceMetric, PerformanceMetric::ConfidenceInterval, PostalAddress, Property, Rating, ReleaseNotes, ResourceReferenceChoice, Risk, Root, SWID, SecuredBy, Service, ServiceData, Signature::JSFSignature, Signature::JSFSignature::PublicKey::EC, Signature::JSFSignature::PublicKey::OKP, Signature::JSFSignature::PublicKey::RSA, Signature::SignatureChain, Signature::SignerList, Standard, Standard::Level, Standard::Requirement, Step, Task, Tools, Trigger, Version, Volume, Vulnerability, Vulnerability::Affects, Vulnerability::Analysis, Vulnerability::Credits, Vulnerability::ProofOfConcept, Vulnerability::Reference, VulnerabilitySource, Workflow, Workspace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
# File 'lib/sbom/cyclone_dx/record/base.rb', line 17

def initialize(**args)
  raise "Cannot instantiate abstract Record" unless self.class < Base

  populate_fields(**args)
  valid?
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



15
16
17
# File 'lib/sbom/cyclone_dx/record/base.rb', line 15

def errors
  @errors
end

Class Method Details

.const(field_name, type, value, required: false, json_name: nil, **kwargs) ⇒ Object



181
182
183
# File 'lib/sbom/cyclone_dx/record/base.rb', line 181

def const(field_name, type, value, required: false, json_name: nil, **kwargs)
  prop(field_name, type, required: required, json_name: json_name, const: value, **kwargs)
end

.custom_validatorsObject



202
203
204
# File 'lib/sbom/cyclone_dx/record/base.rb', line 202

def custom_validators
  @custom_validators ||= []
end

.fieldsObject



113
114
115
# File 'lib/sbom/cyclone_dx/record/base.rb', line 113

def fields
  @fields ||= {} #: Hash[Symbol, singleton(SBOM::CycloneDX::Field::Base)]
end

.json_create(object) ⇒ Object



57
58
59
# File 'lib/sbom/cyclone_dx/record/base.rb', line 57

def self.json_create(object)
  new(**object.deep_symbolize_keys)
end

.json_name(klass_name = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/sbom/cyclone_dx/record/base.rb', line 117

def json_name(klass_name = nil)
  unless klass_name.nil?
    raise ArgumentError, "json_name can only be set within the class body" unless in_subclass_body?

    return @json_name = klass_name
  end

  @json_name ||= name&.split("::")&.last || "Record"
end

.prop(field_name, type, required: false, json_name: nil, **kwargs) ⇒ Object

DSL Methods



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/sbom/cyclone_dx/record/base.rb', line 131

def prop(field_name, type, required: false, json_name: nil, **kwargs) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
  raise "properties cannot be defined for abstract Record" unless self < Base
  raise "properties must be defined in the class body of a subclass of Record" unless in_subclass_body?
  raise "property #{field_name} already defined" if fields.key?(field_name)

  new_prop =
    case type
    when :array
      opts = kwargs.slice(:const, :default, :unique) #: arrayFieldOptions
      Field.array(field_name: field_name, items: kwargs.fetch(:items), required: required,
                  json_name: json_name, **opts)
    when :boolean
      opts = kwargs.slice(:const, :default) #: booleanFieldOptions
      Field.boolean(field_name: field_name, required: required, json_name: json_name, **opts)
    when :date_time
      opts = kwargs.slice(:const, :default) #: dateTimeFieldOptions
      Field.date_time(field_name: field_name,  required: required, json_name: json_name, **opts)
    when :email_address
      opts = kwargs.slice(:const, :default) #: emailAddressFieldOptions
      Field.email_address(field_name: field_name, required: required, json_name: json_name, **opts)
    when :float
      opts = kwargs.slice(:const, :default, :maximum, :minimum) #: floatFieldOptions
      Field.float(field_name: field_name, required: required, json_name: json_name, **opts)
    when :integer
      opts = kwargs.slice(:const, :default, :maximum, :minimum) #: integerFieldOptions
      Field.integer(field_name: field_name, required: required, json_name: json_name, **opts)
    when Class
      opts = kwargs.slice(:const, :default) #: recordFieldOptions
      Field.record(field_name: field_name, klass: type, required: required, json_name: json_name, **opts)
    when :string
      opts = kwargs.slice(:const, :default, :enum, :max_length, :min_length, :pattern) #: stringFieldOptions
      Field.string(field_name: field_name,  required: required, json_name: json_name, **opts)
    when :union
      opts = kwargs.slice(:const, :default) #: unionFieldOptions
      Field.union(field_name: field_name, of: kwargs.fetch(:of), required: required, json_name: json_name,
                  **opts)
    when :uri
      opts = kwargs.slice(:const, :default) #: uriFieldOptions
      Field.uri(field_name: field_name, required: required, json_name: json_name, **opts)
    else
      raise ArgumentError, "unknown type: #{type}"
    end

  @fields[field_name] = new_prop
  define_method(field_name) { @_fields.fetch(field_name).value }
  define_method(:"#{field_name}=") { |value| @_fields.fetch(field_name).value = value } unless new_prop.const?
  define_method(:"#{field_name}?") { @_fields.fetch(field_name).value? }
  define_method(:"#{field_name}_valid?") { @_fields.fetch(field_name).valid? }
end

.validate(*props, presence: nil, message: nil, &block) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/sbom/cyclone_dx/record/base.rb', line 185

def validate(*props, presence: nil, message: nil, &block) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
  raise "custom validators cannot be defined for abstract Record" unless self < Base
  raise "custom validators must be defined in the class body of a subclass of Record" unless in_subclass_body?

  @custom_validators ||= [] #: Array[[Array[Symbol], String, ^(*fieldValue?) -> (bool? | String | Array[String])]]
  @custom_validators <<
    if presence && block
      raise ArgumentError, "cannot provide both :presence and a block"
    elsif presence
      validate_presence(props, presence, message)
    elsif block
      [props, message, block]
    else
      raise ArgumentError, "must provide :presence or a block"
    end
end

Instance Method Details

#<=>(other) ⇒ Object



24
25
26
27
28
# File 'lib/sbom/cyclone_dx/record/base.rb', line 24

def <=>(other)
  return nil unless other.is_a?(self.class)

  as_json <=> other.as_json
end

#formatted_errorsObject



48
49
50
51
52
53
54
55
# File 'lib/sbom/cyclone_dx/record/base.rb', line 48

def formatted_errors
  errors.filter_map do |field_name, field_errors|
    next if field_errors.empty?

    field_name = self.class.json_name if field_name == :_base
    field_errors.map { |error| "#{field_name} #{error}" }
  end.flatten
end

#valid!Object

Raises:

  • (ArgumentError)


44
45
46
# File 'lib/sbom/cyclone_dx/record/base.rb', line 44

def valid!
  raise ArgumentError, formatted_errors
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sbom/cyclone_dx/record/base.rb', line 30

def valid?
  @errors = @_fields.transform_values do |field|
    field.valid?
    field.errors
  end
  @errors[:_base] = []

  self.class.custom_validators.each do |props, message, block|
    @errors[:_base] += validate_custom(*props, message: message, &block)
  end

  @errors.values.all?(&:empty?)
end