rack-i18n_routes 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ Bones {
16
16
  email 'gioele@svario.it'
17
17
  url 'https://github.com/gioele/rack-i18n_routes'
18
18
 
19
- version '0.1'
19
+ version '0.2'
20
20
 
21
21
  ignore_file '.gitignore'
22
22
 
@@ -76,24 +76,42 @@ class Rack::I18nRoutes::AliasMapping
76
76
  # use Rack::I18nRoutes, MAPPING
77
77
  #
78
78
  # @param [Hash] aliases the aliases
79
+ # @param [Hash] opts extra options for the mapping
80
+ # @option opts [Foo] :default the language key associated with the
81
+ # normalized paths
79
82
 
80
- def initialize(aliases)
83
+ def initialize(aliases, opts = {})
81
84
  @aliases = aliases
85
+ @default_lang = opts[:default]
82
86
  end
83
87
 
88
+ # @return [String]
89
+
84
90
  def map(path)
91
+ normalized_path, found_langs = map_with_langs(path)
92
+
93
+ return normalized_path
94
+ end
95
+
96
+ # @return [(String, Array<Object>)]
97
+
98
+ def map_with_langs(path)
85
99
  orig_pieces = path.split('/')
100
+
86
101
  normalized_pieces = []
102
+ found_langs = []
87
103
 
88
104
  normalized_pieces << orig_pieces.shift
105
+ found_langs << @default_lang
89
106
 
90
107
  aliases = @aliases
91
108
 
92
109
  orig_pieces.each do |orig_piece|
93
- normalized = normalization_for(orig_piece, aliases)
110
+ normalized, lang = normalization_for(orig_piece, aliases)
94
111
  replacement = (normalized || orig_piece)
95
112
 
96
113
  normalized_pieces << replacement
114
+ found_langs << lang
97
115
 
98
116
  if !aliases.nil?
99
117
  subaliases = aliases[replacement]
@@ -103,28 +121,36 @@ class Rack::I18nRoutes::AliasMapping
103
121
 
104
122
  if path.end_with?('/')
105
123
  normalized_pieces << ""
124
+ found_langs << @default_lang
106
125
  end
107
126
 
108
- return normalized_pieces.join('/')
127
+ normalized_path = normalized_pieces.join('/')
128
+
129
+ return normalized_path, found_langs
109
130
  end
110
131
 
132
+ # @return [(String, Object)]
133
+
111
134
  def normalization_for(piece, aliases)
112
135
  if aliases.nil?
113
- return nil
136
+ return nil, @default_lang
114
137
  end
115
138
 
116
139
  entities = aliases.keys
117
140
  entities.each do |entity|
118
141
  if piece == entity
119
- return entity
142
+ return entity, @default_lang
120
143
  end
121
144
 
122
145
  subentities = aliases[entity].values.reject { |e| e.is_a? Hash }
123
- if subentities.any? { |subentity| Array(subentity).any? { |sube| piece == sube } }
124
- return entity
146
+ subentity = subentities.find { |s| Array(s).any? { |sube| piece == sube } }
147
+ if !subentity.nil?
148
+ return entity, aliases[entity].index(subentity)
125
149
  end
126
150
  end
127
151
 
128
- return nil
152
+ # the piece is not present in the aliases
153
+
154
+ return nil, @default_lang
129
155
  end
130
156
  end
@@ -46,15 +46,28 @@ class Rack::I18nRoutes::AliasMappingUpdater
46
46
  #
47
47
  # @param [Proc] new_aliases_fn a parameter-less function that returns
48
48
  # the new aliases
49
+ # @param [Hash] opts the options to be passed to the underlying
50
+ # AliasMapping object
49
51
 
50
- def initialize(new_aliases_fn)
52
+ def initialize(new_aliases_fn, opts = {})
51
53
  @new_aliases_fn = new_aliases_fn
54
+ @opts = opts
52
55
  end
53
56
 
57
+ # @return [String]
58
+
54
59
  def map(path)
60
+ normalized_path, found_langs = map_with_langs(path)
61
+
62
+ return normalized_path
63
+ end
64
+
65
+ # @return [(String, Array<Object>)]
66
+
67
+ def map_with_langs(path)
55
68
  aliases = @new_aliases_fn[]
56
- alias_mapping = Rack::I18nRoutes::AliasMapping.new(aliases)
69
+ alias_mapping = Rack::I18nRoutes::AliasMapping.new(aliases, @opts)
57
70
 
58
- return alias_mapping.map(path)
71
+ return alias_mapping.map_with_langs(path)
59
72
  end
60
73
  end
@@ -25,29 +25,35 @@ TEST_ALIASES = {
25
25
  'paintings' => {
26
26
  'fra' => 'peintures',
27
27
  'spa' => 'pinturas',
28
+
29
+ :children => {
30
+ 'gioconda' => {
31
+ 'fra' => 'joconde',
32
+ }
33
+ }
28
34
  }
29
35
  }
30
36
 
31
- describe Rack::I18nRoutes do
32
- def app(*opts)
33
- builder = Rack::Builder.new do
34
- use Rack::Lint
35
- use Rack::I18nRoutes, *opts
36
- use Rack::Lint
37
-
38
- run lambda { |env| [200, {"Content-Type" => "text/plain"}, [""]] }
39
- end
37
+ def app(*opts)
38
+ builder = Rack::Builder.new do
39
+ use Rack::Lint
40
+ use Rack::I18nRoutes, *opts
41
+ use Rack::Lint
40
42
 
41
- return builder.to_app
43
+ run lambda { |env| [200, {"Content-Type" => "text/plain"}, [""]] }
42
44
  end
43
45
 
44
- def request_with(path, mapping_fn)
45
- session = Rack::Test::Session.new(Rack::MockSession.new(app(mapping_fn)))
46
- session.request(path)
46
+ return builder.to_app
47
+ end
47
48
 
48
- return session.last_request
49
- end
49
+ def request_with(path, mapping_fn)
50
+ session = Rack::Test::Session.new(Rack::MockSession.new(app(mapping_fn)))
51
+ session.request(path)
52
+
53
+ return session.last_request
54
+ end
50
55
 
56
+ describe Rack::I18nRoutes do
51
57
  context "with an AliasMapping" do
52
58
  let(:mapping) { Rack::I18nRoutes::AliasMapping.new(TEST_ALIASES) }
53
59
 
@@ -113,3 +119,28 @@ describe Rack::I18nRoutes do
113
119
  end
114
120
  end
115
121
 
122
+ describe Rack::I18nRoutes::AliasMapping do
123
+ let(:default_lang) { 'test-lang' }
124
+ let(:mapping) { Rack::I18nRoutes::AliasMapping.new(TEST_ALIASES, :default => default_lang) }
125
+
126
+ context "with a :default option set" do
127
+ it "returns the default key for normalized paths" do
128
+ ph, found_langs = mapping.map_with_langs('/paintings/gioconda/')
129
+
130
+ found_langs.should == [default_lang]*4
131
+ end
132
+
133
+ it "returns the non-default key when set" do
134
+ ph, found_langs = mapping.map_with_langs('/articulos/la-victoire/')
135
+
136
+ found_langs.should == [default_lang, 'spa', 'fra', default_lang]
137
+ end
138
+
139
+ it "returns the default key for unknown paths" do
140
+ ph, found_langs = mapping.map_with_langs('/articulos/fancy/')
141
+
142
+ found_langs.should == [default_lang, 'spa', default_lang, default_lang]
143
+ end
144
+ end
145
+ end
146
+
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-i18n_routes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Gioele Barabucci