Class: Cear::File

Inherits:
Object
  • Object
show all
Defined in:
lib/cear/file.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(__app_name, options = {}) ⇒ File



10
11
12
13
# File 'lib/cear/file.rb', line 10

def initialize(__app_name, options = {})
  @app_name = __app_name
  @options  = options
end

Class Method Details

.run(__app_name, options = {}) ⇒ Object



15
16
17
# File 'lib/cear/file.rb', line 15

def self.run(__app_name, options = {})
  Cear::File.new(__app_name, options).run
end

Instance Method Details

#add_gems(condition, gems) ⇒ Object



23
24
25
# File 'lib/cear/file.rb', line 23

def add_gems(condition, gems)
  condition ? gems.map {|g| "gem '#{g}'"}.join("\n") : ""
end

#file_generator(file) ⇒ Object



31
32
33
# File 'lib/cear/file.rb', line 31

def file_generator(file)
  eval('"' + ::File.open(::File.expand_path("../../template/#{file}", __FILE__)).read + '"', binding, __FILE__, __LINE__)
end

#generate_file(filename, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cear/file.rb', line 35

def generate_file(filename, options = {})
  @filename = filename
  options   = {header: true}.merge(@options).merge(options)

  if ::File.exists?(@filename) == false or AskQuestion.new("Replace file #{@filename}?", "n/Y").ask != 'n'
    puts "Generate #{filename}".green
    ::File.open(@filename, 'w') do |file|
        file.write(file_generator('header') + "\n") if options[:header]
      if template_exists?(@filename)
          file.write(file_generator(@filename))
      else
        file.write("# require ::File.expand_path('../config/environment',  __FILE__)\n")
      end
    end
  end
end

#runObject



19
20
21
# File 'lib/cear/file.rb', line 19

def run
  generate_file(@app_name)
end

#template_exists?(file) ⇒ Boolean



27
28
29
# File 'lib/cear/file.rb', line 27

def template_exists?(file)
  ::File.exists?(::File.expand_path("../../template/#{file}", __FILE__))
end