Module: ThorExtensions

Included in:
Rails::Generators::Actions
Defined in:
lib/thor-ext.rb

Overview

needed for has_plugin? to use Rails.root require ‘rails’ require ‘thor’

Instance Method Summary collapse

Instance Method Details

#add_gem(gem_name, gem_version = nil) ⇒ Object



33
34
35
36
37
38
# File 'lib/thor-ext.rb', line 33

def add_gem(gem_name, gem_version = nil)
  if !has_gem?(gemfile_txt, gem_name) 
    gem_version_str = gem_version ? ", '#{gem_version}'" : '' 
    append_line_to_file 'Gemfile', "gem '#{gem_name}'#{gem_version_str}"  
  end
end

#add_gems(gem_names) ⇒ Object



40
41
42
# File 'lib/thor-ext.rb', line 40

def add_gems(gem_names)
  gem_names.each{|gem_name| add_gem(gem_name) }
end

#append_line_to_file(path, *args, &block) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/thor-ext.rb', line 6

def append_line_to_file(path, *args, &block)
  if block_given?
    data = block
  else
    data = args.shift
  end
  append_file path, "#{data}\n"
end

#cleanup_gemfileObject



16
17
18
19
# File 'lib/thor-ext.rb', line 16

def cleanup_gemfile
  # add newline between each gem statement in Gemfile
  gsub_file 'Gemfile', /('|")gem/, "\1\ngem"      
end

#gemfile_txtObject



44
45
46
# File 'lib/thor-ext.rb', line 44

def gemfile_txt
  @gemfile_txt ||= File.open('Gemfile').read        
end

#has_gem?(text, gem_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/thor-ext.rb', line 21

def has_gem?(text, gem_name)        
  if /\n[^#]*gem\s*('|")\s*#{Regexp.escape(gem_name)}\s*('|")/i.match(text)  
    true 
  else
    false
  end      
end

#has_plugin?(plugin_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/thor-ext.rb', line 29

def has_plugin?(plugin_name) 
  File.directory?(File.join(Rails.root, "vendor/plugins/#{plugin_name}"))
end