Class: PrepareFile

Inherits:
Object
  • Object
show all
Defined in:
lib/envGen/add_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PrepareFile

Returns a new instance of PrepareFile.



8
9
10
11
# File 'lib/envGen/add_file.rb', line 8

def initialize(file)
  @file = file
  @fileName = File.basename(file)
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



6
7
8
# File 'lib/envGen/add_file.rb', line 6

def file
  @file
end

#fileNameObject

Returns the value of attribute fileName.



6
7
8
# File 'lib/envGen/add_file.rb', line 6

def fileName
  @fileName
end

Class Method Details

.createIfNotInConfig(file) ⇒ Object

creates a files object if not exists



13
14
15
16
17
18
19
# File 'lib/envGen/add_file.rb', line 13

def self.createIfNotInConfig(file) # creates a files object if not exists
  if !inConfig?(file)
    PrepareFile.new(file)
  else
    puts "'#{File.basename(file)}' already added"
  end
end

.inConfig?(file) ⇒ Boolean

looks through environment for fileName; doesn’t create an object/instance method because it may not be necessary

Returns:

  • (Boolean)


21
22
23
# File 'lib/envGen/add_file.rb', line 21

def self.inConfig?(file) # looks through environment for fileName; doesn't create an object/instance method because it may not be necessary
  File.readlines("config/environment.rb").grep(/#{File.basename(file)}/).count > 0
end

Instance Method Details

#isRuby?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/envGen/add_file.rb', line 32

def isRuby?
  File.extname(fileName) == ".rb"
end

#notRubyObject

handles non-Ruby files



36
37
38
39
40
41
42
43
44
# File 'lib/envGen/add_file.rb', line 36

def notRuby # handles non-Ruby files
  puts "'#{fileName}' is not a Ruby (.rb) file. Continue? (Y/N)"
  answer = gets.chomp.strip
  if answer == "Y"
    write
  else
    puts "'#{fileName}' not added"
  end
end

#relativePathObject

finds file path relative to environment



46
47
48
49
50
# File 'lib/envGen/add_file.rb', line 46

def relativePath # finds file path relative to environment
  dir = Pathname.new (Dir.pwd + "/config")
  filePathname = Pathname.new File.absolute_path(file)
  relative = (filePathname.relative_path_from dir).to_s
end

#writeObject

adds relative path of file to environment



25
26
27
28
29
30
# File 'lib/envGen/add_file.rb', line 25

def write # adds relative path of file to environment
  File.open("config/environment.rb", "a") {|env|
    env.puts "require_relative '#{relativePath}'"
  }
  puts "Added '#{fileName}'"
end