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) }
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



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

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

Instance Method Details

#fileObject



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

def file
  optional_string(:file)
end

#groupsObject

Raises:

  • (TypeError)


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

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



109
110
111
# File 'lib/dependabot/dependency_requirement.rb', line 109

def 
  optional_object_hash(:metadata)
end

#requirementObject

Raises:

  • (TypeError)


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

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



102
103
104
# File 'lib/dependabot/dependency_requirement.rb', line 102

def source
  optional_object_hash(:source)
end