Module: FailFast::FileExists

Defined in:
lib/fail_fast/extensions/file_exists.rb

Instance Method Summary collapse

Instance Method Details

#file_exists(path, options = {}) ⇒ Object

Ensure the value is an existing file

Usage

file_exists '~/.bash_profile'
file_exists '~/.bash_profile', :message => 'custom message'


10
11
12
13
14
15
16
# File 'lib/fail_fast/extensions/file_exists.rb', line 10

def file_exists(path, options={})
  success = File.exists?(path) && File.file?(path)
  unless success
    add_error ErrorDetails.new(nil, :file_not_found, path, options[:message])
  end
  success
end

#file_exists_for(raw_key, *params) ⇒ Object

Ensure the key value is an existing file exists

Usage

file_exists_for 'foo/config/app.yml'
file_exists_for 'foo/config/app.yml', :message => 'custom message'


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fail_fast/extensions/file_exists.rb', line 24

def file_exists_for(raw_key, *params)
  p = key_value_regexp_options(raw_key, params)
  key, options = p.key, p.options

  return false unless has_value_for raw_key, :message => options[:message]

  path = value_for_deep_key(key)
  success = File.exists?(path) && File.file?(path)
  unless success
    add_error ErrorDetails.new(key, :file_not_found, p.value, options[:message])
  end
  success
end