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.



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

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



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

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



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

def camelized
  @camelized ||= name.camelize
end

#defaultsObject



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

def defaults
  { :verbose => false }
end

#destination_pathObject

Path to the testing application



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

def destination_path
  File.join(test_path, name)
end

#extensionObject

The name of the extension to be tested



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

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 your_extension/lib/dummy_hooks



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

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
    if File.exists?(file)
      rb = File.read(file) 
      eval(rb)    
    end
  rescue Exception => e
    say_status "failed", "#{hook_name} raised an exception", :red
    say e.message.strip + "\n", :red
  end        
end

#nameObject

The name of the rails application



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

def name
  "dummy"
end

#root_pathObject

Path the the extension’s root folder



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

def root_path 
  @root_path
end

#run!Object

Runs the generator



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dummier/app_generator.rb', line 92

def run!
  
  fire_hook :before_delete
  
  # remove existing test app 
  FileUtils.rm_r(destination_path) if File.directory?(destination_path)

  fire_hook :before_app_generator
    
  # run the base app generator
  Rails::Generators::AppGenerator.start([destination_path])
  
  fire_hook :after_app_generator
  
  inside destination_path do
            
    # remove unnecessary files    
    files = %w(public/index.html public/images/rails.png Gemfile README doc test vendor)
    files.each do |file|
      say_status "delete", file
      FileUtils.rm_r(file) if File.exists?(file)
    end

    # replace crucial templates
    template "rails/application.rb", "config/application.rb", :force => true
    template "rails/boot.rb",        "config/boot.rb",        :force => true
            
    # add cucumber to database.yml
    cukes = File.directory?(File.join(root_path, "features"))
    if cukes
      append_file "config/database.yml" do
%(
cucumber:
  <<: *test
)
      end
    end
    
    fire_hook :before_migrate
            
    rake("db:migrate", :env => "test")
    
    fire_hook :after_migrate
    
  end  
  
end

#test_pathObject

Path to the extension’s test folder



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

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

#underscoredObject

The name, underscored



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

def underscored
  @underscored ||= name.underscore
end