Class: Dependabot::DependencyRequirement
- Inherits:
-
Hash
- Object
- Hash
- Dependabot::DependencyRequirement
- 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, Object], 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::Hash[T.any(Symbol, String), T.anything] }
- K =
type_member { { fixed: Symbol } }
- V =
type_member { { fixed: Object } }
- Elem =
type_member { { fixed: [Symbol, Object] } }
Class Method Summary collapse
Instance Method Summary collapse
- #file ⇒ Object
- #groups ⇒ Object
- #metadata ⇒ Object
- #metadata_boolean(key) ⇒ Object
- #metadata_string(key) ⇒ Object
- #metadata_string_hash(key) ⇒ Object
- #metadata_symbol(key) ⇒ Object
- #requirement ⇒ Object
- #requirement_string ⇒ Object
- #source ⇒ Object
- #source_hash ⇒ Object
- #source_string(key) ⇒ Object
Class Method Details
.create(hash) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dependabot/dependency_requirement.rb', line 50 def self.create(hash) requirement = new hash.each do |key, value| case value when Object then requirement[key.to_sym] = value else raise TypeError, "requirement values must inherit Object" end end requirement end |
Instance Method Details
#file ⇒ Object
75 76 77 |
# File 'lib/dependabot/dependency_requirement.rb', line 75 def file optional_string(:file) end |
#groups ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dependabot/dependency_requirement.rb', line 85 def groups value = self[:groups] 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 |
#metadata ⇒ Object
115 116 117 |
# File 'lib/dependabot/dependency_requirement.rb', line 115 def optional_object_hash(:metadata) end |
#metadata_boolean(key) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/dependabot/dependency_requirement.rb', line 181 def (key) value = (key) return if value.nil? return value if value == true || value == false raise TypeError, "metadata #{key} must be a boolean or nil" end |
#metadata_string(key) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/dependabot/dependency_requirement.rb', line 161 def (key) value = (key) return if value.nil? return value if value.is_a?(String) raise TypeError, "metadata #{key} must be a string or nil" end |
#metadata_string_hash(key) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/dependabot/dependency_requirement.rb', line 194 def (key) value = (key) return if value.nil? raise TypeError, "metadata #{key} must be a hash of strings or nil" unless value.is_a?(Hash) value.to_h do |raw_entry_key, raw_entry_value| entry_key = T.cast(raw_entry_key, Object) entry_value = T.cast(raw_entry_value, Object) unless (entry_key.is_a?(Symbol) || entry_key.is_a?(String)) && entry_value.is_a?(String) raise TypeError, "metadata #{key} must be a hash of strings or nil" end [entry_key.to_sym, entry_value] end end |
#metadata_symbol(key) ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/dependabot/dependency_requirement.rb', line 171 def (key) value = (key) return if value.nil? return value if value.is_a?(Symbol) raise TypeError, "metadata #{key} must be a symbol or nil" end |
#requirement ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/dependabot/dependency_requirement.rb', line 65 def requirement value = self[:requirement] return if value.nil? return value if value.is_a?(String) || value == :unfixable raise TypeError, "requirement must be a string, :unfixable, or nil" end |
#requirement_string ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/dependabot/dependency_requirement.rb', line 149 def requirement_string value = requirement return if value.nil? return value if value.is_a?(String) raise TypeError, "requirement must be a string or nil" end |
#source ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/dependabot/dependency_requirement.rb', line 104 def source value = self[:source] return if value.nil? return value if value.is_a?(String) object_hash(value, :source) end |
#source_hash ⇒ Object
122 123 124 125 126 127 |
# File 'lib/dependabot/dependency_requirement.rb', line 122 def source_hash value = source return if value.nil? || value.is_a?(String) value end |
#source_string(key) ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dependabot/dependency_requirement.rb', line 134 def source_string(key) details = source_hash return if details.nil? value = details[key.to_sym] || details[key] return if value.nil? return value if value.is_a?(String) raise TypeError, "source #{key} must be a string or nil" end |