Class: ComplyanceSDK::Models::SourceRef

Inherits:
Object
  • Object
show all
Defined in:
lib/complyance_sdk/models/source_ref.rb

Overview

Reference to a source using name and version (simplified identity) This replaces the need for source ID bookkeeping on the client side

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, type = :first_party) ⇒ SourceRef

Initialize a new source reference

Parameters:

  • name (String)

    The source name

  • version (String)

    The source version

  • type (Symbol, String) (defaults to: :first_party)

    The source type (defaults to :first_party)

Raises:

  • (ArgumentError)

    If name or version is blank



16
17
18
19
20
21
# File 'lib/complyance_sdk/models/source_ref.rb', line 16

def initialize(name, version, type = :first_party)
  # Allow empty values for mapping purposes
  @name = name.nil? ? '' : name.to_s.strip
  @version = version.nil? ? '' : version.to_s.strip
  @type = type
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/complyance_sdk/models/source_ref.rb', line 8

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/complyance_sdk/models/source_ref.rb', line 8

def type
  @type
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/complyance_sdk/models/source_ref.rb', line 8

def version
  @version
end

Class Method Details

.from_h(hash) ⇒ SourceRef

Create from hash

Parameters:

  • hash (Hash)

    Hash with name and version keys

Returns:

  • (SourceRef)

    New source reference instance



61
62
63
# File 'lib/complyance_sdk/models/source_ref.rb', line 61

def self.from_h(hash)
  new(hash[:name] || hash['name'], hash[:version] || hash['version'])
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Equality comparison

Parameters:

  • other (SourceRef)

    Other source reference to compare

Returns:

  • (Boolean)

    True if equal



83
84
85
86
87
# File 'lib/complyance_sdk/models/source_ref.rb', line 83

def ==(other)
  return false unless other.is_a?(SourceRef)
  
  @name == other.name && @version == other.version
end

#hashInteger

Hash code for equality

Returns:

  • (Integer)

    Hash code



92
93
94
# File 'lib/complyance_sdk/models/source_ref.rb', line 92

def hash
  [@name, @version].hash
end

#identityString

Get the identity string (name:version)

Returns:

  • (String)

    The identity string



26
27
28
29
30
31
32
# File 'lib/complyance_sdk/models/source_ref.rb', line 26

def identity
  if @name.empty? && @version.empty?
    "mapping:unknown"
  else
    "#{@name}:#{@version}"
  end
end

#inspectString

Inspect representation

Returns:

  • (String)

    Inspect representation



75
76
77
# File 'lib/complyance_sdk/models/source_ref.rb', line 75

def inspect
  "#<#{self.class.name}:#{object_id} name=#{@name.inspect} version=#{@version.inspect}>"
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)

    Hash representation of the source reference



37
38
39
40
41
42
43
44
45
# File 'lib/complyance_sdk/models/source_ref.rb', line 37

def to_h
  {
    name: @name,
    version: @version,
    type: @type.to_s.upcase,
    identity: identity,
    id: identity
  }
end

#to_json_hashHash

Convert to JSON-compatible hash (for API requests)

Returns:

  • (Hash)

    JSON-compatible hash



50
51
52
53
54
55
# File 'lib/complyance_sdk/models/source_ref.rb', line 50

def to_json_hash
  {
    'name' => @name,
    'version' => @version
  }
end

#to_sString

String representation

Returns:

  • (String)

    String representation



68
69
70
# File 'lib/complyance_sdk/models/source_ref.rb', line 68

def to_s
  identity
end

#valid?Boolean

Validate the source reference

Returns:

  • (Boolean)

    True if valid



102
103
104
# File 'lib/complyance_sdk/models/source_ref.rb', line 102

def valid?
  !@name.empty? && !@version.empty?
end

#validate!Object

Check if source reference is valid, raise error if not



119
120
121
122
123
124
125
126
127
# File 'lib/complyance_sdk/models/source_ref.rb', line 119

def validate!
  return true if valid?
  
  error_message = validation_errors.join(', ')
  raise ComplyanceSDK::Exceptions::ValidationError.new(
    "Invalid source reference: #{error_message}",
    context: { name: @name, version: @version }
  )
end

#validation_errorsArray<String>

Get validation errors

Returns:

  • (Array<String>)

    Array of validation error messages



109
110
111
112
113
114
# File 'lib/complyance_sdk/models/source_ref.rb', line 109

def validation_errors
  errors = []
  errors << 'Name cannot be blank' if @name.empty?
  errors << 'Version cannot be blank' if @version.empty?
  errors
end