british 0.2.2 → 0.2.3

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 +119 -12
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2de5cefa81b5c10cf76b3021b29272e9eedcc8fc
4
- data.tar.gz: 1c29c1ac92f4e2a4e6ef21fc527d4d1068850c8c
3
+ metadata.gz: ca12a1028d620d5acb7449788a9cc4cd9d687c6e
4
+ data.tar.gz: abf7dc891caacbaa8af865505b4239cb3cbe0c57
5
5
  SHA512:
6
- metadata.gz: 6924d0a0f859e41404787bf4d52298bb006f1977662e52269241d206bcd7fcabe1cd3367064bc65cc8ba05be61b9b50020b9e3e81046dae48c5f72f6435b62b9
7
- data.tar.gz: 0e743a804a051fa72490c1b134192fa7dc5aea33e829d830d49dbd0956bac635dc9a154f8389476dd998e66cbca1069adbfcd941116824ff765b6ada1cc276fa
6
+ metadata.gz: a3cddb6ed91db752f4a1732af9532627cfe0ce08b51126e10a625a2e86fab01a4217b9da0f73c57e0646e40d4a6dc8c1dfac0fb8ba763bc8992314d68e0aabe8
7
+ data.tar.gz: 212803711ca2b2f0a86ff3c714843d244b214e97cf8a8b2da01d5d005c7736c3fa221716d8fea69f325d0c9f786f3101753cf800602d60b085db5214afcb57c7
data/lib/british.rb CHANGED
@@ -78,7 +78,14 @@ module British
78
78
  # 2. Add and alias of the parent's `initialize` (for `super` calls)
79
79
  # 3. Create your own initialize method (to be auto-called by the `new`)
80
80
  #
81
- # Returns nothing.
81
+ # Warning
82
+ # By including this module you redefine your initialiZe method.
83
+ # Use initialiSe method instead of the original, but not both!
84
+ #
85
+ # Example:
86
+ # include British::Initialisable
87
+ #
88
+ # Returns nothing
82
89
  def self.included(host_class)
83
90
  unless host_class == Object
84
91
  # alias parent's initialise method
@@ -86,10 +93,19 @@ module British
86
93
  alias_method :initialise, :initialize
87
94
  end
88
95
 
89
- # create own initialize method
96
+ # suppress 'method redefined; discarding old initialize' warning
97
+ # https://goo.gl/PSzrbF ¯\_(ツ)_/¯
98
+ verbose = $VERBOSE
99
+ $VERBOSE = nil
100
+
101
+ # Once again: since we use this Initialisable module in our classes
102
+ # ONLY, and create our own initialiSe method, we can't break anything
103
+ # by redefining initialiZe
90
104
  define_method :initialize do |*args|
91
105
  initialise(*args)
92
106
  end
107
+
108
+ $VERBOSE = verbose
93
109
  end
94
110
  end
95
111
  end
@@ -98,36 +114,80 @@ module British
98
114
  alias is_an? is_a?
99
115
  alias an? is_an?
100
116
 
117
+ # Hook to be called when British module being included
118
+ # Add method_missing method with all the British 'magic' behaviour
119
+ # Extends a class to make it British
120
+ #
121
+ # Example:
122
+ #
123
+ # class Example
124
+ # include British
125
+ #
126
+ # def color
127
+ # 'red'
128
+ # end
129
+ # end
130
+ #
131
+ # example = Example.new
132
+ # example.colour # => "red"
133
+ #
134
+ # Returns nothing
101
135
  def self.included(host_class)
102
136
  host_class.extend ClassMethods
103
- host_class.overwrite_method_missing
137
+ host_class.class_overwrite_method_missing
104
138
 
105
139
  host_class.instance_eval do
106
140
  def method_added(name)
107
- return if name != :method_missing
108
- overwrite_method_missing
141
+ class_overwrite_method_missing if name == :method_missing
109
142
  end
110
143
  end
111
144
  end
112
145
 
113
- # Public: ClassMethods to extend in self.included
146
+ # Hook to be called when British module being used to extend an object
147
+ # Add method_missing method with all the British 'magic' behaviour
148
+ # Extends an object instance to make it British
149
+ #
150
+ # Example:
151
+ #
152
+ # not_british = SomeClass.new # with color method
153
+ # refugee = SomeClass.new # with color method
154
+ #
155
+ # # Make it British
156
+ # british = refugee.extend(British)
157
+ #
158
+ # not_british.colour # undefined method `colour'
159
+ # british.colour # works well
160
+ #
161
+ # Returns nothing
162
+ def self.extended(host_object)
163
+ host_object.extend ClassMethods
164
+ host_object.object_overwrite_method_missing
165
+
166
+ host_object.instance_eval do
167
+ def singleton_method_added(name)
168
+ object_overwrite_method_missing if name == :method_missing
169
+ end
170
+ end
171
+ end
172
+
173
+ # Public: ClassMethods to extend in self.included/self.extended
114
174
  # Defines an?, is_an?, method_missing
115
175
  module ClassMethods
116
-
117
- # Public: method to overwrite original method_missing with magic one:
176
+ # Public: method to overwrite original method_missing with a magic one:
118
177
  # this method_missing tries to translate British methods to American
119
- # ones before throwing NoMethodError if neither method was found.
178
+ # ones before throwing NoMethodError if neither method was found. Works
179
+ # on a class level.
120
180
  #
121
181
  # name - original method name
122
182
  # *args - original method args
123
183
  #
124
- # Example
184
+ # Example:
125
185
  #
126
186
  # # with any British object
127
187
  # british_object.colour # will be translated into color
128
188
  # british_object.magnetise # will be translated into magnetize
129
189
  #
130
- # # all method endings age allowed
190
+ # # all method endings are allowed
131
191
  # british_object.surprize!
132
192
  # british_object.surprize?
133
193
  # british_object.surprize=
@@ -138,10 +198,11 @@ module British
138
198
  # Returns the original method's result
139
199
  # Calls original method_missing (if British didn't hook anything)
140
200
  # Raises NoMethodError if the method cannot be found
141
- def overwrite_method_missing
201
+ def class_overwrite_method_missing
142
202
  class_eval do
143
203
  unless method_defined?(:british_method_missing)
144
204
  define_method(:british_method_missing) do |name, *args|
205
+
145
206
  # do British magic
146
207
  americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
147
208
  return send(americanised_name, *args) if respond_to?(americanised_name)
@@ -161,5 +222,51 @@ module British
161
222
  end
162
223
  end
163
224
  end
225
+
226
+ # 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.
230
+ #
231
+ # name - original method name
232
+ # *args - original method args
233
+ #
234
+ # Example:
235
+ #
236
+ # # with any British object
237
+ # british_object.colour # will be translated into color
238
+ # british_object.magnetise # will be translated into magnetize
239
+ #
240
+ # # all method endings are allowed
241
+ # british_object.surprize!
242
+ # british_object.surprize?
243
+ # british_object.surprize=
244
+ #
245
+ # # complex names are supported
246
+ # british_object.initialise_something # initialize_something will be called
247
+ #
248
+ # Returns the original method's result
249
+ # Calls original method_missing (if British didn't hook anything)
250
+ # Raises NoMethodError if the method cannot be found
251
+ def object_overwrite_method_missing
252
+ instance_eval do
253
+ unless respond_to?(:british_method_missing)
254
+ def british_method_missing(name, *args)
255
+ $stdout.flush
256
+ # do British magic
257
+ americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
258
+ return send(americanised_name, *args) if respond_to?(americanised_name)
259
+
260
+ # call original method_missing (avoid double original method calls)
261
+ return original_method_missing(name, *args) if caller[0] !~ /method_missing/ && defined?(:original_method_missing)
262
+ end
263
+ end
264
+
265
+ if method(:method_missing) != method(:british_method_missing)
266
+ alias :original_method_missing :method_missing
267
+ alias :method_missing :british_method_missing
268
+ end
269
+ end
270
+ end
164
271
  end
165
272
  end
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.2
4
+ version: 0.2.3
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-06-06 00:00:00.000000000 Z
11
+ date: 2016-09-28 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
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  version: '0'
40
40
  requirements: []
41
41
  rubyforge_project:
42
- rubygems_version: 2.5.2
42
+ rubygems_version: 2.5.1
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: A tiny module that is supposed to help Brits to use Ruby with more comfort