Class: Tracing::FileTarget

Inherits:
Object show all
Extended by:
DefaultPath
Includes:
DefaultPath
Defined in:
lib/targets/file_target.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultPath

default_path, default_path=

Constructor Details

#initialize(options) ⇒ FileTarget

Returns a new instance of FileTarget.



11
12
13
14
15
16
17
# File 'lib/targets/file_target.rb', line 11

def initialize(options)
  def_path = options[:default_path]
  _to_file = options[:to_file]

  @default_path = def_path if def_path
  @to_file = _to_file if _to_file
end

Instance Attribute Details

#to_fileObject

Returns the value of attribute to_file.



5
6
7
# File 'lib/targets/file_target.rb', line 5

def to_file
  @to_file
end

Instance Method Details

#append(txt, context) ⇒ Object



19
20
21
22
# File 'lib/targets/file_target.rb', line 19

def append(txt, context)
  file = get_file(context)
  write_file(file, txt)
end

#create_initial_file(file) ⇒ Object



34
35
# File 'lib/targets/file_target.rb', line 34

def create_initial_file(file)
end

#file_path(file) ⇒ Object



56
57
58
59
# File 'lib/targets/file_target.rb', line 56

def file_path(file)
  def_path = default_path || self.class.default_path
  file = File.join(def_path, file) if self.class.default_path      
end

#insert_into_file(file, txt, marker_txt) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/targets/file_target.rb', line 37

def insert_into_file(file, txt, marker_txt)
  line = marker_txt

  file = file_path(file)

  if !File.exist?(file)
    create_initial_file(file)
  end  
  
  gsub_file file, /(#{Regexp.escape(line)})/mi do |match|
    "#{txt}\n#{match}"
  end
end

#is_old_file?(file) ⇒ Boolean

helper for deciding file “write mode”

Returns:

  • (Boolean)


52
53
54
# File 'lib/targets/file_target.rb', line 52

def is_old_file?(file)
  File.new(file).mtime < (Time.now - time_limit)
end

#write_file(file, txt) ⇒ Object

file



25
26
27
28
29
30
31
32
# File 'lib/targets/file_target.rb', line 25

def write_file(file, txt)
  file = file_path(file)      
  
  write_mode = mode(file) 
  File.open(file, write_mode) do |f|
    f.puts txt
  end   
end