Class: ProjectFile
- Inherits:
-
Object
- Object
- ProjectFile
- Includes:
- Comparable
- Defined in:
- lib/header-inserter/project_file.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#version_control ⇒ Object
readonly
Returns the value of attribute version_control.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #absolute_path ⇒ Object
- #add_header(header, old_header = nil) ⇒ Object
- #contributors ⇒ Object
- #created_on ⇒ Object
- #generate_header(header, hooks) ⇒ Object
-
#initialize(project, path, version_control = NilVersionControl.new) ⇒ ProjectFile
constructor
A new instance of ProjectFile.
- #original_header ⇒ Object
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
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/header-inserter/project_file.rb', line 9 def path @path end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
9 10 11 |
# File 'lib/header-inserter/project_file.rb', line 9 def project @project end |
#version_control ⇒ Object (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_path ⇒ Object
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 |
#contributors ⇒ Object
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..strip }.uniq end |
#created_on ⇒ Object
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_header ⇒ Object
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 |