Class: KatanaStamp::StampFile
- Inherits:
-
Object
- Object
- KatanaStamp::StampFile
- 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
-
#comment_delimiter ⇒ Object
readonly
Comment delimeter used in stamp (defaults to ‘#’).
-
#owner ⇒ Object
readonly
Copyright owner (defaults to Katana Code Ltd).
-
#path ⇒ Object
readonly
Path to this file.
-
#year ⇒ Object
readonly
Copyright year (defaults to this Year).
Instance Method Summary collapse
-
#existing_owner ⇒ Object
The existing Copyright owner or nil.
-
#existing_stamp ⇒ Object
Does the file already have a copyright stamp?.
-
#has_stamp? ⇒ Boolean
Does this file already have a stamp?.
-
#has_stamp_with_another_owner? ⇒ Boolean
Does this file have a stamp with a different owner?.
-
#initialize(path, options) ⇒ StampFile
constructor
A new instance of StampFile.
-
#message ⇒ Object
The message to be stamped to each file.
-
#stamp! ⇒ Object
When called, will add a copyright notice comment to the end of the file with path.
-
#stamp_regexp ⇒ Object
Regular expression to match stamps.
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, ) @path = path @comment_delimiter = [:comment_delimiter] || '#' @year = [:year] || Time.now.year @owner = [:owner] || 'Katana Code Ltd' @options_message = [:message] end |
Instance Attribute Details
#comment_delimiter ⇒ Object (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 |
#owner ⇒ Object (readonly)
Copyright owner (defaults to Katana Code Ltd)
22 23 24 |
# File 'lib/katana_stamp/stamp_file.rb', line 22 def owner @owner end |
#path ⇒ Object (readonly)
Path to this file
13 14 15 |
# File 'lib/katana_stamp/stamp_file.rb', line 13 def path @path end |
#year ⇒ Object (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_owner ⇒ Object
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_stamp ⇒ Object
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?
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?
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 |
#message ⇒ Object
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 ||= begin = "#{comment_delimiter} (c) " if @options_message << @options_message else << "Copyright #{year} #{owner}. All Rights Reserved" end 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?}#{}" >> #{path}` print_colour("stamped!", :green) end end |
#stamp_regexp ⇒ Object
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 |