Class: Dependabot::DependencyRequirement

Inherits:
Hash
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/dependabot/dependency_requirement.rb

Overview

A single requirement entry within Dependency#requirements, e.g.:

{
requirement: ">= 1.0, < 2.0",
file: "Gemfile",
groups: [:default],
source: { type: "rubygems", url: "https://rubygems.org" },
metadata: { property_name: "rails.version" } # optional
}

Subclasses Hash so it is a drop-in replacement at call sites (and in type annotations) that treat requirement entries as T::Hash[Symbol, T.untyped], while exposing typed readers for the well-known keys. New code should prefer the typed readers; hash-style access remains supported while call sites are migrated gradually.

Wire compatibility: instances serialise to JSON exactly like the plain hash they were created from, and compare equal (==/eql?/#hash) to plain hashes with the same content, so existing comparisons, Array/Set operations, and API payloads are unaffected.

Note on Hash methods: in Ruby 3+, #merge, #dup and #compact preserve this class, while #select, #reject, #except, #transform_values and #to_h return plain Hash instances. Dependency#initialize re-wraps whatever it is given, so both styles remain safe.

Constant Summary collapse

Group =
T.type_alias { T.any(String, Symbol) }
ObjectHash =
T.type_alias { T::Hash[T.any(Symbol, String), Object] }
Requirement =
T.type_alias { T.any(String, Symbol) }
Source =
T.type_alias { T.any(String, ObjectHash) }
Input =
T.type_alias { T.any(DependencyRequirement, ObjectHash) }
K =
type_member { { fixed: Symbol } }
V =

Hash-style access remains dynamic until ecosystem callers migrate to the typed readers. Keeping that compatibility here avoids a flag-day change. rubocop:disable Sorbet/ForbidTUntyped

type_member { { fixed: T.untyped } }
Elem =
type_member { { fixed: [Symbol, T.untyped] } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(hash) ⇒ Object



54
55
56
57
58
# File 'lib/dependabot/dependency_requirement.rb', line 54

def self.create(hash)
  requirement = new
  requirement.replace(hash.keys.to_h { |k| [k.to_sym, hash[k]] })
  requirement
end

Instance Method Details

#fileObject



74
75
76
# File 'lib/dependabot/dependency_requirement.rb', line 74

def file
  optional_string(:file)
end

#groupsObject

Raises:

  • (TypeError)


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dependabot/dependency_requirement.rb', line 84

def groups
  value = T.cast(self[:groups], T.nilable(Object))
  return if value.nil?
  raise TypeError, "groups must be an array of strings or symbols, or nil" unless value.is_a?(Array)

  value.each do |raw_group|
    group = T.cast(raw_group, Object)
    next if group.is_a?(String) || group.is_a?(Symbol)

    raise TypeError, "groups must be an array of strings or symbols, or nil"
  end
  value
end

#metadataObject



114
115
116
# File 'lib/dependabot/dependency_requirement.rb', line 114

def 
  optional_object_hash(:metadata)
end

#requirementObject

Raises:

  • (TypeError)


64
65
66
67
68
69
70
# File 'lib/dependabot/dependency_requirement.rb', line 64

def requirement
  value = T.cast(self[:requirement], T.nilable(Object))
  return if value.nil?
  return value if value.is_a?(String) || value == :unfixable

  raise TypeError, "requirement must be a string, :unfixable, or nil"
end

#sourceObject



103
104
105
106
107
108
109
# File 'lib/dependabot/dependency_requirement.rb', line 103

def source
  value = T.cast(self[:source], T.nilable(Object))
  return if value.nil?
  return value if value.is_a?(String)

  object_hash(value, :source)
end