Class: KatanaStamp::StampFile

Inherits:
Object
  • Object
show all
Defined in:
lib/katana_stamp/stamp_file.rb

Constant Summary collapse

ANSI_COLORS =

ANSI Colour code numbers

{
  red: 31,
  green: 32,    
  yellow: 33,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options) ⇒ StampFile

Returns a new instance of StampFile.



4
5
6
7
8
9
10
# File 'lib/katana_stamp/stamp_file.rb', line 4

def initialize(path, options)
  @path              = path      
  @comment_delimiter = options[:comment_delimiter] || '#'
  @year              = options[:year] || Time.now.year
  @owner             = options[:owner] || 'Katana Code Ltd'
  @options_message   = options[:message]
end

Instance Attribute Details

#comment_delimiterObject (readonly)

Comment delimeter used in stamp (defaults to ‘#’)



16
17
18
# File 'lib/katana_stamp/stamp_file.rb', line 16

def comment_delimiter
  @comment_delimiter
end

#ownerObject (readonly)

Copyright owner (defaults to Katana Code Ltd)



22
23
24
# File 'lib/katana_stamp/stamp_file.rb', line 22

def owner
  @owner
end

#pathObject (readonly)

Path to this file



13
14
15
# File 'lib/katana_stamp/stamp_file.rb', line 13

def path
  @path
end

#yearObject (readonly)

Copyright year (defaults to this Year)



19
20
21
# File 'lib/katana_stamp/stamp_file.rb', line 19

def year
  @year
end

Instance Method Details

#existing_ownerObject

The existing Copyright owner or nil



55
56
57
# File 'lib/katana_stamp/stamp_file.rb', line 55

def existing_owner
  existing_stamp.scan(%r{Copyright\s#{year}\s(.+)\.}).flatten.first
end

#existing_stampObject

Does the file already have a copyright stamp?



46
47
48
49
50
51
52
# File 'lib/katana_stamp/stamp_file.rb', line 46

def existing_stamp
  @existing_stamp = begin
    File.open(path, "r") do |file|
      file.read.scan(%r{#{comment_delimiter}\s\(c\)(.+)\sAll\sRights\sReserved}).flatten.first
    end
  end
end

#has_stamp?Boolean

Does this file already have a stamp?

Returns:

  • (Boolean)


60
61
62
# File 'lib/katana_stamp/stamp_file.rb', line 60

def has_stamp?
  !!existing_stamp
end

#has_stamp_with_another_owner?Boolean

Does this file have a stamp with a different owner?

Returns:

  • (Boolean)


70
71
72
# File 'lib/katana_stamp/stamp_file.rb', line 70

def has_stamp_with_another_owner?
  has_stamp? and existing_owner != owner
end

#messageObject

The message to be stamped to each file



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/katana_stamp/stamp_file.rb', line 75

def message
  @message ||= begin
    message_prefix = "#{comment_delimiter} (c) "
    if @options_message
      message_prefix << @options_message
    else
      message_prefix << "Copyright #{year} #{owner}. All Rights Reserved"
    end
    message_prefix
  end
end

#stamp!Object

When called, will add a copyright notice comment to the end of the file with path



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/katana_stamp/stamp_file.rb', line 33

def stamp!
  case 
  when has_stamp_with_another_owner?
    print_colour("stamped by another owner!", :red, :warn)
  when has_stamp?
    print_colour("already stamped!", :yellow)
  else
    `echo "#{"\n" unless has_closing_break?}#{message}" >> #{path}`
    print_colour("stamped!", :green)        
  end
end

#stamp_regexpObject

Regular expression to match stamps



65
66
67
# File 'lib/katana_stamp/stamp_file.rb', line 65

def stamp_regexp
  %r{#{comment_delimiter}\s\(c\)(.+)\sAll\sRights\sReserved}
end