Class: Buildr::RepositoryArray

Inherits:
Array
  • Object
show all
Defined in:
lib/buildr/packaging/repository_array.rb

Overview

Extention of Array to standardize URLs into { :url => ‘url’, :username => ‘username’, :password => ‘password’ }.

:url = url of remote repository
:username = optional username for authentication
:password = optional password for authentication

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RepositoryArray

Returns a new instance of RepositoryArray.



23
24
25
26
27
28
29
30
31
# File 'lib/buildr/packaging/repository_array.rb', line 23

def initialize( *args )
  if !args.nil? && args.is_a?(Array) && args.size == 1
    args = args[0]
    converted = args.map {|url| convert_remote_url(url) }
    super(converted)
  else
    super(args)
  end
end

Instance Method Details

#+(urls) ⇒ Object

Concatenation—Returns a new array built by concatenating the two arrays together to produce a third array of standardized URL Hashes.



34
35
36
# File 'lib/buildr/packaging/repository_array.rb', line 34

def +(urls)
  concat(urls)      
end

#<<(url) ⇒ Object

Append—Pushes the given object on to the end of this array, as a standardize URL Hash. This expression returns the array itself, so several appends may be chained together.



39
40
41
42
# File 'lib/buildr/packaging/repository_array.rb', line 39

def <<(url)
  converted = convert_remote_url( url )
  super( converted )
end

#concat(urls) ⇒ Object

Appends the elements in other_array to self, as standardized URL Hashes



45
46
47
48
49
50
51
52
# File 'lib/buildr/packaging/repository_array.rb', line 45

def concat(urls)
  if !urls.nil?
    converted = urls.map { |url| convert_remote_url( url ) }
    super(converted)
  else
    super(nil)
  end 
end

#convert_remote_url(url) ⇒ Object

:call-seq:

url = url => hash
url = Hash => hash

With a String url, returns a standardize URL Hash.

With a Hash argument { :url => ‘url’, :username => ‘username’, :password => ‘password’ }, returns a standardize URL Hash.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/buildr/packaging/repository_array.rb', line 62

def convert_remote_url(url)
  if url.is_a? String
    return {:url =>  URI.parse(url)}
  elsif url.is_a? Hash
    hash = { :url =>  URI.parse((url[:url] || url['url'])) }
    username = url[:username] || url['username']
    hash[:username] = username if username
    
    password = (url[:password] || url['password'])
    hash[:password] = password if password
    
    return hash
  elsif url.is_a? URI
    return { :url => url }
  else
    raise ArgumentError, "Unsupported :url, must be String, Hash, or URI: #{url.inspect}"
  end
end

#insert(index, *args) ⇒ Object

Inserts the given values before the element with the given index (which may be negative), as standardized URL Hashes



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/buildr/packaging/repository_array.rb', line 82

def insert( index, *args)
  if !args.nil?
    pos = index
    args.each do |url| 
      convert = convert_remote_url( url )
      super( pos, convert )
      if index >= 0
        pos = pos + 1          
      end
    end
  else
    super( index, nil )  
  end
end

#replace(other_array) ⇒ Object

Replaces the contents of self with the contents of other_array, truncating or expanding if necessary. The contents of other_array is converted standardized URL Hashes.



99
100
101
102
103
104
105
106
# File 'lib/buildr/packaging/repository_array.rb', line 99

def replace( other_array )
  if !other_array.nil?
    converted = other_array.map { |url| convert_remote_url( url ) }
    super( converted )
  else
    super( nil )  
  end
end