british 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/british.rb +115 -57
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca12a1028d620d5acb7449788a9cc4cd9d687c6e
4
- data.tar.gz: abf7dc891caacbaa8af865505b4239cb3cbe0c57
3
+ metadata.gz: 0237633aac781d2eb9414af40b9ca1975ac4f99c
4
+ data.tar.gz: 5db567584c645c78ba84c2c9a13e4259bf0a04ca
5
5
  SHA512:
6
- metadata.gz: a3cddb6ed91db752f4a1732af9532627cfe0ce08b51126e10a625a2e86fab01a4217b9da0f73c57e0646e40d4a6dc8c1dfac0fb8ba763bc8992314d68e0aabe8
7
- data.tar.gz: 212803711ca2b2f0a86ff3c714843d244b214e97cf8a8b2da01d5d005c7736c3fa221716d8fea69f325d0c9f786f3101753cf800602d60b085db5214afcb57c7
6
+ metadata.gz: 10a7474b9c6dce19165ef13fda273f4d75dc1b05feae0db8305ac0e9e5b813d691e63ecae60ffad82d2b983e8cd3b8cb2f4a486793d904cfe28eea10db6a3f13
7
+ data.tar.gz: 04d4af10b3b0adaaa187bf5d4df8d4b4cb9c68406f260fa68227591c3b99aa0d33d35208582e22d248f5c4b273de75059a33f955e05eae4e9b6c8157ee20fbdf
data/lib/british.rb CHANGED
@@ -1,53 +1,68 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Public: method_missing which tries to call "British" version before failing
4
- # Could be included to the particular class or globally (monkey-patching Object)
3
+ # Public: method_missing which tries to call "British"/"American" version before failing
4
+ # Could be included/extended to/by the particular class or globally (monkey-patching Object)
5
5
  #
6
6
  # Examples
7
7
  #
8
- # # Create classes with `initialise` constructor (British::Initialisable)
9
- # # And magic British methods and attributes (British)
10
- # class BritishObject
8
+ # Create classes with `initialise` constructor
9
+ # And your own British methods and attributes
11
10
  # require 'british'
12
11
  #
13
- # # use within your objects only *1 …
14
- # include British
15
- # include British::Initialisable
12
+ # class BritishObject < BlackBoxObject
13
+ # # use British::Initialisable within your classes only *1 …
14
+ # include British::Initialisable
16
15
  #
17
- # attr_reader :color
16
+ # attr_reader :colour
18
17
  #
19
- # # works exactly like an initialize (including `super` usage)
20
- # def initialise(test)
21
- # @test = test
22
- # @color = 'red'
18
+ # # works exactly like an initialize (including `super` usage)
19
+ # def initialise(test)
20
+ # @test = test
21
+ # @colour = 'red'
23
22
  #
24
- # super('black', 'box', 'arguments')
25
- # end
23
+ # super('black', 'box', 'arguments')
24
+ # end
26
25
  #
27
- # def magnetize(test)
28
- # @test
26
+ # def magnetise(test)
27
+ # @test
28
+ # end
29
29
  # end
30
- # end
31
30
  #
32
- # british_object = BritishObject.new('Hello UK!')
33
- # british_object.test # => 'Hello UK!'
31
+ # british_object = BritishObject.new('Hello UK!')
32
+ # british_object.test # => 'Hello UK!'
33
+ #
34
+ # # Use American or British endings with any method or attribute
35
+ # british_object.color # => "red"
36
+ # british_object.colour # => "red"
37
+ #
38
+ # british_object.magnetize # => "Hello UK!"
39
+ # british_object.magnetise # => "Hello UK!"
40
+ #
41
+ # # *1 … patch third party or all the system Objects
42
+ # # (wouldn't really recommend to do the last one)
43
+ # String.include(British)
44
+ # 'cheers!'.capitalise # => "Cheers!"
45
+ #
46
+ # require 'active_support/inflector'
47
+ # include British
34
48
  #
35
- # # Use British endings with any method or attribute
36
- # british_object.colour # => "red"
37
- # british_object.magnetise # => "Hello UK!"
49
+ # 'cheers!'.capitalise # => "Cheers!"
50
+ # 'oi_ya_bloody_wanker'.camelise # => "OiYaBloodyWanker"
38
51
  #
39
- # # *1 patch third party or all the system Objects
40
- # String.include British
41
- # 'cheers!'.capitalise # => "Cheers!"
52
+ # # Extend an object instance to make it British
53
+ # not_british = SomeClass.new # with #color method
54
+ # refugee = SomeClass.new # with #color method
42
55
  #
43
- # require 'active_support/inflector'
44
- # include British
56
+ # # Make it British
57
+ # british = refugee.extend(British)
45
58
  #
46
- # # Use is_an? with classes like an Array!
47
- # [].is_an? Array # => true
59
+ # not_british.colour # undefined method `colour'
60
+ # british.colour # works well
61
+ #
62
+ # # Use is_an?/an? with classes like an Array!
63
+ # [].is_an? Array # => true
64
+ # [].an? Array # => true
48
65
  #
49
- # 'cheers!'.capitalise # => "Cheers!"
50
- # 'oi_ya_bloody_wanker'.camelise # => "OiYaBloodyWanker"
51
66
  module British
52
67
  # Public: Hash of British ↔ American words endings
53
68
  ENDINGS = {
@@ -65,18 +80,24 @@ module British
65
80
  }.freeze
66
81
 
67
82
  # Public: Regexp pattern to search/replace British words endings
68
- BRITISH_ENDING_PATTERN = /#{Regexp.union(ENDINGS.keys)}(?=_|-|\?|\!|=|$)/
83
+ BRITISH_ENDING_PATTERN = /#{Regexp.union(ENDINGS.keys)}(?=_|-|\?|\!|=|$)/
84
+
85
+ # Public: Regexp pattern to search/replace American words endings
86
+ AMERICAN_ENDING_PATTERN = /#{Regexp.union(ENDINGS.values)}(?=_|-|\?|\!|=|$)/
69
87
 
70
88
  # Public: Submodule to be included in your own classes to use `initialise`
89
+ # and allow American methods to be called from outside
71
90
  #
72
- # As far as `initialize` called automatically by a `new` method there is no
73
- # sense to use it for third party classes.
91
+ # Warning: as far as `initialize` called automatically by a `new` method there
92
+ # is no sense to use it for third party classes. Use `include British` instead.
74
93
  #
75
94
  module Initialisable
76
- # Public: On module being included do:
95
+ # Public: On British::Initialisable module being included do:
77
96
  # 1. Check if it's a global include
78
97
  # 2. Add and alias of the parent's `initialize` (for `super` calls)
79
98
  # 3. Create your own initialize method (to be auto-called by the `new`)
99
+ # 4. Patch a class with British magic `method_missing`
100
+ # 5. Add aliases for `is_a?`
80
101
  #
81
102
  # Warning
82
103
  # By including this module you redefine your initialiZe method.
@@ -107,14 +128,20 @@ module British
107
128
 
108
129
  $VERBOSE = verbose
109
130
  end
131
+
132
+ host_class.extend ClassMethods
133
+ host_class.class_overwrite_method_missing
134
+
135
+ alias_method(:is_an?, :is_a?)
136
+ alias_method(:an?, :is_an?)
110
137
  end
111
138
  end
112
139
 
113
- # Public: aliases for is_an?/is_a? methods
140
+ # Public: aliases (is_an?/an?) for is_a? method
114
141
  alias is_an? is_a?
115
142
  alias an? is_an?
116
143
 
117
- # Hook to be called when British module being included
144
+ # Hook to be called when British module being included itself
118
145
  # Add method_missing method with all the British 'magic' behaviour
119
146
  # Extends a class to make it British
120
147
  #
@@ -136,9 +163,18 @@ module British
136
163
  host_class.extend ClassMethods
137
164
  host_class.class_overwrite_method_missing
138
165
 
139
- host_class.instance_eval do
140
- def method_added(name)
141
- class_overwrite_method_missing if name == :method_missing
166
+ # Inject our own method_added hook to catch it when
167
+ # `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
142
178
  end
143
179
  end
144
180
  end
@@ -163,9 +199,18 @@ module British
163
199
  host_object.extend ClassMethods
164
200
  host_object.object_overwrite_method_missing
165
201
 
166
- host_object.instance_eval do
167
- def singleton_method_added(name)
168
- object_overwrite_method_missing if name == :method_missing
202
+ # Inject our own singleton_method_added hook to catch it when
203
+ # `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
169
214
  end
170
215
  end
171
216
  end
@@ -174,19 +219,23 @@ module British
174
219
  # Defines an?, is_an?, method_missing
175
220
  module ClassMethods
176
221
  # Public: method to overwrite original method_missing with a magic one:
177
- # this method_missing tries to translate British methods to American
178
- # ones before throwing NoMethodError if neither method was found. Works
179
- # on a class level.
222
+ # this method_missing tries to translate British methods to American ones
223
+ # (or vice versa) before throwing NoMethodError if neither method was found.
224
+ # Works on a class level.
180
225
  #
181
226
  # name - original method name
182
227
  # *args - original method args
183
228
  #
184
229
  # Example:
185
230
  #
186
- # # with any British object
231
+ # # with any American object
187
232
  # british_object.colour # will be translated into color
188
233
  # british_object.magnetise # will be translated into magnetize
189
234
  #
235
+ # # with any British object
236
+ # british_object.color # will be translated into colour
237
+ # british_object.magnetize # will be translated into magnetise
238
+ #
190
239
  # # all method endings are allowed
191
240
  # british_object.surprize!
192
241
  # british_object.surprize?
@@ -202,10 +251,16 @@ module British
202
251
  class_eval do
203
252
  unless method_defined?(:british_method_missing)
204
253
  define_method(:british_method_missing) do |name, *args|
205
-
206
- # do British magic
207
- americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
208
- return send(americanised_name, *args) if respond_to?(americanised_name)
254
+ # When in our own British class
255
+ if self.class.include?(British::Initialisable)
256
+ # do American magic
257
+ britanised_name = name.to_s.gsub(AMERICAN_ENDING_PATTERN, ENDINGS.invert)
258
+ return send(britanised_name, *args) if respond_to?(britanised_name)
259
+ else
260
+ # do British magic
261
+ americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
262
+ return send(americanised_name, *args) if respond_to?(americanised_name)
263
+ end
209
264
 
210
265
  # call original method_missing (avoid double original method calls)
211
266
  return original_method_missing(name, *args) if caller[0] !~ /method_missing/ && defined?(:original_method_missing)
@@ -224,19 +279,23 @@ module British
224
279
  end
225
280
 
226
281
  # Public: method to overwrite original method_missing with a magic one:
227
- # this method_missing tries to translate British methods to American
228
- # ones before throwing NoMethodError if neither method was found. Works
229
- # on an instance level.
282
+ # this method_missing tries to translate British methods to American ones
283
+ # (or vice versa) before throwing NoMethodError if neither method was found.
284
+ # Works on an instance level.
230
285
  #
231
286
  # name - original method name
232
287
  # *args - original method args
233
288
  #
234
289
  # Example:
235
290
  #
236
- # # with any British object
291
+ # # with any American object
237
292
  # british_object.colour # will be translated into color
238
293
  # british_object.magnetise # will be translated into magnetize
239
294
  #
295
+ # # with any British object
296
+ # british_object.color # will be translated into colour
297
+ # british_object.magnetize # will be translated into magnetise
298
+ #
240
299
  # # all method endings are allowed
241
300
  # british_object.surprize!
242
301
  # british_object.surprize?
@@ -252,7 +311,6 @@ module British
252
311
  instance_eval do
253
312
  unless respond_to?(:british_method_missing)
254
313
  def british_method_missing(name, *args)
255
- $stdout.flush
256
314
  # do British magic
257
315
  americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
258
316
  return send(americanised_name, *args) if respond_to?(americanised_name)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: british
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.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-28 00:00:00.000000000 Z
11
+ date: 2016-09-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Allows to use alternative words endings: -ise instead of -ize etc. Defines
14
14
  `is_an?` as an alias of the is_a? method. Provides module to use `initialise` in