Class: Bgem::New::Lib

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

Constant Summary collapse

TEMPLATES =
"#{__dir__}/templates"

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Lib

Returns a new instance of Lib.



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

def initialize name
  @name = name
  @constants = @name.split ':'
  @gem_name = @name.to_gem_name
  @root = "#{Dir.pwd}/#{@gem_name}"

  create
end

Instance Method Details

#copy(from, to) ⇒ Object



60
61
62
63
# File 'lib/bgem/lib.rb', line 60

def copy from, to
  require 'fileutils'
  FileUtils.cp from, to
end

#createObject



15
16
17
18
19
20
21
22
23
# File 'lib/bgem/lib.rb', line 15

def create
  create_common

  if @constants.size > 1
    create_for_nested_constant
  else
    create_for_top_constant
  end
end

#create_commonObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bgem/lib.rb', line 27

def create_common
  Dir.mkdir @root
  IO.write "#{@root}/#{@gem_name}.gemspec", (template :gemspec)

  copy "#{TEMPLATES}/Gemfile", "#{@root}/Gemfile"
  copy "#{TEMPLATES}/Rakefile", "#{@root}/Rakefile"
  copy "#{TEMPLATES}/gitignore", "#{@root}/.gitignore"

  Dir.mkdir "#{@root}/src"
  Dir.mkdir "#{@root}/bgem"
  Dir.mkdir "#{@root}/spec"
end

#create_for_nested_constantObject



47
48
49
50
51
52
# File 'lib/bgem/lib.rb', line 47

def create_for_nested_constant
  copy "#{TEMPLATES}/module_stub.rb", "#{@root}/src/#{@constants.last}.module.rb"
  IO.write "#{@root}/bgem/config.rb", (template 'bgem-config.nested')
  IO.write "#{@root}/spec/helper.rb", (template 'rspec-helper.nested')
  IO.write "#{@root}/spec/main_spec.rb", (template 'main_spec.nested')
end

#create_for_top_constantObject



40
41
42
43
44
45
# File 'lib/bgem/lib.rb', line 40

def create_for_top_constant
  copy "#{TEMPLATES}/module_stub.rb", "#{@root}/src/#{@name}.module.rb"
  IO.write "#{@root}/bgem/config.rb", (template 'bgem-config.top')
  IO.write "#{@root}/spec/helper.rb", (template 'rspec-helper.top')
  IO.write "#{@root}/spec/main_spec.rb", (template 'main_spec.top')
end

#template(template_name) ⇒ Object



54
55
56
57
58
# File 'lib/bgem/lib.rb', line 54

def template template_name
  require 'erb'
  renderer = ERB.new IO.read "#{TEMPLATES}/#{template_name}.erb"
  renderer.result binding
end