Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[merb-slices] AS migration

  • Loading branch information...
commit 77d1453d8af1903a0ff9a6289a9a1dcbe65b06ae 1 parent d1790ff
Nicos Gollan authored May 16, 2011
2  merb-slices/.rspec
... ...
@@ -1,4 +1,2 @@
1 1
 --color
2 2
 --format progress
3  
---format html
4  
---out rspec_results.html
12  merb-slices/lib/generators/base.rb
@@ -23,7 +23,11 @@ def initialize(*args)
23 23
   end
24 24
   
25 25
   class BaseSliceGenerator < NamedGenerator
26  
-    
  26
+    def initialize(*args)
  27
+      Merb.disable(:initfile)
  28
+      super
  29
+    end
  30
+
27 31
     def self.common_template(name, template_source)
28 32
       common_base_dir = File.expand_path(File.dirname(__FILE__))
29 33
       template name do |t|
@@ -31,9 +35,9 @@ def self.common_template(name, template_source)
31 35
         t.destination = template_source
32 36
       end
33 37
     end
34  
-    
  38
+
35 39
   end
36  
-  
  40
+
37 41
   add :slice, SliceGenerator
38  
-  
  42
+
39 43
 end
8  merb-slices/lib/generators/full.rb
... ...
@@ -1,12 +1,6 @@
1 1
 module Merb::Generators
2 2
   
3 3
   class FullSliceGenerator < BaseSliceGenerator
4  
-    
5  
-    def initialize(*args)
6  
-      Merb.disable(:initfile)
7  
-      super
8  
-    end
9  
-
10 4
     def self.source_root
11 5
       File.join(File.dirname(__FILE__), 'templates', 'full')
12 6
     end
@@ -35,4 +29,4 @@ def destination_root
35 29
   
36 30
   add_private :full_slice, FullSliceGenerator
37 31
   
38  
-end
  32
+end
14  merb-slices/lib/merb-slices/module.rb
... ...
@@ -1,5 +1,7 @@
1 1
 # encoding: UTF-8
2 2
 
  3
+require 'pathname'
  4
+
3 5
 module Merb
4 6
   module Slices
5 7
     
@@ -11,7 +13,7 @@ class << self
11 13
       #
12 14
       # @return [Module] The slice module itself.
13 15
       def [](module_name)
14  
-        Object.full_const_get(module_name.to_s) if exists?(module_name)
  16
+        module_name.to_s.constantize if exists?(module_name)
15 17
       end
16 18
 
17 19
       # Helper method to transform a slice filename to a module Symbol.
@@ -62,8 +64,8 @@ def register(slice_file, force = true)
62 64
       # given, `Merb::Plugins.config[:merb_slices][:search_path]` (String/Array)
63 65
       def register_slices_from_search_path!
64 66
         slice_files_from_search_path.each do |slice_file|
65  
-          absolute_path = File.expand_path(slice_file)
66  
-          Merb.logger.info!("Found slice '#{File.basename(absolute_path, '.rb')}' in search path at #{absolute_path.relative_path_from(Merb.root)}")
  67
+          absolute_path = Pathname.new(File.expand_path(slice_file))
  68
+          Merb.logger.info!("Found slice '#{File.basename(absolute_path, '.rb')}' in search path at #{absolute_path.relative_path_from(Pathname.new(Merb.root))}")
67 69
           Merb::Slices::Loader.load_classes(absolute_path)
68 70
         end
69 71
       end
@@ -95,7 +97,7 @@ def unregister(slice_module)
95 97
       #   module name.
96 98
       def activate(slice_module)  
97 99
         unless slice_file = self.files[slice_module.to_s]
98  
-          module_name_underscored = slice_module.to_s.snake_case.escape_regexp
  100
+          module_name_underscored = slice_module.to_s.underscore.escape_regexp
99 101
           module_name_dasherized  = module_name_underscored.tr('_', '-').escape_regexp
100 102
           regexp = Regexp.new(/\/(#{module_name_underscored}|#{module_name_dasherized})\/lib\/(#{module_name_underscored}|#{module_name_dasherized})\.rb$/)
101 103
           slice_file = slice_files_from_search_path.find { |path| path.match(regexp) } # from search path(s)
@@ -217,7 +219,7 @@ def config
217 219
       # @return [Array<Module>] A sorted array of all slice modules.
218 220
       def slices
219 221
         slice_names.map do |name|
220  
-          Object.full_const_get(name) rescue nil
  222
+          name.constantize rescue nil
221 223
         end.compact
222 224
       end
223 225
 
@@ -305,7 +307,7 @@ def slice_files_from_search_path
305 307
       # @return [Module] The module that has been setup.
306 308
       def setup_module(module_name)
307 309
         Object.make_module(module_name)
308  
-        slice_mod = Object.full_const_get(module_name)
  310
+        slice_mod = module_name.constantize
309 311
         slice_mod.extend(ModuleMixin)
310 312
         slice_mod
311 313
       end
8  merb-slices/lib/merb-slices/module_mixin.rb
@@ -169,7 +169,7 @@ def app_glob_for(type) self.app_paths[type][1] end
169 169
       #   requested type.
170 170
       def public_dir_for(type)
171 171
         dir = type.is_a?(Symbol) ? self.app_dir_for(type) : self.app_dir_for(:public) / type
172  
-        dir = dir.relative_path_from(Merb.dir_for(:public)) rescue '.'
  172
+        dir = dir.relative_path_from(Merb.dir_for(:public))
173 173
         dir == '.' ? '/' : "/#{dir}"
174 174
       end
175 175
 
@@ -453,17 +453,17 @@ def setup_default_structure!
453 453
       #   using {Merb.push_path}.
454 454
       def collect_load_paths(modify_load_path = true, push_merb_path = true)
455 455
         self.collected_slice_paths.clear; self.collected_app_paths.clear
456  
-        Merb.push_path(:"#{self.name.snake_case}_file", File.dirname(self.file), File.basename(self.file))
  456
+        Merb.push_path(:"#{self.name.underscore}_file", File.dirname(self.file), File.basename(self.file))
457 457
         self.collected_app_paths << self.file
458 458
         self.slice_paths.each do |component, path|
459 459
           if File.directory?(component_path = path.first)
460  
-            $LOAD_PATH.unshift(component_path) if modify_load_path && component.in?(:model, :controller, :lib) && !$LOAD_PATH.include?(component_path)
  460
+            $LOAD_PATH.unshift(component_path) if modify_load_path && [:model, :controller, :lib].include?(component) && !$LOAD_PATH.include?(component_path)
461 461
             # slice-level component load path - will be preceded by application/app/component - loaded next by Setup.load_classes
462 462
             self.collected_slice_paths << path.first / path.last if path.last
463 463
             # app-level component load path (override) path - loaded by BootLoader::LoadClasses
464 464
             if (app_glob = self.app_glob_for(component)) && File.directory?(app_component_dir = self.app_dir_for(component))
465 465
               self.collected_app_paths << app_component_dir / app_glob
466  
-              Merb.push_path(:"#{self.name.snake_case}_#{component}", app_component_dir, app_glob) if push_merb_path
  466
+              Merb.push_path(:"#{self.name.underscore}_#{component}", app_component_dir, app_glob) if push_merb_path
467 467
             end
468 468
           end
469 469
         end
2  merb-slices/lib/merb-slices/router_ext.rb
@@ -47,7 +47,7 @@ def all_slices(config = {}, &block)
47 47
   def add_slice(slice_module, options = {}, &block)
48 48
     if Merb::Slices.exists?(slice_module)
49 49
       options = { :path => options } if options.is_a?(String)
50  
-      slice_module = Object.full_const_get(slice_module.to_s.camelize) if slice_module.class.in?(String, Symbol)
  50
+      slice_module = slice_module.to_s.camelize.constantize if [String, Symbol].include?(slice_module.class)
51 51
       namespace = options[:namespace] || slice_module.identifier_sym
52 52
       options[:path] ||= options[:path_prefix] || slice_module[:path_prefix] || options[:namespace] || slice_module.identifier
53 53
       options[:prepend_routes] = block if block_given?
4  merb-slices/spec/full_slice_generator_spec.rb
@@ -15,7 +15,7 @@
15 15
     
16 16
     it "should create a number of templates" do
17 17
       @generator.invoke!
18  
-      files = Dir[slices_path / 'testing' / '**' / '*'].map do |path| 
  18
+      files = Dir[slices_path / 'testing' / '**' / '*'].map do |path|
19 19
         path.relative_path_from(slices_path)
20 20
       end
21 21
       expected = [
@@ -46,4 +46,4 @@
46 46
     
47 47
   end
48 48
   
49  
-end
  49
+end
2  merb-slices/spec/full_slice_spec.rb
@@ -159,4 +159,4 @@
159 159
     controller.slice_url(:home).should == url
160 160
   end
161 161
     
162  
-end
  162
+end
2  merb-slices/spec/slices/full-test-slice/app/views/layout/full_test_slice.html.erb
@@ -13,4 +13,4 @@
13 13
     <div id="main"><%= catch_content :for_layout %></div>
14 14
   </div>
15 15
   </body>
16  
-</html>
  16
+</html>

0 notes on commit 77d1453

Please sign in to comment.
Something went wrong with that request. Please try again.