Module: SugarHigh::FileExt::ClassMethods
- Included in:
- File
- Defined in:
- lib/sugar-high/file_ext.rb
Instance Method Summary collapse
- #blank?(file_name) ⇒ Boolean
- #has_content?(file_name, content_matcher, &block) ⇒ Boolean
- #read_from(file_name, options = {}) {|content| ... } ⇒ Object (also: #read_content_from, #with_content_from)
Instance Method Details
#blank?(file_name) ⇒ Boolean
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
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
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, = {}, &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 [:before] begin regexp = [: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 : #{[:before]}" end end if [:after] begin regexp = [: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 : #{[:after]}" end end yield content if block content end |