Class: Composer::Repository::HashRepository

Inherits:
BaseRepository show all
Defined in:
lib/composer/repository/hash_repository.rb

Direct Known Subclasses

WritableHashRepository

Constant Summary

Constants inherited from BaseRepository

BaseRepository::SEARCH_FULLTEXT, BaseRepository::SEARCH_NAME

Instance Method Summary collapse

Constructor Details

#initialize(packages = []) ⇒ HashRepository

Returns a new instance of HashRepository.



15
16
17
18
19
# File 'lib/composer/repository/hash_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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/composer/repository/hash_repository.rb', line 112

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

#countObject



169
170
171
# File 'lib/composer/repository/hash_repository.rb', line 169

def count
  @packages.length
end

#find_package(name, version = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/composer/repository/hash_repository.rb', line 21

def find_package(name, version = nil)
  # normalize name
  name = name.downcase

  # normalize version
  if !version.nil?
    version_parser = Composer::Package::Version::VersionParser.new
    version = version_parser.normalize(version)
  end

  match = nil
  packages.each do |package|
    if package.name === name
      if version.nil? || package.version === version
        match = package
        break
      end
    end
  end
  match
end

#find_packages(name, version = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/composer/repository/hash_repository.rb', line 43

def find_packages(name, version = nil)
    # normalize name
    name = name.downcase

    # normalize version
    if version != nil
      version_parser = Composer::Package::Version::VersionParser.new
      version = version_parser.normalize(version)
    end

    matches = []
    packages.each do |package|
      if package.name === name && (nil === version || version === package.version)
        matches.push(package)
      end
    end
    matches
end

#package?(package) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/composer/repository/hash_repository.rb', line 89

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

#packagesObject



164
165
166
167
# File 'lib/composer/repository/hash_repository.rb', line 164

def packages
  initialize_repository unless @packages
  @packages
end

#remove_package(package) ⇒ Object

Removes package from repository.

Params: package package instance to remove



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/composer/repository/hash_repository.rb', line 141

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, mode = 0) ⇒ Object



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
87
# File 'lib/composer/repository/hash_repository.rb', line 62

def search(query, mode = 0)
  regex = /(?:#{query.split(/\s+/).join('|')})/i
  matches = {}
  packages.each do |package|
    name = package.name

    # already matched
    next if matches[name]

    # search
    unless regex.match(name)
      unless mode === Composer::Repository::BaseRepository::SEARCH_FULLTEXT &&
          package.instance_of?(Composer::Package::CompletePackage) &&
          regex.match("#{package.keywords ? package.keywords.join(' ') : ''} #{package.description ? package.description : ''}")
        next
      end
    end

    matches[name] = {
      'name' => package.pretty_name,
      'description' => package.description,
    }

  end
  matches.values
end