Class: Dummier::AppGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/dummier/app_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ AppGenerator

Returns a new instance of AppGenerator.



19
20
21
22
23
24
25
26
27
# File 'lib/dummier/app_generator.rb', line 19

def initialize(root, options={})
  @behavior = :invoke
  @root_path = File.expand_path(root)
  @destination_stack = []
  @options = defaults.merge(options)
  self.source_paths << File.join(root_path, "test/dummy_hooks/templates")
  self.destination_root = File.join(test_path, name)
  raise "Invalid directory!" unless File.directory?(@root_path)
end

Instance Method Details

#application_definitionObject Also known as: store_application_definition!

gets the current application.rb contents



65
66
67
68
69
70
# File 'lib/dummier/app_generator.rb', line 65

def application_definition
  @application_definition ||= begin
    contents = File.read("#{destination_root}/config/application.rb")
    contents[(contents.index("module Dummy"))..-1]
  end
end

#camelizedObject

The name, camelized



40
41
42
# File 'lib/dummier/app_generator.rb', line 40

def camelized
  @camelized ||= name.camelize
end

#defaultsObject

Default generator options



15
16
17
# File 'lib/dummier/app_generator.rb', line 15

def defaults
  { :verbose => false }
end

#destination_pathObject

Path to the testing application



60
61
62
# File 'lib/dummier/app_generator.rb', line 60

def destination_path
  File.join(test_path, name)
end

#extensionObject

The name of the extension to be tested



35
36
37
# File 'lib/dummier/app_generator.rb', line 35

def extension
  File.basename(root_path)
end

#fire_hook(hook_name) ⇒ Object

Loads a hook file and evalutes its contents. rescues any exceptions and logs their message. Store hooks in gem_root/test/dummy_hooks



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dummier/app_generator.rb', line 76

def fire_hook(hook_name)
  begin
    file = File.join(root_path, "test/dummy_hooks/#{hook_name}.rb")
    say_status "hook", hook_name, File.exists?(file) ? :cyan : :red
    eval File.read(file) if File.exists?(file)
  rescue Exception => e
    say_status "failed", "#{hook_name} raised an exception", :red
    say "*" * 60, :red
    say e.message.strip + "\n", :red
    say e.backtrace.join("\n"), :red
    raise ::Dummier::HookException, "Quitting, #{hook_name} raised an exception."        
  end
end

#nameObject

The name of the rails application



30
31
32
# File 'lib/dummier/app_generator.rb', line 30

def name
  "dummy"
end

#root_pathObject

Path the the extension’s root folder



50
51
52
# File 'lib/dummier/app_generator.rb', line 50

def root_path 
  @root_path
end

#run!Object

Runs the generator



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dummier/app_generator.rb', line 91

def run!
  fire_hook :before_delete
  remove_existing_dummy
  fire_hook :before_app_generator
  run_base_generator
  fire_hook :after_app_generator
  inside destination_path do
    remove_unnecessary_files
    replace_boot_templates
    add_cucumber_support if has_features?
    fire_hook :before_migrate
    run_migration
    fire_hook :after_migrate
  end
end

#test_pathObject

Path to the extension’s test folder



55
56
57
# File 'lib/dummier/app_generator.rb', line 55

def test_path
  File.join(root_path, "test")
end

#underscoredObject

The name, underscored



45
46
47
# File 'lib/dummier/app_generator.rb', line 45

def underscored
  @underscored ||= name.underscore
end