Class: Composer::Repository::ArrayRepository
- Inherits:
-
Object
- Object
- Composer::Repository::ArrayRepository
- Defined in:
- lib/composer/repository/array_repository.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#add_package(package) ⇒ Object
Adds a new package to the repository.
- #count ⇒ Object
- #find_package(name, version = nil) ⇒ Object
- #find_packages(name, version = nil) ⇒ Object
-
#initialize(packages = []) ⇒ ArrayRepository
constructor
A new instance of ArrayRepository.
- #package?(package) ⇒ Boolean
- #packages ⇒ Object
-
#remove_package(package) ⇒ Object
Removes package from repository.
- #search(query, full_search = false) ⇒ Object
Constructor Details
#initialize(packages = []) ⇒ ArrayRepository
Returns a new instance of ArrayRepository.
15 16 17 18 19 |
# File 'lib/composer/repository/array_repository.rb', line 15 def initialize(packages = []) packages.each do |package| add_package(package) end end |
Instance Method Details
#add_package(package) ⇒ Object
Adds a new package to the repository
Params: package Package The package to add
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/composer/repository/array_repository.rb', line 111 def add_package(package) unless package raise ArgumentError, 'package must be specified' end unless package.is_a(Composer::Package::BasePackage) raise TypeError, 'package must be a class or superclass of \ Composer::Package::Package' end initialize_repository unless @packages package.repository = self @packages << package if package.instance_of(Composer::Package::AliasPackage) aliased_package = package.alias_of if aliased_package.repository === nil add_package(aliased_package) end end end |
#count ⇒ Object
168 169 170 |
# File 'lib/composer/repository/array_repository.rb', line 168 def count @packages.lenght end |
#find_package(name, version = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/composer/repository/array_repository.rb', line 21 def find_package(name, version = nil) # normalize name name = name.downcase # normalize version if version != nil version_parser = Composer::Package::Verision::VersionParser.new version = version_parser.normalize(version) end packages.each do |package| if package.name === name && (nil === $version || version === package.version) return package end end end |
#find_packages(name, version = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/composer/repository/array_repository.rb', line 39 def find_packages(name, version = nil) # normalize name name = name.downcase # normalize version if version != nil version_parser = Composer::Package::Verision::VersionParser.new version = version_parser.normalize(version) end matches = [] packages.each do |package| if package.name === name && (nil === $version || version === package.version) matches << package end end matches end |
#package?(package) ⇒ Boolean
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/composer/repository/array_repository.rb', line 88 def package?(package) unless package raise ArgumentError, 'package must be specified' end unless package.is_a(Composer::Package::BasePackage) raise TypeError, 'package must be a class or superclass of \ Composer::Package::Package' end package_id = package.unique_name packages.each do |repo_package| return true if repo_package.unique_name === package_id end false end |
#packages ⇒ Object
163 164 165 166 |
# File 'lib/composer/repository/array_repository.rb', line 163 def packages initialize_repository unless @packages @packages end |
#remove_package(package) ⇒ Object
Removes package from repository.
Params: package package instance to remove
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/composer/repository/array_repository.rb', line 140 def remove_package(package) unless package raise ArgumentError, 'package must be specified' end unless package.is_a(Composer::Package::BasePackage) raise TypeError, 'package must be a class or superclass of \ Composer::Package::Package' end package_id = package.unique_name index = 0 packages.each do |repo_package| if repo_package.unique_name === package_id @packages.delete_at(index) return end index = index + 1 end end |
#search(query, full_search = false) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/composer/repository/array_repository.rb', line 58 def search(query, full_search = false) regex = /(?:#{query.split(/\s+/).join('|')})/i matches = {} packages.each do |package| name = package.name # allready matched next if matches['name'] # search if full_search next unless ( package.instance_of(Composer::Package::CompletePackage) && regex.match("#{package.keywords.join(' ')} #{package.description}") ) else next unless ( full_search == false && regex.match(name) ) end matches[name] = { 'name' => package.pretty_name, 'description' => $package.description, } end matches end |