Class: ProjectFile

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/header-inserter/project_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, path, version_control = NilVersionControl.new) ⇒ ProjectFile

Returns a new instance of ProjectFile.



11
12
13
14
15
16
17
18
19
# File 'lib/header-inserter/project_file.rb', line 11

def initialize project, path, version_control = NilVersionControl.new
  if project.nil?
    @project = Project.new "/"
  else
    @project = project
  end
  @path = path
  @version_control = version_control
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/header-inserter/project_file.rb', line 9

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



9
10
11
# File 'lib/header-inserter/project_file.rb', line 9

def project
  @project
end

#version_controlObject (readonly)

Returns the value of attribute version_control.



9
10
11
# File 'lib/header-inserter/project_file.rb', line 9

def version_control
  @version_control
end

Instance Method Details

#<=>(other) ⇒ Object



21
22
23
# File 'lib/header-inserter/project_file.rb', line 21

def <=> other
  absolute_path <=> other.absolute_path
end

#absolute_pathObject



25
26
27
28
29
# File 'lib/header-inserter/project_file.rb', line 25

def absolute_path
  absolute = @project.path
  absolute += File::SEPARATOR if @project.path != "/"
  absolute + @path
end

#add_header(header, old_header = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/header-inserter/project_file.rb', line 64

def add_header header, old_header = nil
  content = ""
  file = File.new absolute_path, "r"
  file.each do |line|
    content += line
  end
  file.close
  
  content = remove old_header, content
  
  file = File.new absolute_path, "w"
  file.puts(header + content)
  file.close
end

#contributorsObject



47
48
49
50
# File 'lib/header-inserter/project_file.rb', line 47

def contributors
  return [username.strip] if modifications.empty?
  modifications.map{ |mod| mod.author.strip }.uniq
end

#created_onObject



42
43
44
45
# File 'lib/header-inserter/project_file.rb', line 42

def created_on
  return Date.today if modifications.empty?
  modifications[0].date
end

#generate_header(header, hooks) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/header-inserter/project_file.rb', line 52

def generate_header header, hooks
  generated_header = header
  hooks.keys.each do |key|
    value = hooks[key].call(self)
    if not value.is_a?(String)
      puts "Error while evaluating the hook for #{key}. Returned a #{value.class} valued as '#{value}'. Calling to_s."
    end
    generated_header = generated_header.gsub key.to_s, value.to_s
  end
  generated_header
end

#original_headerObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/header-inserter/project_file.rb', line 31

def original_header
  file = File.new absolute_path, "r"
  header = ""
  while (line = file.readline) and not(/\s*(?:package|class|import)\s*/.match line)
    header += line
  end
  header
rescue Errno::ENOENT
  return nil
end