Class: Transit::Controller::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/transit/controller/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*klasses) ⇒ Generator

Returns a new instance of Generator.



10
11
12
# File 'lib/transit/controller/generator.rb', line 10

def initialize(*klasses)
  @types = klasses.map(&:to_s)
end

Instance Attribute Details

#typesObject

Returns the value of attribute types.



8
9
10
# File 'lib/transit/controller/generator.rb', line 8

def types
  @types
end

Instance Method Details

#generate!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/transit/controller/generator.rb', line 14

def generate!
  @types.map(&:to_sym).each do |type|
    typed_classes = Transit.lookup(type).dup.map!(&:constantize)
    
    typed_classes.each do |sup|          
      sup.subclasses.each do |klass|              
        controller_name = to_controller(klass) 
        next if Transit.const_defined?( controller_name, false )
        eval "class Transit::#{controller_name} < Transit::#{parent_controller(sup)}; end"
        ActiveSupport::Dependencies::Reference.store(Transit.const_get("#{controller_name}"))
      end
    end
    
  end
end

#parent_controller(str) ⇒ Object



30
31
32
33
# File 'lib/transit/controller/generator.rb', line 30

def parent_controller(str)
  p = ActiveSupport::Inflector.pluralize(str.to_s.classify)
  "#{p}Controller"
end

#to_controller(str) ⇒ Object



35
36
37
# File 'lib/transit/controller/generator.rb', line 35

def to_controller(str)
  (ActiveSupport::Inflector.pluralize(str.to_s.classify) << "Controller")
end