forked from cloudfoundry/java-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_enumerable.rb
More file actions
72 lines (63 loc) · 3.2 KB
/
Copy pathfile_enumerable.rb
File metadata and controls
72 lines (63 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Encoding: utf-8
# Cloud Foundry Java Buildpack
# Copyright 2013-2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'java_buildpack/util'
module JavaBuildpack
module Util
# Passes the open file descriptor for each candidate file to the given block. If opening or reading the file causes
# an error, iteration will continue, and the failing element will be assumed to have returned +true+ (i.e. failure
# will not affect the net result of the successful elements).
#
# @param [Array<Pathname>] candidates the candidate files to iterate
# @return [Boolean] +true+ if the block never returns +false+ or +nil+, otherwise +false+.
def all?(candidates, &block)
candidates.all? { |candidate| open(true, candidate, &block) }
end
# Passes the open file descriptor for each candidate file to the given block. If opening or reading the file causes
# an error, iteration will continue, and the failing element will be assumed to have returned +false+ (i.e. failure
# will not affect the net result of the successful elements).
#
# @param [Array<Pathname>] candidates the candidate files to iterate
# @return [Boolean] +true+ if the block always returns +false+ or +nil+, otherwise +false+.
def none?(candidates, &block)
candidates.none? { |candidate| open(false, candidate, &block) }
end
# Passes the open file descriptor for each candidate file to the given block. If opening or reading the file causes
# an error, iteration will continue, and the failing element will be assumed to have returned +true+ (i.e. failure
# will not affect the net result of the successful elements).
#
# @param [Array<Pathname>] candidates the candidate files to iterate
# @return [Array<Pathname>] the candidates for which the block returned +false+ or +nil+
def reject(candidates, &block)
candidates.reject { |candidate| open(true, candidate, &block) }
end
# Passes the open file descriptor for each candidate file to the given block. If opening or reading the file causes
# an error, iteration will continue, and the failing element will be assumed to have returned +false+ (i.e. failure
# will not affect the net result of the successful elements).
#
# @param [Array<Pathname>] candidates the candidate files to iterate
# @return [Array<Pathname>] the candidates for which the block returned +true+
def select(candidates, &block)
candidates.select { |candidate| open(false, candidate, &block) }
end
private
def open(default, candidate, &block)
candidate.open('r', external_encoding: 'UTF-8', &block)
rescue => e
@logger.warn e.message
default
end
end
end