Class: Onboard::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/onboard/find.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(needle, haystack) ⇒ Finder

Returns a new instance of Finder.



9
10
11
12
# File 'lib/onboard/find.rb', line 9

def initialize(needle, haystack)
  @needle = needle
  @haystack = haystack
end

Instance Attribute Details

#haystackObject (readonly)

Returns the value of attribute haystack.



7
8
9
# File 'lib/onboard/find.rb', line 7

def haystack
  @haystack
end

#needleObject (readonly)

Returns the value of attribute needle.



7
8
9
# File 'lib/onboard/find.rb', line 7

def needle
  @needle
end

Instance Method Details

#info_ext?(file) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/onboard/find.rb', line 32

def info_ext?(file)
  File.extname(file) == '.info'
end

#info_file(dir) ⇒ Object



25
26
27
28
29
30
# File 'lib/onboard/find.rb', line 25

def info_file(dir)
  Find.find(dir).select do |f|
    next unless File.file?(f)
    return f if info_ext?(f)
  end
end

#locateObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/onboard/find.rb', line 14

def locate
  found = {}
  Find.find(haystack) do |e|
    next unless File.directory?(e)
    next unless needle.include?(File.basename(e))
    file = info_file(e)
    found[e] = version(file)
  end
  found
end

#version(file) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/onboard/find.rb', line 36

def version(file)
  File.open(file) do |g|
    g.each_line do |line|
      if line =~ /version/
        return line.scan(/.*?"(.*?)".*$/)[0].nil? ? false : line.scan(/.*?"(.*?)".*$/)[0][0]
      end
    end
  end
end