Class: Stratagem::ModelBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModelBuilder

Returns a new instance of ModelBuilder.



5
6
7
# File 'lib/stratagem/model_builder.rb', line 5

def initialize
  @model = Stratagem::Model::Application.instance
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/stratagem/model_builder.rb', line 3

def model
  @model
end

#parsed_controllersObject (readonly)

Returns the value of attribute parsed_controllers.



3
4
5
# File 'lib/stratagem/model_builder.rb', line 3

def parsed_controllers
  @parsed_controllers
end

#parsed_modelsObject (readonly)

Returns the value of attribute parsed_models.



3
4
5
# File 'lib/stratagem/model_builder.rb', line 3

def parsed_models
  @parsed_models
end

Instance Method Details

#load_files(base_dir, path = [], file_list = []) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/stratagem/model_builder.rb', line 127

def load_files(base_dir,path=[],file_list=[])
  dir = File.join(base_dir, *path)
  files = Dir.new(dir).entries
  files.each do |file|
    next if file =~ /^\./

    if (File.directory?(File.join(dir, file)))
      load_files(base_dir, path + [file], file_list)
    else
      file_path = File.join(path, file)
      file_list << file_path
    end
  end
  file_list
end

#load_modelsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stratagem/model_builder.rb', line 46

def load_models()
  @model.models.clear
  
  # load files into classes
  log "loading models"
  root = File.join(RAILS_ROOT, 'app','models')
  load_files(File.join(root)).map {|model|
    models = Stratagem::Model::Component::Model.load_all(File.join(root, model))
    models.each do |c|
      log "\t#{c.klass.name} loaded from #{model}"
    end
    @model.models << models
  }
  log ""
end

#load_pluginsObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/stratagem/model_builder.rb', line 35

def load_plugins()
  @model.plugins.clear
  if (Stratagem.rails_3?)
    @model.plugins << Rails.application.railties.plugins
  else
    loader = Rails::Plugin::Loader.new(Rails::Initializer.new(Rails::Configuration.new))
    loader.load_plugins
    @model.plugins << loader.initializer.loaded_plugins
  end
end

#load_publicObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/stratagem/model_builder.rb', line 62

def load_public
  @model.static_files.clear
  
  log "loading static files"
  Dir[File.join(RAILS_ROOT, 'public', '**', '*.html')].each {|static|
    static.gsub!(RAILS_ROOT, '').gsub!(/^\/public\//, '')
    @model.static_files << Stratagem::Model::Component::StaticFile.new(static)
    log "\t#{static}"
  }
  log ""
end

#load_routesObject



86
87
88
89
90
91
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
# File 'lib/stratagem/model_builder.rb', line 86

def load_routes
  @model.routes.clear
  @model.controllers.clear

  log 'loading routes'
  root = File.join(RAILS_ROOT, 'app','controllers')
  ActionController::Routing::Routes.routes.each {|route|
    route_container = Stratagem::Model::Component::Route.new(route)
    @model.routes << route_container 
    
    begin
      controller_class = route_container.controller_name.constantize

      # find or create the controller
      controller_container = @model.controllers.find {|c| c.klass == controller_class } || begin
        puts "loading controller - #{route_container.controller_name}"

        # load the parse tree
        parse_tree = nil
        begin
          filename = File.join(root, "#{route_container.controller_path}_controller.rb")
          parse_tree = RedParse.new(File.read(filename)).parse if (File.exists?(filename))
        rescue
          puts "ERROR PARSING CONTROLLER: #{$!.message}"
        end

        # store the new controller in the model
        controller_container = Stratagem::Model::Component::Controller.new(filename, parse_tree, controller_class)
        @model.controllers << controller_container
        controller_container
      end
      
      # configure the route
      configure_route(route_container, controller_container)
    rescue
      @model.routes.invalid << Stratagem::Model::Component::Route.new(route)
    end
  }
  log ""
end

#load_template_pathsObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stratagem/model_builder.rb', line 74

def load_template_paths
  @model.views.clear

  log "loading templates"
  root = File.join(RAILS_ROOT, 'app','views')
  load_files(root).map {|template|
    @model.views << Stratagem::Model::Component::View.new(template)
    log "\t#{template}"
  }
  log ""
end

#log(msg) ⇒ Object



21
22
23
# File 'lib/stratagem/model_builder.rb', line 21

def log(msg)
  Stratagem.logger.debug msg
end


25
26
27
28
29
30
31
32
33
# File 'lib/stratagem/model_builder.rb', line 25

def print_errors
  @model.routes.invalid.each {|route|
    puts "route: #{route.route.to_s} is invalid"
  }

  @model.controllers.missing.each {|controller, routes|
    puts "controller: #{controller}, with #{routes.size} routes is missing"
  }
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/stratagem/model_builder.rb', line 9

def run
  load_plugins
  load_public
  load_template_paths
  load_routes
  load_models

  print_errors
  
  @model
end