| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  | require "set" | 
					
						
							|  |  |  | require "cache_store" | 
					
						
							|  |  |  | require "searchable" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # `DescriptionCacheStore` provides methods to fetch and mutate linkage-specific data used | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  | # by the `brew linkage` command. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  | # | 
					
						
							|  |  |  | class DescriptionCacheStore < CacheStore | 
					
						
							|  |  |  |   include Searchable | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Inserts a formula description into the cache if it does not exist or | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # updates the formula description if it does exist. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param formula_name [String] the name of the formula to set | 
					
						
							|  |  |  |   # @param description  [String] the description from the formula to set | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def update!(formula_name, description) | 
					
						
							|  |  |  |     database.set(formula_name, description) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # Delete the formula description from the `DescriptionCacheStore`. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param formula_name [String] the name of the formula to delete | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def delete!(formula_name) | 
					
						
							|  |  |  |     database.delete(formula_name) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # If the database is empty `update!` it with all known formulae. | 
					
						
							|  |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def populate_if_empty! | 
					
						
							|  |  |  |     return unless database.empty? | 
					
						
							| 
									
										
										
										
											2018-10-15 22:51:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     Formula.each { |f| update!(f.full_name, f.desc) } | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Use an update report to update the `DescriptionCacheStore`. | 
					
						
							|  |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param report [Report] an update report generated by cmd/update.rb | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def update_from_report!(report) | 
					
						
							| 
									
										
										
										
											2018-10-15 22:51:43 -07:00
										 |  |  |     return populate_if_empty! if database.empty? | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     return if report.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     renamings   = report.select_formula(:R) | 
					
						
							|  |  |  |     alterations = report.select_formula(:A) + | 
					
						
							|  |  |  |                   report.select_formula(:M) + | 
					
						
							|  |  |  |                   renamings.map(&:last) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     update_from_formula_names!(alterations) | 
					
						
							|  |  |  |     delete_from_formula_names!(report.select_formula(:D) + | 
					
						
							|  |  |  |                                renamings.map(&:first)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Use an array of formulae names to update the `DescriptionCacheStore`. | 
					
						
							|  |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param formula_names [Array] the formulae to update | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def update_from_formula_names!(formula_names) | 
					
						
							| 
									
										
										
										
											2018-10-15 22:51:43 -07:00
										 |  |  |     return populate_if_empty! if database.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     formula_names.each do |name| | 
					
						
							| 
									
										
										
										
											2019-10-13 10:03:26 +01:00
										 |  |  |       update!(name, Formula[name].desc) | 
					
						
							|  |  |  |     rescue FormulaUnavailableError, *FormulaVersions::IGNORED_EXCEPTIONS | 
					
						
							|  |  |  |       delete!(name) | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Use an array of formulae names to delete them from the `DescriptionCacheStore`. | 
					
						
							|  |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param formula_names [Array] the formulae to delete | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def delete_from_formula_names!(formula_names) | 
					
						
							| 
									
										
										
										
											2018-10-15 22:51:43 -07:00
										 |  |  |     return if database.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     formula_names.each(&method(:delete!)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Not implemented; access is through `Searchable`. | 
					
						
							|  |  |  |   def fetch | 
					
						
							|  |  |  |     super | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # `select` from the underlying database. | 
					
						
							|  |  |  |   def select(&block) | 
					
						
							|  |  |  |     database.select(&block) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |