Skip to content

Commit f477f8d

Browse files
committed
🎨 Resolve code duplication and circular dependency issues; fix specs
Signed-off-by: Peter H. Boling <[email protected]>
1 parent d6379a8 commit f477f8d

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

features/help.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ Scenario: Generate help on demand
1616
--include-metadata Include metadata.tools identifying cyclonedx-ruby as the producer
1717
--enrich-components Include bom-ref and publisher fields on components (uses purl and first author)
1818
--gem-server URL Gem server URL to fetch gem metadata (default: https://gem.coop)
19+
--validate Validate the BOM against CycloneDX schema (currently a no-op)
1920
-h, --help Show help message
2021
"""

lib/cyclonedx/bom_builder.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def self.setup(path)
7575
opts.on('--gem-server URL', 'Gem server URL to fetch gem metadata (default: https://gem.coop)') do |gem_server|
7676
@options[:gem_server] = gem_server
7777
end
78+
opts.on('--validate', 'Validate the BOM against CycloneDX schema (currently a no-op)') do
79+
@options[:validate] = true
80+
end
7881
opts.on_tail('-h', '--help', 'Show help message') do
7982
puts opts
8083
exit

lib/cyclonedx/bom_component.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# frozen_string_literal: true
22

3+
require_relative 'field_accessor'
4+
35
module Cyclonedx
46
class BomComponent
7+
58
DEFAULT_TYPE = 'library'
69
HASH_ALG = 'SHA-256'
710

@@ -63,12 +66,9 @@ def hash_val(include_enrichment: false)
6366

6467
private
6568

69+
# Safe accessor for Hash or OpenStruct-like objects
6670
def fetch(key)
67-
if @gem.respond_to?(:[]) && @gem[key]
68-
@gem[key]
69-
elsif @gem.respond_to?(key)
70-
@gem.public_send(key)
71-
end
71+
FieldAccessor._get(@gem, key)
7272
end
7373
end
7474
end

lib/cyclonedx/bom_helpers.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ def tool_identity
5858
end
5959

6060
# Safe accessor for Hash or OpenStruct-like objects
61+
# Delegates to FieldAccessor to avoid code duplication
6162
def _get(obj, key)
62-
if obj.respond_to?(:[]) && obj[key]
63-
obj[key]
64-
elsif obj.respond_to?(key)
65-
obj.public_send(key)
66-
end
63+
FieldAccessor._get(obj, key)
6764
end
6865

6966
def build_bom(gems, format, spec_version, include_metadata: false, include_enrichment: false)

lib/cyclonedx/field_accessor.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
# This file is part of CycloneDX Ruby Gem.
4+
#
5+
# Licensed to the Apache Software Foundation (ASF) under one
6+
# or more contributor license agreements. See the NOTICE file
7+
# distributed with this work for additional information
8+
# regarding copyright ownership. The ASF licenses this file
9+
# to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance
11+
# with the License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing,
16+
# software distributed under the License is distributed on an
17+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
# KIND, either express or implied. See the License for the
19+
# specific language governing permissions and limitations
20+
# under the License.
21+
#
22+
# SPDX-License-Identifier: Apache-2.0
23+
# Copyright (c) OWASP Foundation. All Rights Reserved.
24+
#
25+
26+
module Cyclonedx
27+
# Shared utility for safe field access from Hash or OpenStruct-like objects
28+
module FieldAccessor
29+
module_function
30+
31+
# Safe accessor for Hash or OpenStruct-like objects
32+
def _get(obj, key)
33+
if obj.respond_to?(:[]) && obj[key]
34+
obj[key]
35+
elsif obj.respond_to?(key)
36+
obj.public_send(key)
37+
end
38+
end
39+
end
40+
end
41+

lib/cyclonedx/ruby.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
# This gem
1616
require_relative 'ruby/version'
17-
require_relative 'bom_component' # no dependencies
18-
require_relative 'bom_helpers' # depends on bom_component
17+
require_relative 'field_accessor' # shared utility with no dependencies
18+
require_relative 'bom_component' # depends on field_accessor
19+
require_relative 'bom_helpers' # depends on field_accessor and bom_component
1920
require_relative 'bom_builder' # depends on bom_helpers
2021

2122
module Cyclonedx

0 commit comments

Comments
 (0)