Class: Sign::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/sign/generator.rb

Instance Method Summary collapse

Instance Method Details

#create(license, name, year) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sign/generator.rb', line 3

def create(license, name, year)
  raise ArgumentError, "#{license} is not available." unless File.readable?(license)

  license_template = File.read(license)
  name = get_name if name.nil?
  year = get_year if year.nil?
  
  if placeholders_exist?(license_template)
    new_license = license_template.gsub("[AUTHOR]", name)
    new_license = new_license.gsub("[YEAR]", year)
  else
    new_license = license_template
  end
  
  create_new_file(new_license)
end

#create_new_file(new_license) ⇒ Object



26
27
28
29
30
31
# File 'lib/sign/generator.rb', line 26

def create_new_file(new_license)
  new_file = File.new("LICENSE", "w")
  new_file.puts(new_license)
  new_file.close
  puts "License created \033[91m<3\033[0m"
end

#find_nameObject



41
42
43
44
45
46
47
# File 'lib/sign/generator.rb', line 41

def find_name
  File.foreach("#{gitconfig_path}/.gitconfig").detect do |line| 
    if line.match("\tname")
      return line.scan(/= (.*)/).last[0]
    end
  end
end

#get_nameObject



33
34
35
36
37
38
39
# File 'lib/sign/generator.rb', line 33

def get_name
  if gitconfig_exist?
    find_name
  else
    return ""
  end
end

#get_yearObject



49
50
51
# File 'lib/sign/generator.rb', line 49

def get_year
  return Time.new.year.to_s
end

#gitconfig_exist?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/sign/generator.rb', line 53

def gitconfig_exist?
  File.exist?("#{gitconfig_path}/.gitconfig")
end

#gitconfig_pathObject



57
58
59
# File 'lib/sign/generator.rb', line 57

def gitconfig_path
  File.expand_path("~/", __dir__)
end

#placeholders_exist?(license_template) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/sign/generator.rb', line 20

def placeholders_exist?(license_template)
  placeholders = ["[YEAR]", "[AUTHOR]"]
  
  placeholders.any? { |placeholder| license_template.include?(placeholder) }
end