british 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/british.rb +139 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a34dfe5bf9cd65171a44729459b0638c8780cfa5
|
4
|
+
data.tar.gz: b9128cc10357654df255eae4dd625522add3d0b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e64fdc3a10c93847b6b5e544ea4ce957b825e3d92878e28614910f52c48fa6ad94fff5863d35590e0bfdcae02dbe7aa250f99417f72fe7cae4134bdb9f18f0c4
|
7
|
+
data.tar.gz: b7987b53f0c8ad135db28d6a2f625779441f4b1dde0810827edab847832d157f29879fc2cf980119fd7cb49440f0e6d35371554d949f5858ea8988e97214f4af
|
data/lib/british.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# encoding: utf-8
|
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)
|
5
|
+
#
|
6
|
+
# Examples
|
7
|
+
#
|
8
|
+
# # Create classes with `initialise` constructor (British::Initialisable)
|
9
|
+
# # And magic British methods and attributes (British)
|
10
|
+
# class BritishObject
|
11
|
+
# require 'british'
|
12
|
+
#
|
13
|
+
# # use within your objects only *1 …
|
14
|
+
# include British
|
15
|
+
# include British::Initialisable
|
16
|
+
#
|
17
|
+
# attr_reader :color
|
18
|
+
#
|
19
|
+
# # works exactly like an initialize (including `super` usage)
|
20
|
+
# def initialise(test)
|
21
|
+
# @test = test
|
22
|
+
# @color = 'red'
|
23
|
+
#
|
24
|
+
# super('black', 'box', 'arguments')
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# def magnetize(test)
|
28
|
+
# @test
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# british_object = BritishObject.new('Hello UK!')
|
33
|
+
# british_object.test # => 'Hello UK!'
|
34
|
+
#
|
35
|
+
# # Use British endings with any method or attribute
|
36
|
+
# british_object.colour # => "red"
|
37
|
+
# british_object.magnetise # => "Hello UK!"
|
38
|
+
#
|
39
|
+
# # *1 … patch third party or all the system Objects
|
40
|
+
# String.include British
|
41
|
+
# 'cheers!'.capitalise # => "Cheers!"
|
42
|
+
#
|
43
|
+
# require 'active_support/inflector'
|
44
|
+
# include British
|
45
|
+
#
|
46
|
+
# # Use is_an? with classes like an Array!
|
47
|
+
# [].is_an? Array # => true
|
48
|
+
#
|
49
|
+
# 'cheers!'.capitalise # => "Cheers!"
|
50
|
+
# 'oi_ya_bloody_wanker'.camelise # => "OiYaBloodyWanker"
|
51
|
+
module British
|
52
|
+
# Public: Hash of British ↔ American words endings
|
53
|
+
ENDINGS = {
|
54
|
+
# Latin-derived spellings
|
55
|
+
'our' => 'or',
|
56
|
+
're' => 'er',
|
57
|
+
'ce' => 'se',
|
58
|
+
'xion' => 'ction',
|
59
|
+
|
60
|
+
# Greek-derived spellings
|
61
|
+
'ise' => 'ize',
|
62
|
+
'isation' => 'ization',
|
63
|
+
'yse' => 'yze',
|
64
|
+
'ogue' => 'og'
|
65
|
+
}
|
66
|
+
|
67
|
+
# Public: Submodule to be included in your own classes to use `initialise`
|
68
|
+
#
|
69
|
+
# As far as `initialize` called automatically by a `new` method there is no
|
70
|
+
# sense to use it for third party classes.
|
71
|
+
#
|
72
|
+
module Initialisable
|
73
|
+
# Public: On module being included do:
|
74
|
+
# 1. Check if it's a global include
|
75
|
+
# 2. Add and alias of the parent's `initialize` (for `super` calls)
|
76
|
+
# 3. Create your own initialize method (to be auto-called by the `new`)
|
77
|
+
#
|
78
|
+
# Returns nothing.
|
79
|
+
def self.included(host_class)
|
80
|
+
unless host_class == Object
|
81
|
+
# alias parent's initialise method
|
82
|
+
host_class.superclass.class_eval do
|
83
|
+
alias_method :initialise, :initialize
|
84
|
+
end
|
85
|
+
|
86
|
+
# create own initialize method
|
87
|
+
define_method :initialize do |*args|
|
88
|
+
initialise(*args)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Public: British alias of native is_a? method
|
95
|
+
# Returns the original method's result
|
96
|
+
def is_an?(*args)
|
97
|
+
is_a?(*args) if self.respond_to?(:is_a?)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Public: additional alias for is_an?/is_a? method
|
101
|
+
alias_method :an?, :is_an?
|
102
|
+
|
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
|
127
|
+
|
128
|
+
ENDINGS.each_pair do |bre_e, ame_e|
|
129
|
+
next unless name.include?(bre_e)
|
130
|
+
|
131
|
+
british_ending_pattern = /#{bre_e}(?=_|-|$|\?|\!|=)/
|
132
|
+
americanised_name = name.gsub(british_ending_pattern, ame_e)
|
133
|
+
|
134
|
+
return send(americanised_name, *args) if respond_to?(americanised_name)
|
135
|
+
end
|
136
|
+
|
137
|
+
fail NoMethodError, "undefined method `#{name}' for #{self}"
|
138
|
+
end
|
139
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: british
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Serge Bedzhyk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
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.'
|
16
|
+
email: smileart21@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/british.rb
|
22
|
+
homepage: https://github.com/smileart/british
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.4.5.1
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: A tiny module that is supposed to help Brits to use Ruby with more comfort.
|
46
|
+
test_files: []
|
47
|
+
has_rdoc:
|