british 0.1.1 → 0.2.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 +65 -34
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c090e831e4a64a95f1feead19c0fc31e4e33df9
4
- data.tar.gz: c943d73c87c99f5c0a983defde95ba647b36d6d9
3
+ metadata.gz: 3836974f5f6e65cc936f70cf0c34dad3eb98f9b7
4
+ data.tar.gz: 7ec6347cb6583f9cea09d399e32e277a60ce6753
5
5
  SHA512:
6
- metadata.gz: 4b56de9b11c79a9a4d82742199ffd5e04f0e1f9975f07b15d2f68ce28e3eef9fe6265c369e75582a182fd0deb721699a844a8fccf28de9d435c8a2ef61d74ab3
7
- data.tar.gz: aa63e6ecedae8e29029d64af7395ea7c6a626e39da81d4bbbb7778547252b926e6fa4e9d93abb40fa99a6a9dcbaaa66e2535e09d903a2c8f372fa917d8b164f2
6
+ metadata.gz: 2efa86bab2c4438f3bba0b8831e57a727019f01cde7b6e8187c50b049636dbb31a86bddcab39915fbd32e3d70cbcc1c213ef0aad1cd9404cd89e67fc64b130f5
7
+ data.tar.gz: f103153e8c950d6397c718025eb1c25516d8fba309c10380ae64755f4ca9dfd3c65c17809cc80bcdb701d16d877498258401ff256f298483a4d578037030aed7
data/lib/british.rb CHANGED
@@ -62,7 +62,10 @@ module British
62
62
  'isation' => 'ization',
63
63
  'yse' => 'yze',
64
64
  'ogue' => 'og'
65
- }
65
+ }.freeze
66
+
67
+ # Public: Regexp pattern to search/replace British words endings
68
+ BRITISH_ENDING_PATTERN = /#{Regexp.union(ENDINGS.keys)}(?=_|-|\?|\!|=|$)/
66
69
 
67
70
  # Public: Submodule to be included in your own classes to use `initialise`
68
71
  #
@@ -94,46 +97,74 @@ module British
94
97
  # Public: British alias of native is_a? method
95
98
  # Returns the original method's result
96
99
  def is_an?(*args)
97
- is_a?(*args) if self.respond_to?(:is_a?)
100
+ is_a?(*args) if respond_to?(:is_a?)
98
101
  end
99
102
 
100
103
  # Public: additional alias for is_an?/is_a? method
101
- alias_method :an?, :is_an?
104
+ alias an? is_an?
102
105
 
103
- # Public: Magic method which tries to translate British methods to American
104
- # methods before throwing NoMethodError if neither method was found.
105
- #
106
- # name - original method name
107
- # *args - original method args
108
- #
109
- # Example
110
- #
111
- # # with any British object
112
- # british_object.colour # will be translated into color
113
- # british_object.magnetise # will be translated into magnetize
114
- #
115
- # # all method endings age allowed
116
- # british_object.surprize!
117
- # british_object.surprize?
118
- # british_object.surprize=
119
- #
120
- # # complex names are supported
121
- # british_object.initialise_something # initialize_something will be called
122
- #
123
- # Returns the original method's result
124
- # Raises NoMethodError if the method cannot be found.
125
- def method_missing(name, *args)
126
- name = name.to_s
106
+ def self.included(host_class)
107
+ host_class.extend ClassMethods
108
+ host_class.overwrite_method_missing
127
109
 
128
- ENDINGS.each_pair do |bre_e, ame_e|
129
- next unless name.include?(bre_e)
110
+ host_class.instance_eval do
111
+ def method_added(name)
112
+ return if name != :method_missing
113
+ overwrite_method_missing
114
+ end
115
+ end
116
+ end
130
117
 
131
- british_ending_pattern = /#{bre_e}(?=_|-|$|\?|\!|=)/
132
- americanised_name = name.gsub(british_ending_pattern, ame_e)
118
+ # Public: ClassMethods to extend in self.included
119
+ # Defines an?, is_an?, method_missing
120
+ module ClassMethods
133
121
 
134
- return send(americanised_name, *args) if respond_to?(americanised_name)
135
- end
122
+ # Public: method to overwrite original method_missing with magic one:
123
+ # this method_missing tries to translate British methods to American
124
+ # ones before throwing NoMethodError if neither method was found.
125
+ #
126
+ # name - original method name
127
+ # *args - original method args
128
+ #
129
+ # Example
130
+ #
131
+ # # with any British object
132
+ # british_object.colour # will be translated into color
133
+ # british_object.magnetise # will be translated into magnetize
134
+ #
135
+ # # all method endings age allowed
136
+ # british_object.surprize!
137
+ # british_object.surprize?
138
+ # british_object.surprize=
139
+ #
140
+ # # complex names are supported
141
+ # british_object.initialise_something # initialize_something will be called
142
+ #
143
+ # Returns the original method's result
144
+ # Calls original method_missing (if British didn't hook anything)
145
+ # Raises NoMethodError if the method cannot be found
146
+ def overwrite_method_missing
147
+ class_eval do
148
+ unless method_defined?(:british_method_missing)
149
+ define_method(:british_method_missing) do |name, *args|
150
+ # do British magic
151
+ americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
152
+ return send(americanised_name, *args) if respond_to?(americanised_name)
136
153
 
137
- fail NoMethodError, "undefined method `#{name}' for #{self}"
154
+ # call original method_missing (avoid double original method calls)
155
+ return original_method_missing(name, *args) if caller[0] !~ /method_missing/ && defined?(:original_method_missing)
156
+
157
+ # call original parent's method_missing
158
+ method = self.class.superclass.instance_method(:original_method_missing)
159
+ return method.bind(self).call(name, *args) if method
160
+ end
161
+ end
162
+
163
+ if instance_method(:method_missing) != instance_method(:british_method_missing)
164
+ alias_method :original_method_missing, :method_missing
165
+ alias_method :method_missing, :british_method_missing
166
+ end
167
+ end
168
+ end
138
169
  end
139
170
  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.1.1
4
+ version: 0.2.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-01-27 00:00:00.000000000 Z
11
+ date: 2016-03-06 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.1
42
+ rubygems_version: 2.5.2
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.