british 0.3.0 → 0.4.0

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/british.rb +63 -27
  3. metadata +5 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0237633aac781d2eb9414af40b9ca1975ac4f99c
4
- data.tar.gz: 5db567584c645c78ba84c2c9a13e4259bf0a04ca
3
+ metadata.gz: 7115edabb03d3d4647a323d60496b44acf8d90c3
4
+ data.tar.gz: 186e32e55afdc4baf90854b20f9c4185e686f351
5
5
  SHA512:
6
- metadata.gz: 10a7474b9c6dce19165ef13fda273f4d75dc1b05feae0db8305ac0e9e5b813d691e63ecae60ffad82d2b983e8cd3b8cb2f4a486793d904cfe28eea10db6a3f13
7
- data.tar.gz: 04d4af10b3b0adaaa187bf5d4df8d4b4cb9c68406f260fa68227591c3b99aa0d33d35208582e22d248f5c4b273de75059a33f955e05eae4e9b6c8157ee20fbdf
6
+ metadata.gz: 6bb4952776e2e9f52f73dcf6757a1d9bc3827128f5a79c98ab86e8aaa71f6ff8d95b9f33bec79c553033e92f35f8988405d56aa7ffcc9feffe4cad8564b882ed
7
+ data.tar.gz: 90576b1bb474d3d5df85475071e8a380891f1be5b3e51f52bd4fe19483cfcce603ce3fb94a248f6ba968c5dc887b407493c18c48202d901939e6e681c9478016
data/lib/british.rb CHANGED
@@ -79,11 +79,26 @@ module British
79
79
  'ogue' => 'og'
80
80
  }.freeze
81
81
 
82
+ # Public: Hash of American ↔ British words endings
83
+ INVERTED_ENDINGS = {
84
+ # Latin-derived spellings
85
+ 'or' => 'our',
86
+ 'er' => 're',
87
+ 'se' => 'ce',
88
+ 'ction' => 'xion',
89
+
90
+ # Greek-derived spellings
91
+ 'ize' => 'ise',
92
+ 'ization' => 'isation',
93
+ 'yze' => 'yse',
94
+ 'og' => 'ogue'
95
+ }.freeze
96
+
82
97
  # Public: Regexp pattern to search/replace British words endings
83
- BRITISH_ENDING_PATTERN = /#{Regexp.union(ENDINGS.keys)}(?=_|-|\?|\!|=|$)/
98
+ BRITISH_ENDING_PATTERN = /#{Regexp.union(ENDINGS.keys)}(?=_|\?|\!|=|$)/
84
99
 
85
100
  # Public: Regexp pattern to search/replace American words endings
86
- AMERICAN_ENDING_PATTERN = /#{Regexp.union(ENDINGS.values)}(?=_|-|\?|\!|=|$)/
101
+ AMERICAN_ENDING_PATTERN = /#{Regexp.union(ENDINGS.values)}(?=_|\?|\!|=|$)/
87
102
 
88
103
  # Public: Submodule to be included in your own classes to use `initialise`
89
104
  # and allow American methods to be called from outside
@@ -163,19 +178,19 @@ module British
163
178
  host_class.extend ClassMethods
164
179
  host_class.class_overwrite_method_missing
165
180
 
181
+ return unless host_class.private_methods(true).include?(:method_added)
182
+
166
183
  # Inject our own method_added hook to catch it when
167
184
  # `method_missing` is added
168
- if host_class.private_methods(true).include?(:method_added)
169
- host_class.instance_eval do
170
- def british_method_added(name)
171
- original_method_added(name)
172
- class_overwrite_method_missing if name == :method_missing
173
- end
174
-
175
- # do not mess with others :method_added
176
- alias original_method_added method_added
177
- alias method_added british_method_added
185
+ host_class.instance_eval do
186
+ def british_method_added(name)
187
+ original_method_added(name)
188
+ class_overwrite_method_missing if name == :method_missing
178
189
  end
190
+
191
+ # do not mess with others :method_added
192
+ alias original_method_added method_added
193
+ alias method_added british_method_added
179
194
  end
180
195
  end
181
196
 
@@ -199,19 +214,19 @@ module British
199
214
  host_object.extend ClassMethods
200
215
  host_object.object_overwrite_method_missing
201
216
 
217
+ return unless host_object.private_methods(true).include?(:singleton_method_added)
218
+
202
219
  # Inject our own singleton_method_added hook to catch it when
203
220
  # `method_missing` is added
204
- if host_object.private_methods(true).include?(:singleton_method_added)
205
- host_object.instance_eval do
206
- def british_singleton_method_added(name)
207
- original_singleton_method_added(name)
208
- object_overwrite_method_missing if name == :method_missing
209
- end
210
-
211
- # do not mess with others :singleton_method_added
212
- alias original_singleton_method_added singleton_method_added
213
- alias singleton_method_added british_singleton_method_added
221
+ host_object.instance_eval do
222
+ def british_singleton_method_added(name)
223
+ original_singleton_method_added(name)
224
+ object_overwrite_method_missing if name == :method_missing
214
225
  end
226
+
227
+ # do not mess with others :singleton_method_added
228
+ alias original_singleton_method_added singleton_method_added
229
+ alias singleton_method_added british_singleton_method_added
215
230
  end
216
231
  end
217
232
 
@@ -251,14 +266,30 @@ module British
251
266
  class_eval do
252
267
  unless method_defined?(:british_method_missing)
253
268
  define_method(:british_method_missing) do |name, *args|
269
+ binded_class = self.class
270
+
254
271
  # When in our own British class
255
- if self.class.include?(British::Initialisable)
272
+ if binded_class.include?(British::Initialisable)
273
+ name_str = name.to_s
274
+
256
275
  # do American magic
257
- britanised_name = name.to_s.gsub(AMERICAN_ENDING_PATTERN, ENDINGS.invert)
276
+ britanised_name = if name_str =~ /_/
277
+ name_str.gsub(AMERICAN_ENDING_PATTERN, INVERTED_ENDINGS)
278
+ else
279
+ name_str.sub(AMERICAN_ENDING_PATTERN, INVERTED_ENDINGS)
280
+ end
281
+
258
282
  return send(britanised_name, *args) if respond_to?(britanised_name)
259
283
  else
284
+ name_str = name.to_s
285
+
260
286
  # do British magic
261
- americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
287
+ americanised_name = if name_str =~ /_/
288
+ name_str.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
289
+ else
290
+ name_str.sub(BRITISH_ENDING_PATTERN, ENDINGS)
291
+ end
292
+
262
293
  return send(americanised_name, *args) if respond_to?(americanised_name)
263
294
  end
264
295
 
@@ -266,7 +297,7 @@ module British
266
297
  return original_method_missing(name, *args) if caller[0] !~ /method_missing/ && defined?(:original_method_missing)
267
298
 
268
299
  # call original parent's method_missing
269
- method = self.class.superclass.instance_method(:original_method_missing)
300
+ method = binded_class.superclass.instance_method(:original_method_missing)
270
301
  return method.bind(self).call(name, *args) if method
271
302
  end
272
303
  end
@@ -312,7 +343,12 @@ module British
312
343
  unless respond_to?(:british_method_missing)
313
344
  def british_method_missing(name, *args)
314
345
  # do British magic
315
- americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
346
+ americanised_name = if name.to_s =~ /_/
347
+ name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
348
+ else
349
+ name.to_s.sub(BRITISH_ENDING_PATTERN, ENDINGS)
350
+ end
351
+
316
352
  return send(americanised_name, *args) if respond_to?(americanised_name)
317
353
 
318
354
  # call original method_missing (avoid double original method calls)
metadata CHANGED
@@ -1,18 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: british
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serge Bedzhyk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-29 00:00:00.000000000 Z
11
+ date: 2016-12-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Allows to use alternative words endings: -ise instead of -ize etc. Defines
14
- `is_an?` as an alias of the is_a? method. Provides module to use `initialise` in
15
- your classes.'
14
+ `is_an?` as an alias of the is_a? method. Provides a module to use `initialise`
15
+ in your classes.'
16
16
  email: smileart21@gmail.com
17
17
  executables: []
18
18
  extensions: []
@@ -42,6 +42,5 @@ rubyforge_project:
42
42
  rubygems_version: 2.5.1
43
43
  signing_key:
44
44
  specification_version: 4
45
- summary: A tiny module that is supposed to help Brits to use Ruby with more comfort
45
+ summary: A tiny module which is supposed to help Brits to use Ruby with more comfort
46
46
  test_files: []
47
- has_rdoc: