Module: SugarHigh::FileExt::ClassMethods

Included in:
File
Defined in:
lib/sugar-high/file_ext.rb

Instance Method Summary collapse

Instance Method Details

#blank?(file_name) ⇒ Boolean

Returns:

Raises:

  • (ArgumentError)


4
5
6
7
8
# File 'lib/sugar-high/file_ext.rb', line 4

def blank? file_name
  raise ArgumentError, "Filename argument must not be blank" if file_name.blank?
  raise ArgumentError, "There is no file at: #{file_name}" if !File.file?(file_name)
  File.zero?(file_name)
end

#has_content?(file_name, content_matcher, &block) ⇒ Boolean

Returns:



10
11
12
13
# File 'lib/sugar-high/file_ext.rb', line 10

def has_content? file_name, content_matcher, &block
  file = get_file file_name
  file.has_content? content_matcher, &block
end

#read_from(file_name, options = {}) {|content| ... } ⇒ Object Also known as: read_content_from, with_content_from

Yields:

  • (content)

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sugar-high/file_ext.rb', line 15

def read_from file_name, options = {}, &block
  raise ArgumentError, "File to read from not found or not a file: #{file_name}" if !File.file? file_name
  content = File.read file_name

  if options[:before]
    begin
      regexp = options[:before].to_regexp
      index = content.match(regexp).offset_before
      content = content[0..index]
    rescue
      raise ArgumentError, ":before option must be a string or regular expression, was : #{options[:before]}"
    end
  end

  if options[:after]
    begin
      regexp = options[:after].to_regexp
      index = content.match(regexp).offset_after
      content = content[index..-1]
    rescue
      raise ArgumentError, ":after option must be a string or regular expression, was : #{options[:after]}"
    end
  end
  yield content if block
  content
end