Class: Yamlook::File

Inherits:
Object
  • Object
show all
Defined in:
lib/yamlook/file.rb

Overview

Yaml file to perform search

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ File

Returns a new instance of File.



8
9
10
# File 'lib/yamlook/file.rb', line 8

def initialize(filename)
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/yamlook/file.rb', line 6

def filename
  @filename
end

Instance Method Details

#search(keys) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yamlook/file.rb', line 12

def search(keys)
  file = ::File.read(filename)
  handler = Handler.new(keys: keys, locales: LOCALES)
  parser = ::Psych::Parser.new(handler)

  findings = parser.parse(file).handler.found.map do |value, line, column|
    "#{filename}:#{line}:#{column}\n#{value}"
  end

  findings if findings.any?
rescue ::Psych::Exception
  nil
end