Best Rr_ruby code snippet using Gem.from_gems_in
source_index.rb
Source:source_index.rb
...31 attr_accessor :spec_dirs32 class << self33 # Undef old methods34 %w(from_installed_gems installed_spec_directories35 from_gems_in load_specification).each do |meth|36 if instance_methods(true).find {|m| m.to_s == meth }37 undef_method(meth)38 end39 end40 ##41 # Factory method to construct a source index instance for a given42 # path.43 #44 # deprecated::45 # If supplied, from_installed_gems will act just like46 # +from_gems_in+. This argument is deprecated and is provided47 # just for backwards compatibility, and should not generally48 # be used.49 #50 # return::51 # SourceIndex instance52 def from_installed_gems(*deprecated)53 if deprecated.empty?54 from_gems_in(*installed_spec_directories)55 else56 from_gems_in(*deprecated) # HACK warn57 end58 end59 ##60 # Returns a list of directories from Gem.path that contain specifications.61 def installed_spec_directories62 Gem.path.collect { |dir| File.join(dir, "specifications") }63 end64 ##65 # Creates a new SourceIndex from the ruby format gem specifications in66 # +spec_dirs+.67 def from_gems_in(*spec_dirs)68 source_index = new69 source_index.spec_dirs = spec_dirs70 source_index.refresh!71 end72 ##73 # Loads a ruby-format specification from +file_name+ and returns the74 # loaded spec.75 def load_specification(file_name)76 Gem::Specification.load file_name77 end78 end79 ##80 # Constructs a source index instance from the provided specifications, which81 # is a Hash of gem full names and Gem::Specifications.82 #--83 # TODO merge @gems and @prerelease_gems and provide a separate method84 # #prerelease_gems85 def initialize(specifications={})86 @gems = {}87 specifications.each{ |full_name, spec| add_spec spec }88 @spec_dirs = nil89 end90 # TODO: remove method91 def all_gems92 @gems93 end94 def prerelease_gems95 @gems.reject{ |name, gem| !gem.version.prerelease? }96 end97 def released_gems98 @gems.reject{ |name, gem| gem.version.prerelease? }99 end100 ##101 # Reconstruct the source index from the specifications in +spec_dirs+.102 def load_gems_in(*spec_dirs)103 @gems.clear104 spec_dirs.reverse_each do |spec_dir|105 spec_files = Dir.glob File.join(spec_dir, '*.gemspec')106 spec_files.each do |spec_file|107 gemspec = Gem::Specification.load spec_file108 add_spec gemspec if gemspec109 end110 end111 self112 end113 ##114 # Returns an Array specifications for the latest released versions115 # of each gem in this index.116 def latest_specs(include_prerelease=false)117 result = Hash.new { |h,k| h[k] = [] }118 latest = {}119 sort.each do |_, spec|120 name = spec.name121 curr_ver = spec.version122 prev_ver = latest.key?(name) ? latest[name].version : nil123 next if !include_prerelease && curr_ver.prerelease?124 next unless prev_ver.nil? or curr_ver >= prev_ver or125 latest[name].platform != Gem::Platform::RUBY126 if prev_ver.nil? or127 (curr_ver > prev_ver and spec.platform == Gem::Platform::RUBY) then128 result[name].clear129 latest[name] = spec130 end131 if spec.platform != Gem::Platform::RUBY then132 result[name].delete_if do |result_spec|133 result_spec.platform == spec.platform134 end135 end136 result[name] << spec137 end138 # TODO: why is this a hash while @gems is an array? Seems like139 # structural similarity would be good.140 result.values.flatten141 end142 ##143 # An array including only the prerelease gemspecs144 def prerelease_specs145 prerelease_gems.values146 end147 ##148 # An array including only the released gemspecs149 def released_specs150 released_gems.values151 end152 ##153 # Add a gem specification to the source index.154 def add_spec(gem_spec, name = gem_spec.full_name)155 # No idea why, but the Indexer wants to insert them using original_name156 # instead of full_name. So we make it an optional arg.157 @gems[name] = gem_spec158 end159 ##160 # Add gem specifications to the source index.161 def add_specs(*gem_specs)162 gem_specs.each do |spec|163 add_spec spec164 end165 end166 ##167 # Remove a gem specification named +full_name+.168 def remove_spec(full_name)169 @gems.delete full_name170 end171 ##172 # Iterate over the specifications in the source index.173 def each(&block) # :yields: gem.full_name, gem174 @gems.each(&block)175 end176 ##177 # The gem specification given a full gem spec name.178 def specification(full_name)179 @gems[full_name]180 end181 ##182 # The signature for the source index. Changes in the signature indicate a183 # change in the index.184 def index_signature185 require 'digest'186 Digest::SHA256.new.hexdigest(@gems.keys.sort.join(',')).to_s187 end188 ##189 # The signature for the given gem specification.190 def gem_signature(gem_full_name)191 require 'digest'192 Digest::SHA256.new.hexdigest(@gems[gem_full_name].to_yaml).to_s193 end194 def size195 @gems.size196 end197 alias length size198 ##199 # Find a gem by an exact match on the short name.200 def find_name(gem_name, requirement = Gem::Requirement.default)201 dep = Gem::Dependency.new gem_name, requirement202 search dep203 end204 ##205 # Search for a gem by Gem::Dependency +gem_pattern+. If +only_platform+206 # is true, only gems matching Gem::Platform.local will be returned. An207 # Array of matching Gem::Specification objects is returned.208 #209 # For backwards compatibility, a String or Regexp pattern may be passed as210 # +gem_pattern+, and a Gem::Requirement for +platform_only+. This211 # behavior is deprecated and will be removed.212 def search(gem_pattern, platform_only = false)213 requirement = nil214 only_platform = false215 # TODO - Remove support and warning for legacy arguments after 2008/11216 unless Gem::Dependency === gem_pattern217 warn "#{Gem.location_of_caller.join ':'}:Warning: Gem::SourceIndex#search support for #{gem_pattern.class} patterns is deprecated, use #find_name"218 end219 case gem_pattern220 when Regexp then221 requirement = platform_only || Gem::Requirement.default222 when Gem::Dependency then223 only_platform = platform_only224 requirement = gem_pattern.requirement225 gem_pattern = if Regexp === gem_pattern.name then226 gem_pattern.name227 elsif gem_pattern.name.empty? then228 //229 else230 /^#{Regexp.escape gem_pattern.name}$/231 end232 else233 requirement = platform_only || Gem::Requirement.default234 gem_pattern = /#{gem_pattern}/i235 end236 unless Gem::Requirement === requirement then237 requirement = Gem::Requirement.create requirement238 end239 specs = all_gems.values.select do |spec|240 spec.name =~ gem_pattern and241 requirement.satisfied_by? spec.version242 end243 if only_platform then244 specs = specs.select do |spec|245 Gem::Platform.match spec.platform246 end247 end248 specs.sort_by { |s| s.sort_obj }249 end250 ##251 # Replaces the gems in the source index from specifications in the252 # directories this source index was created from. Raises an exception if253 # this source index wasn't created from a directory (via from_gems_in or254 # from_installed_gems, or having spec_dirs set).255 def refresh!256 raise 'source index not created from disk' if @spec_dirs.nil?257 load_gems_in(*@spec_dirs)258 end259 ##260 # Returns an Array of Gem::Specifications that are not up to date.261 def outdated262 outdateds = []263 latest_specs.each do |local|264 dependency = Gem::Dependency.new local.name, ">= #{local.version}"265 fetcher = Gem::SpecFetcher.fetcher266 remotes = fetcher.find_matching dependency267 remotes = remotes.map { |(_, version, _), _| version }...
from_gems_in
Using AI Code Generation
1def from_gems_in(gems_dir)2 if File.exist?(gems_dir)3 gems_dir = File.expand_path(gems_dir)4 Gem.path.unshift(gems_dir)5def install_gems(gems)6 installer = Gem::DependencyInstaller.new(:version_requirements => '>= 0')7 installer.install(gem)8from_gems_in('gems')9install_gems(['sinatra', 'sinatra-contrib'])
from_gems_in
Using AI Code Generation
1 def self.use_ui(ui)2 def self.use_ui(ui)3 def self.use_ui(ui)4 def self.use_ui(ui)5 def self.use_ui(ui)6 def self.use_ui(ui)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!