Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactoring of the configuration interface

  • Loading branch information...
commit 4f3b4b9dd643d3e43aa7e400430468e7605d78cf 1 parent 4e69aac
authored May 02, 2009
8  README.markdown
Source Rendered
@@ -15,8 +15,8 @@ No routes have to be changed to use this functionality.
15 15
 
16 16
 ## Configuration
17 17
 
18  
-To enable or disable the locale routing set **Perfectline::LocaleRouting.enabled = true/false** # defaults to true  
19  
-To specifiy which part of the URL should be matched for the locale identifier, set **Perfectline::LocaleRouting.match_from = option**
  18
+To enable or disable the locale routing set **Perfectline::LocaleRouting::Config.enabled = true/false** # defaults to true
  19
+To specifiy which part of the URL should be matched for the locale identifier, set **Perfectline::LocaleRouting::Config.match_from = option**
20 20
 
21 21
 ### Available options for match_from are:
22 22
 *   **:params**  
@@ -33,13 +33,13 @@ To specifiy which part of the URL should be matched for the locale identifier, s
33 33
     Searches for configured matches in the domain name. If a match is found, the configured locale is then checked against I18n.available_locales.  
34 34
     If the configured locale does not exist in available locales, it will fall back to the default locale.
35 35
 
36  
-    Configuring host to locale mappings is done via **Perfectline::LocaleRouting.create_mapping = hash**.  
  36
+    Configuring host to locale mappings is done via **Perfectline::LocaleRouting::Config.mapping do |block|**.
37 37
     Hash key must be the hostname pattern to be matched and value is the locale string.  
38 38
     Hostname patterns are like simplified regexp patterns with * wildcard support.  
39 39
     Mappings matching is similar to route config - first match found is used, rest is ignored.
40 40
 
41 41
     Example:
42  
-        Perfectline::LocaleRouting.mapping do |map|
  42
+        Perfectline::LocaleRouting::Config.mapping do |map|
43 43
           map.match "en.dev.*", "en", # matches en.dev.yoursite.com and en.dev.mysite.co.uk
44 44
           map.match "*.dev.*",  "en", # matches foo.dev.yoursite.com and www.dev.yoursite.com but not en.dev.mysite.com as its defined AFTER that mapping
45 45
           map.match "en.*",     "en", # matches en.yoursite.com but not en.dev.yoursite.com or en.foo.dev.yoursite.com as its defined AFTER that mapping
2  lib/locale_routing.rb
@@ -4,4 +4,4 @@
4 4
 require 'locale_routing/locale'
5 5
 
6 6
 # wire up routing hooks
7  
-Perfectline::LocaleRouting.bootstrap
  7
+Perfectline::LocaleRouting::Config.bootstrap
68  lib/locale_routing/config.rb
@@ -7,56 +7,54 @@ def initialize(config)
7 7
       end
8 8
 
9 9
       def match(host, locale)
10  
-        @config.add_host_match(host, locale)
  10
+        @config.push({:host => host.to_s.gsub('.', '\.').gsub('*', '.*'), :locale => locale.to_s})
11 11
       end
12 12
     end
13 13
 
14  
-    @locale_match_options = [:params, :host]
15  
-    @host_mapping = []
  14
+    class Config
  15
+      @locale_match_options = [:params, :host]
  16
+      @host_mapping = []
16 17
 
17  
-    # defaults
18  
-    @enabled = true
19  
-    @match_from = :params
  18
+      # defaults
  19
+      @enabled = true
  20
+      @match_from = :params
20 21
 
21  
-    class << self
  22
+      class << self
22 23
 
23  
-      def enabled?
24  
-        @enabled
25  
-      end
26  
-
27  
-      def enabled=(enabled)
28  
-        @enabled = (enabled.nil? || enabled == false) ? false : true
29  
-      end
  24
+        def enabled?
  25
+          @enabled
  26
+        end
30 27
 
31  
-      def match_from
32  
-        @match_from
33  
-      end
  28
+        def enabled=(enabled)
  29
+          @enabled = (enabled.nil? || enabled == false) ? false : true
  30
+        end
34 31
 
35  
-      def match_from=(location)
36  
-        unless @locale_match_options.include?(location.to_sym)
37  
-          raise "#{location.to_sym} is not supported as Perfectline::LocaleRouting.match_from option."
  32
+        def match_from
  33
+          @match_from
38 34
         end
39 35
 
40  
-        @match_from = location.to_sym
41  
-      end
  36
+        def match_from=(location)
  37
+          unless @locale_match_options.include?(location.to_sym)
  38
+            raise "#{location.to_sym} is not supported as Perfectline::LocaleRouting.match_from option."
  39
+          end
42 40
 
43  
-      def host_mapping
44  
-        @host_mapping
45  
-      end
  41
+          @match_from = location.to_sym
  42
+        end
46 43
 
47  
-      def add_host_mapping(host, locale)
48  
-        @host_mapping.push({:host => host.to_s.gsub('.', '\.').gsub('*', '.*'), :locale => locale.to_s})
49  
-      end
  44
+        def host_mapping
  45
+          @host_mapping
  46
+        end
50 47
 
51  
-      def mapping
52  
-        yield Mapper.new(self)
53  
-      end
  48
+        def mapping
  49
+          yield Mapper.new(@host_mapping)
  50
+        end
54 51
 
55  
-      # straps the LocaleRouteSet module into ActionControllers RouteSet
56  
-      def bootstrap
57  
-        ActionController::Routing::RouteSet.send(:include, Perfectline::LocaleRouting::LocaleRouteSet) if self.enabled?
58  
-      end
  52
+        # straps the LocaleRouteSet module into ActionControllers RouteSet
  53
+        def bootstrap
  54
+          ActionController::Routing::RouteSet.send(:include, Perfectline::LocaleRouting::LocaleRouteSet) if self.enabled?
  55
+        end
59 56
 
  57
+      end
60 58
     end
61 59
   end
62 60
 end
6  lib/locale_routing/locale.rb
@@ -25,8 +25,8 @@ def prepend(result, locale)
25 25
         end
26 26
 
27 27
         # extract and set locale
28  
-        def extract(path, env)     
29  
-          case Perfectline::LocaleRouting.match_from
  28
+        def extract(path, env)
  29
+          case Perfectline::LocaleRouting::Config.match_from
30 30
             when :params    then path = extract_from_params(path)
31 31
             when :host      then extract_from_host(env)
32 32
           end
@@ -58,7 +58,7 @@ def extract_from_host(env)
58 58
 
59 59
           host = env[:host].to_s
60 60
           locale = I18n.default_locale
61  
-          mappings = Perfectline::LocaleRouting.host_mapping
  61
+          mappings = Perfectline::LocaleRouting::Config.host_mapping
62 62
 
63 63
           if mappings.nil? || mappings.empty?
64 64
             RAILS_DEFAULT_LOGGER.warn "Perfectline::LocaleRouting.host_mapping is nil or empty."

0 notes on commit 4f3b4b9

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