mundo-pepino 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,7 +37,7 @@ module MundoPepino
37
37
  end
38
38
  end
39
39
 
40
- class NotFoundInDatabase
40
+ class NotFoundInDatabase < ResourceNotFound
41
41
  def initialize(model, value='')
42
42
  super "#{model} #{value} not found in database"
43
43
  end
@@ -88,10 +88,10 @@ module MundoPepino
88
88
  pile_up res
89
89
  end
90
90
 
91
- def add_resource_from_database(modelo, nombre)
92
- model = modelo.to_unquoted.to_model
91
+ def add_resource_from_database(raw_model, name)
92
+ model = raw_model.to_unquoted.to_model
93
93
  field = field_for(model)
94
- if resource = model.send("find_by_#{field}", nombre)
94
+ if resource = model.send("find_by_#{field}", name)
95
95
  pile_up resource
96
96
  else
97
97
  NotFoundInDatabase.new(model, name)
@@ -123,27 +123,31 @@ module MundoPepino
123
123
  end
124
124
  end
125
125
 
126
- def last_mentioned_of(modelo, with_name = nil)
127
- if model = modelo.to_model
126
+ def last_mentioned_of(raw_model, with_name = nil)
127
+ if model = raw_model.to_model
128
128
  resource = if with_name
129
- detect_first @resources.flatten, [model, with_name]
129
+ if mentioned = detect_first(resources_flatten, [model, with_name])
130
+ mentioned
131
+ else
132
+ add_resource_from_database(raw_model, with_name)
133
+ end
130
134
  elsif(last_mentioned.mr_model == model)
131
135
  last_mentioned
132
136
  else
133
137
  if group = recursive_group_search(model, @resources[1..-1])
134
138
  group
135
139
  else
136
- detect_first @resources.flatten, model
140
+ detect_first resources_flatten, model
137
141
  end
138
142
  end
139
143
  resource || raise(ResourceNotFound.new("model:#{model.name}, name:#{with_name||'nil'}"))
140
144
  else
141
- raise ModelNotMapped.new(modelo)
145
+ raise ModelNotMapped.new(raw_model)
142
146
  end
143
147
  end
144
148
 
145
149
  def last_mentioned_called(name)
146
- detect_first @resources.flatten, name
150
+ detect_first resources_flatten, name
147
151
  end
148
152
 
149
153
  def recursive_group_search(model, resources)
@@ -181,22 +185,26 @@ module MundoPepino
181
185
  if valores.size == mentioned.size
182
186
  [mentioned, valores]
183
187
  else
184
- [mentioned, [ valor ] * mentioned.size]
188
+ [mentioned, [valor] * mentioned.size]
185
189
  end
186
190
  else
187
- [[ mentioned ], [ valor ]]
191
+ [[mentioned], [valor]]
188
192
  end
189
193
  field, values = if (child_model = campo.to_model)
190
- child_name_field = field_for(mentioned.mr_model)
194
+ child_name_field = field_for(child_model)
191
195
  values = add_resource(child_model,
192
196
  valores.map { |val| { child_name_field => val } })
193
197
  values = [ values ] unless values.is_a?(Array)
194
- [ campo.to_field || child_model.name.underscore, values ]
198
+ [field_for(mentioned.mr_model, campo) || child_model.name.underscore, values]
195
199
  else
196
- [ field_for(mentioned.mr_model, campo), valores ]
200
+ [field_for(mentioned.mr_model, campo), valores]
197
201
  end
198
202
  [resources, field, values]
199
203
  end
204
+
205
+ def resources_flatten
206
+ (@resources || []).flatten
207
+ end
200
208
 
201
209
  def method_missing(method, *args, &block)
202
210
  if (method.to_s =~ /^last_mentioned_(.+)$/)
@@ -2,7 +2,7 @@ module MundoPepino #:nodoc:
2
2
  class VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 6
5
+ TINY = 7
6
6
  PATCH = nil # Set to nil for official release
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PATCH].compact.join('.')
data/lib/mundo_pepino.rb CHANGED
@@ -35,9 +35,10 @@ module MundoPepino
35
35
  String.add_mapper(:content_type,
36
36
  /\.png$/ => 'image/png',
37
37
  /\.jpe?g$/ => 'image/jpg',
38
- /\.gif$/ => 'image/gif') { |str| 'text/plain' }
39
- String.add_mapper(:underscored) { |string| string.gsub(/ +/, '_') }
40
- String.add_mapper(:unquoted) { |str| str =~ /^['"](.*)['"]$/ ? $1 : str}
38
+ /\.gif$/ => 'image/gif') {|str| 'text/plain'}
39
+ String.add_mapper(:underscored) {|string| string.gsub(/ +/, '_')}
40
+ String.add_mapper(:unquoted) {|str| str =~ /^['"](.*)['"]$/ ? $1 : str}
41
+ String.add_mapper(:regexp) {|str| str =~ /\/.+\/[a-z]*/ ? eval(str) : /#{Regexp.escape(str)}/m }
41
42
  String.add_mapper(:translated) do |str|
42
43
  if str =~ /^[a-z_]+\.[a-z_]+[a-z_\.]+$/
43
44
  I18n.translate(str, :default => str)
@@ -47,13 +48,7 @@ module MundoPepino
47
48
  str
48
49
  end
49
50
  end
50
- String.add_mapper(:url) do |string|
51
- if world.respond_to? :path_to
52
- world.path_to string
53
- else
54
- string if string =~ /^\/.*$|^https?:\/\//i
55
- end
56
- end
51
+ String.add_mapper(:url) {|str| str if str =~ /^\/.*$|^https?:\/\//i}
57
52
  end
58
53
 
59
54
  def config
@@ -24,6 +24,22 @@ MundoPepino.configure do |config|
24
24
  # TRADUCCIÓN ESPECÍFICA PARA UN MODELO
25
25
  # /^Orchard::longitud(es)?$/ => :longitude
26
26
  }
27
+
28
+ config.url_mappings = {
29
+ # TRADUCCIÓN DE RUTAS/URLS AQUÍ
30
+ # Hardcoded
31
+ #/^la página de registro/i => '/users/new',
32
+ # From app routes:
33
+ #/^la página de inicio de sesión/i => lambda {
34
+ # MundoPepino.world.new_session_path
35
+ #},
36
+ # And the fancy one: specific resource page
37
+ #/^la página de(?:l| la) (.+) ["'](.+)['"]$/ => lambda {|captures|
38
+ # if model = captures[0].to_model
39
+ # MundoPepino.world.send "#{model.name.underscore}_path", model.find_by_name(captures[1])
40
+ # end
41
+ #}
42
+ }
27
43
  end
28
44
 
29
45
  Before do
@@ -26,6 +26,22 @@ MundoPepino.configure do |config|
26
26
  # TRADUCCIÓN ESPECÍFICA PARA UN MODELO
27
27
  # /^Orchard::longitud(es)?$/ => 'longitude'
28
28
  }
29
+
30
+ config.url_mappings = {
31
+ # TRADUCCIÓN DE RUTAS/URLS AQUÍ
32
+ # Hardcoded
33
+ #/^la página de registro/i => '/users/new',
34
+ # From app routes:
35
+ #/^la página de inicio de sesión/i => lambda {
36
+ # MundoPepino.world.new_session_path
37
+ #},
38
+ # And the fancy one: specific resource page
39
+ #/^la página de(?:l| la) (.+) ["'](.+)['"]$/ => lambda {|captures|
40
+ # if model = captures[0].to_model
41
+ # MundoPepino.world.send "#{model.name.underscore}_path", model.find_by_name(captures[1])
42
+ # end
43
+ #}
44
+ }
29
45
  end
30
46
 
31
47
  Before do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mundo-pepino
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Fernando Garc\xC3\xADa Samblas"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-13 00:00:00 +01:00
12
+ date: 2009-11-23 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency