Class: EnhanceRepo::ArrayArg
- Inherits:
-
Object
- Object
- EnhanceRepo::ArrayArg
- Includes:
- Enumerable
- Defined in:
- lib/enhance_repo/array_arg.rb
Overview
Helper class to turn an array like:
- ‘a’, ‘b’, ‘/foo/file.txt’, ‘d’
-
into an
array containing the content of file.txt (each line) merged into the place of the file name
Instance Method Summary collapse
-
#each ⇒ Object
see Enumerable.
-
#expand_file(file) ⇒ Object
yields one element per line in the file.
-
#expanded ⇒ Object
expand the wrapped array with fles.
-
#initialize(array) ⇒ ArrayArg
constructor
initialize the wrapper with an array.
-
#method_missing(method, *args) ⇒ Object
delegate other methods to the expanded array.
Constructor Details
#initialize(array) ⇒ ArrayArg
initialize the wrapper with an array
38 39 40 41 42 |
# File 'lib/enhance_repo/array_arg.rb', line 38 def initialize(array) @array = array @array ||= [] @expanded_cache = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
delegate other methods to the expanded array
74 75 76 |
# File 'lib/enhance_repo/array_arg.rb', line 74 def method_missing(method, *args) each.to_a.send(method, *args) end |
Instance Method Details
#each ⇒ Object
see Enumerable
79 80 81 82 83 |
# File 'lib/enhance_repo/array_arg.rb', line 79 def each return enum_for(:each) unless block_given? .each { |x| yield x } self end |
#expand_file(file) ⇒ Object
yields one element per line in the file
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/enhance_repo/array_arg.rb', line 46 def (file) ret = [] File.open(file) do |f| f.each_line do |line| stripped_line = line.strip ret << stripped_line unless stripped_line.empty? end end ret end |
#expanded ⇒ Object
expand the wrapped array with fles
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/enhance_repo/array_arg.rb', line 58 def return @expanded_cache if @expanded_cache ret = [] @array.each do |element| if File.exist?(element) && !File.directory?(element) EnhanceRepo.logger.info "Expanding the content of file '#{element}'..." ret += (element) else ret << element end end @expanded_cache = ret ret end |