Class: EnhanceRepo::ArrayArg

Inherits:
Object
  • Object
show all
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

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

#eachObject

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?
  expanded.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 expand_file(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

#expandedObject

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 expanded
  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 += expand_file(element)
    else
      ret << element
    end
  end
  @expanded_cache = ret
  ret
end