Class: CopyrightHeader::License

Inherits:
Object
  • Object
show all
Defined in:
lib/copyright_header/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ License

Returns a new instance of License.



32
33
34
35
# File 'lib/copyright_header/parser.rb', line 32

def initialize(options)
  @options = options
  @lines = load_template.split(/\n/).map { |line| line += "\n" }
end

Instance Method Details

#format(comment_open = nil, comment_close = nil, comment_prefix = nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/copyright_header/parser.rb', line 53

def format(comment_open = nil, comment_close = nil, comment_prefix = nil)
  comment_open ||= ''
  comment_close ||= ''
  comment_prefix ||= ''
  license = comment_open + @lines.map { |line| (comment_prefix + line).gsub(/\s+\n$/, "\n") }.join() + comment_close
  license.gsub!(/\\n/, "\n")
  license
end

#load_templateObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/copyright_header/parser.rb', line 42

def load_template
  if File.exists?(@options[:license_file])
    template = ::ERB.new File.new(@options[:license_file]).read, 0, '%'
    license = template.result(OpenStruct.new(@options).instance_eval { binding }) 
    license = word_wrap(license)
    license
  else
    raise FileNotFoundException.new("Unable to open #{file}")
  end
end

#word_wrap(text, max_width = nil) ⇒ Object



37
38
39
40
# File 'lib/copyright_header/parser.rb', line 37

def word_wrap(text, max_width = nil)
  max_width ||= @options[:word_wrap]
  text.gsub(/(.{1,#{max_width}})(\s|\Z)/, "\\1\n")
end