functionable 0.0.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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +3 -0
- data/LICENSE.adoc +134 -0
- data/README.adoc +460 -0
- data/functionable.gemspec +29 -0
- data/lib/functionable.rb +77 -0
- data.tar.gz.sig +0 -0
- metadata +78 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1d0373eae031682977e0b0b675e1a448c760f5028291e8859c2ad867183a9da3
|
|
4
|
+
data.tar.gz: ebb2e10a2989723277ea225cfe87b912471d0d8321af005ddb60a687acbec669
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 89a3de952523e9236074581784c317623f64e27c24c2888b60a815f704cf1b518b2b16e7e062481843d7285bc34d4a387170bc7cd674c03caa957cf79737ff4b
|
|
7
|
+
data.tar.gz: be97ad64ee0a5eedba64fb5062dec6b619e164e2a0a01460c0050263effba8c110ec4ddc5152847d8fab6ab549bb6c2e0ed2b372524577ea90a158cd06f0d95f
|
checksums.yaml.gz.sig
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
Fc!��S����J�����h�&�x1�bSm�*�0�ڽI��w�i3_�c%�]'TmJ4�F�h��&�wc�����Q����$�L]LO�`�q=��?>ԉxWbI��^� 6=X !F�}o��A��|!�[L��rW�Z�=G\��D�t���*;������W�7^�3�K��(���0�����JnD�x9�d9�g{`yV�9_q��Ge�]�z᧪��!� ʵY�+r��|$���D�j��?��B�VJ���5r�H���c{�>3�����j�_��������S<M:�g)+t���W[�,��פ6���
|
|
2
|
+
E��,O��U�5�����H�:�Ia
|
|
3
|
+
�W���a�v��2%���a�E|?*uVˉ��߃7{�Dv����;�b�
|
data/LICENSE.adoc
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
= Hippocratic License
|
|
2
|
+
|
|
3
|
+
Version: 2.1.0.
|
|
4
|
+
|
|
5
|
+
Purpose. The purpose of this License is for the Licensor named above to
|
|
6
|
+
permit the Licensee (as defined below) broad permission, if consistent
|
|
7
|
+
with Human Rights Laws and Human Rights Principles (as each is defined
|
|
8
|
+
below), to use and work with the Software (as defined below) within the
|
|
9
|
+
full scope of Licensor’s copyright and patent rights, if any, in the
|
|
10
|
+
Software, while ensuring attribution and protecting the Licensor from
|
|
11
|
+
liability.
|
|
12
|
+
|
|
13
|
+
Permission and Conditions. The Licensor grants permission by this
|
|
14
|
+
license ("License"), free of charge, to the extent of Licensor’s
|
|
15
|
+
rights under applicable copyright and patent law, to any person or
|
|
16
|
+
entity (the "Licensee") obtaining a copy of this software and
|
|
17
|
+
associated documentation files (the "Software"), to do everything with
|
|
18
|
+
the Software that would otherwise infringe (i) the Licensor’s copyright
|
|
19
|
+
in the Software or (ii) any patent claims to the Software that the
|
|
20
|
+
Licensor can license or becomes able to license, subject to all of the
|
|
21
|
+
following terms and conditions:
|
|
22
|
+
|
|
23
|
+
* Acceptance. This License is automatically offered to every person and
|
|
24
|
+
entity subject to its terms and conditions. Licensee accepts this
|
|
25
|
+
License and agrees to its terms and conditions by taking any action with
|
|
26
|
+
the Software that, absent this License, would infringe any intellectual
|
|
27
|
+
property right held by Licensor.
|
|
28
|
+
* Notice. Licensee must ensure that everyone who gets a copy of any part
|
|
29
|
+
of this Software from Licensee, with or without changes, also receives
|
|
30
|
+
the License and the above copyright notice (and if included by the
|
|
31
|
+
Licensor, patent, trademark and attribution notice). Licensee must cause
|
|
32
|
+
any modified versions of the Software to carry prominent notices stating
|
|
33
|
+
that Licensee changed the Software. For clarity, although Licensee is
|
|
34
|
+
free to create modifications of the Software and distribute only the
|
|
35
|
+
modified portion created by Licensee with additional or different terms,
|
|
36
|
+
the portion of the Software not modified must be distributed pursuant to
|
|
37
|
+
this License. If anyone notifies Licensee in writing that Licensee has
|
|
38
|
+
not complied with this Notice section, Licensee can keep this License by
|
|
39
|
+
taking all practical steps to comply within 30 days after the notice. If
|
|
40
|
+
Licensee does not do so, Licensee’s License (and all rights licensed
|
|
41
|
+
hereunder) shall end immediately.
|
|
42
|
+
* Compliance with Human Rights Principles and Human Rights Laws.
|
|
43
|
+
[arabic]
|
|
44
|
+
. Human Rights Principles.
|
|
45
|
+
[loweralpha]
|
|
46
|
+
.. Licensee is advised to consult the articles of the United Nations
|
|
47
|
+
Universal Declaration of Human Rights and the United Nations Global
|
|
48
|
+
Compact that define recognized principles of international human rights
|
|
49
|
+
(the "Human Rights Principles"). Licensee shall use the Software in a
|
|
50
|
+
manner consistent with Human Rights Principles.
|
|
51
|
+
.. Unless the Licensor and Licensee agree otherwise, any dispute,
|
|
52
|
+
controversy, or claim arising out of or relating to (i) Section 1(a)
|
|
53
|
+
regarding Human Rights Principles, including the breach of Section 1(a),
|
|
54
|
+
termination of this License for breach of the Human Rights Principles,
|
|
55
|
+
or invalidity of Section 1(a) or (ii) a determination of whether any Law
|
|
56
|
+
is consistent or in conflict with Human Rights Principles pursuant to
|
|
57
|
+
Section 2, below, shall be settled by arbitration in accordance with the
|
|
58
|
+
Hague Rules on Business and Human Rights Arbitration (the "Rules");
|
|
59
|
+
provided, however, that Licensee may elect not to participate in such
|
|
60
|
+
arbitration, in which event this License (and all rights licensed
|
|
61
|
+
hereunder) shall end immediately. The number of arbitrators shall be one
|
|
62
|
+
unless the Rules require otherwise.
|
|
63
|
+
+
|
|
64
|
+
Unless both the Licensor and Licensee agree to the contrary: (1) All
|
|
65
|
+
documents and information concerning the arbitration shall be public and
|
|
66
|
+
may be disclosed by any party; (2) The repository referred to under
|
|
67
|
+
Article 43 of the Rules shall make available to the public in a timely
|
|
68
|
+
manner all documents concerning the arbitration which are communicated
|
|
69
|
+
to it, including all submissions of the parties, all evidence admitted
|
|
70
|
+
into the record of the proceedings, all transcripts or other recordings
|
|
71
|
+
of hearings and all orders, decisions and awards of the arbitral
|
|
72
|
+
tribunal, subject only to the arbitral tribunal’s powers to take such
|
|
73
|
+
measures as may be necessary to safeguard the integrity of the arbitral
|
|
74
|
+
process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3)
|
|
75
|
+
Article 26(6) of the Rules shall not apply.
|
|
76
|
+
. Human Rights Laws. The Software shall not be used by any person or
|
|
77
|
+
entity for any systems, activities, or other uses that violate any Human
|
|
78
|
+
Rights Laws. "Human Rights Laws" means any applicable laws,
|
|
79
|
+
regulations, or rules (collectively, "Laws") that protect human,
|
|
80
|
+
civil, labor, privacy, political, environmental, security, economic, due
|
|
81
|
+
process, or similar rights; provided, however, that such Laws are
|
|
82
|
+
consistent and not in conflict with Human Rights Principles (a dispute
|
|
83
|
+
over the consistency or a conflict between Laws and Human Rights
|
|
84
|
+
Principles shall be determined by arbitration as stated above). Where
|
|
85
|
+
the Human Rights Laws of more than one jurisdiction are applicable or in
|
|
86
|
+
conflict with respect to the use of the Software, the Human Rights Laws
|
|
87
|
+
that are most protective of the individuals or groups harmed shall
|
|
88
|
+
apply.
|
|
89
|
+
. Indemnity. Licensee shall hold harmless and indemnify Licensor (and
|
|
90
|
+
any other contributor) against all losses, damages, liabilities,
|
|
91
|
+
deficiencies, claims, actions, judgments, settlements, interest, awards,
|
|
92
|
+
penalties, fines, costs, or expenses of whatever kind, including
|
|
93
|
+
Licensor’s reasonable attorneys’ fees, arising out of or relating to
|
|
94
|
+
Licensee’s use of the Software in violation of Human Rights Laws or
|
|
95
|
+
Human Rights Principles.
|
|
96
|
+
* Failure to Comply. Any failure of Licensee to act according to the
|
|
97
|
+
terms and conditions of this License is both a breach of the License and
|
|
98
|
+
an infringement of the intellectual property rights of the Licensor
|
|
99
|
+
(subject to exceptions under Laws, e.g., fair use). In the event of a
|
|
100
|
+
breach or infringement, the terms and conditions of this License may be
|
|
101
|
+
enforced by Licensor under the Laws of any jurisdiction to which
|
|
102
|
+
Licensee is subject. Licensee also agrees that the Licensor may enforce
|
|
103
|
+
the terms and conditions of this License against Licensee through
|
|
104
|
+
specific performance (or similar remedy under Laws) to the extent
|
|
105
|
+
permitted by Laws. For clarity, except in the event of a breach of this
|
|
106
|
+
License, infringement, or as otherwise stated in this License, Licensor
|
|
107
|
+
may not terminate this License with Licensee.
|
|
108
|
+
* Enforceability and Interpretation. If any term or provision of this
|
|
109
|
+
License is determined to be invalid, illegal, or unenforceable by a
|
|
110
|
+
court of competent jurisdiction, then such invalidity, illegality, or
|
|
111
|
+
unenforceability shall not affect any other term or provision of this
|
|
112
|
+
License or invalidate or render unenforceable such term or provision in
|
|
113
|
+
any other jurisdiction; provided, however, subject to a court
|
|
114
|
+
modification pursuant to the immediately following sentence, if any term
|
|
115
|
+
or provision of this License pertaining to Human Rights Laws or Human
|
|
116
|
+
Rights Principles is deemed invalid, illegal, or unenforceable against
|
|
117
|
+
Licensee by a court of competent jurisdiction, all rights in the
|
|
118
|
+
Software granted to Licensee shall be deemed null and void as between
|
|
119
|
+
Licensor and Licensee. Upon a determination that any term or provision
|
|
120
|
+
is invalid, illegal, or unenforceable, to the extent permitted by Laws,
|
|
121
|
+
the court may modify this License to affect the original purpose that
|
|
122
|
+
the Software be used in compliance with Human Rights Principles and
|
|
123
|
+
Human Rights Laws as closely as possible. The language in this License
|
|
124
|
+
shall be interpreted as to its fair meaning and not strictly for or
|
|
125
|
+
against any party.
|
|
126
|
+
* Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES
|
|
127
|
+
"AS IS," WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND
|
|
128
|
+
ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR
|
|
129
|
+
OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
|
|
130
|
+
OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
|
|
131
|
+
|
|
132
|
+
This Hippocratic License is an link:https://ethicalsource.dev[Ethical Source license] and is offered
|
|
133
|
+
for use by licensors and licensees at their own risk, on an "AS IS" basis, and with no warranties
|
|
134
|
+
express or implied, to the maximum extent permitted by Laws.
|
data/README.adoc
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
:toc: macro
|
|
2
|
+
:toclevels: 5
|
|
3
|
+
:figure-caption!:
|
|
4
|
+
|
|
5
|
+
:module_function_link: link:https://docs.ruby-lang.org/en/master/Module.html#method-i-module_function[module_function]
|
|
6
|
+
:modules_link: link:https://alchemists.io/articles/ruby_modules[Ruby Modules]
|
|
7
|
+
|
|
8
|
+
= Functionable
|
|
9
|
+
|
|
10
|
+
Enhances modules to be functional by disabling the ability to extend, include, or prepend while also ensuring all methods are class methods. This allows you to use a module as a functional collection of related methods for reuse throughout your application via _composition_ instead of _inheritance_. In other words, this is an enhanced version of {module_function_link} by preventing inheritance and reducing the memory overhead due to making copies of the original methods.
|
|
11
|
+
|
|
12
|
+
toc::[]
|
|
13
|
+
|
|
14
|
+
== Features
|
|
15
|
+
|
|
16
|
+
* Allows modules to be a collection of functional methods.
|
|
17
|
+
* Allows your modules and/or methods to be composable instead of inherited.
|
|
18
|
+
* Provides faster performance and a reduced memory than any object within your application. See the xref:_benchmarks[Benchmarks] section for details.
|
|
19
|
+
|
|
20
|
+
== Requirements
|
|
21
|
+
|
|
22
|
+
. link:https://www.ruby-lang.org[Ruby].
|
|
23
|
+
. A solid understanding {modules_link}.
|
|
24
|
+
|
|
25
|
+
== Setup
|
|
26
|
+
|
|
27
|
+
To install _with_ security, run:
|
|
28
|
+
|
|
29
|
+
[source,bash]
|
|
30
|
+
----
|
|
31
|
+
# 💡 Skip this line if you already have the public certificate installed.
|
|
32
|
+
gem cert --add <(curl --compressed --location https://alchemists.io/gems.pem)
|
|
33
|
+
gem install functionable --trust-policy HighSecurity
|
|
34
|
+
----
|
|
35
|
+
|
|
36
|
+
To install _without_ security, run:
|
|
37
|
+
|
|
38
|
+
[source,bash]
|
|
39
|
+
----
|
|
40
|
+
gem install functionable
|
|
41
|
+
----
|
|
42
|
+
|
|
43
|
+
You can also add the gem directly to your project:
|
|
44
|
+
|
|
45
|
+
[source,bash]
|
|
46
|
+
----
|
|
47
|
+
bundle add functionable
|
|
48
|
+
----
|
|
49
|
+
|
|
50
|
+
Once the gem is installed, you only need to require it:
|
|
51
|
+
|
|
52
|
+
[source,ruby]
|
|
53
|
+
----
|
|
54
|
+
require "functionable"
|
|
55
|
+
----
|
|
56
|
+
|
|
57
|
+
== Usage
|
|
58
|
+
|
|
59
|
+
Enhancing your module to be functional is as simple as extending your module:
|
|
60
|
+
|
|
61
|
+
[source,ruby]
|
|
62
|
+
----
|
|
63
|
+
module Math
|
|
64
|
+
extend Functionable
|
|
65
|
+
|
|
66
|
+
def add(first, second) = first + second
|
|
67
|
+
|
|
68
|
+
def subtract(first, second) = first - second
|
|
69
|
+
|
|
70
|
+
def multiply(first, second) = first * second
|
|
71
|
+
|
|
72
|
+
def divide(first, second) = first / second
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
Math.add 6, 3 # 9
|
|
76
|
+
Math.subtract 6, 3 # 3
|
|
77
|
+
Math.multiply 6, 3 # 18
|
|
78
|
+
Math.divide 6, 3 # 2
|
|
79
|
+
----
|
|
80
|
+
|
|
81
|
+
That's it! Now you can add related methods, as desired, which are specific to your namespace.
|
|
82
|
+
|
|
83
|
+
=== Conceal
|
|
84
|
+
|
|
85
|
+
Should you need to make any of your methods private, use the `conceal` method as follows:
|
|
86
|
+
|
|
87
|
+
[source,ruby]
|
|
88
|
+
----
|
|
89
|
+
module Demo
|
|
90
|
+
extend Functionable
|
|
91
|
+
|
|
92
|
+
def welcome(*) = print(*)
|
|
93
|
+
|
|
94
|
+
def print(message = "The default message.") = puts message
|
|
95
|
+
|
|
96
|
+
conceal :print
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
Demo.welcome # "The default message."
|
|
100
|
+
Demo.print # private method 'print' called for module Demo (NoMethodError)
|
|
101
|
+
----
|
|
102
|
+
|
|
103
|
+
The `conceal` method takes the same parameters as the link:https://docs.ruby-lang.org/en/master/Module.html#method-i-private_class_method[private_class_method] which means you can use a string, symbol, a single argument, multiple arguments, or an array.
|
|
104
|
+
|
|
105
|
+
=== Avoidances
|
|
106
|
+
|
|
107
|
+
Functional modules are only meant to be a collection of related methods which allows you to namespace your behavior, compose multiple methods together, and use composition to inject your module as a dependency within objects that need this functionality.
|
|
108
|
+
|
|
109
|
+
This means the following behavior is disabled (each uses an anonymous module and/or class for demonstration purposes and reduced syntax).
|
|
110
|
+
|
|
111
|
+
==== Extend
|
|
112
|
+
|
|
113
|
+
Use composition instead of inheritance:
|
|
114
|
+
|
|
115
|
+
[source,ruby]
|
|
116
|
+
----
|
|
117
|
+
functions = Module.new.extend Functionable
|
|
118
|
+
Class.new.extend functions
|
|
119
|
+
|
|
120
|
+
# Module extend is disabled. (NoMethodError)
|
|
121
|
+
----
|
|
122
|
+
|
|
123
|
+
==== Include
|
|
124
|
+
|
|
125
|
+
Use composition instead of inheritance:
|
|
126
|
+
|
|
127
|
+
[source,ruby]
|
|
128
|
+
----
|
|
129
|
+
functions = Module.new.extend Functionable
|
|
130
|
+
Class.new.include functions
|
|
131
|
+
|
|
132
|
+
# Module include is disabled. (NoMethodError)
|
|
133
|
+
----
|
|
134
|
+
|
|
135
|
+
You also can't include `Funtionable`, only extend:
|
|
136
|
+
|
|
137
|
+
[source,ruby]
|
|
138
|
+
----
|
|
139
|
+
Module.new.include Functionable
|
|
140
|
+
# Module include is disabled, use extend instead. (NoMethodError)
|
|
141
|
+
----
|
|
142
|
+
|
|
143
|
+
==== Prepend
|
|
144
|
+
|
|
145
|
+
Use composition instead of inheritance:
|
|
146
|
+
|
|
147
|
+
[source,ruby]
|
|
148
|
+
----
|
|
149
|
+
functions = Module.new.extend Functionable
|
|
150
|
+
Class.new.prepend functions
|
|
151
|
+
|
|
152
|
+
# Module prepend is disabled. (NoMethodError)
|
|
153
|
+
----
|
|
154
|
+
|
|
155
|
+
You also can't prepend, only extend:
|
|
156
|
+
|
|
157
|
+
[source,ruby]
|
|
158
|
+
----
|
|
159
|
+
Module.new.prepend Functionable
|
|
160
|
+
# Module prepend is disabled, use extend instead. (NoMethodError)
|
|
161
|
+
----
|
|
162
|
+
|
|
163
|
+
==== Module Function
|
|
164
|
+
|
|
165
|
+
The following is not allowed because you have this behavior when extending `Functionable`:
|
|
166
|
+
|
|
167
|
+
[source,ruby]
|
|
168
|
+
----
|
|
169
|
+
Module.new do
|
|
170
|
+
extend Functionable
|
|
171
|
+
|
|
172
|
+
module_function
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Module function behavior is disabled. (NoMethodError)
|
|
176
|
+
----
|
|
177
|
+
|
|
178
|
+
==== Public
|
|
179
|
+
|
|
180
|
+
Avoid the following since all methods are public by default:
|
|
181
|
+
|
|
182
|
+
[source,ruby]
|
|
183
|
+
----
|
|
184
|
+
Module.new do
|
|
185
|
+
extend Functionable
|
|
186
|
+
|
|
187
|
+
public
|
|
188
|
+
|
|
189
|
+
def demo = :demo
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Public visibility is disabled. (NoMethodError)
|
|
193
|
+
----
|
|
194
|
+
|
|
195
|
+
==== Protected
|
|
196
|
+
|
|
197
|
+
Avoid the following since a functionable module isn't mean to be inherited:
|
|
198
|
+
|
|
199
|
+
[source,ruby]
|
|
200
|
+
----
|
|
201
|
+
Module.new do
|
|
202
|
+
extend Functionable
|
|
203
|
+
|
|
204
|
+
protected
|
|
205
|
+
|
|
206
|
+
def demo = :demo
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Protected visibility is disabled. (NoMethodError)
|
|
210
|
+
----
|
|
211
|
+
|
|
212
|
+
==== Private
|
|
213
|
+
|
|
214
|
+
Avoid the following by using xref:_conceal[conceal] instead:
|
|
215
|
+
|
|
216
|
+
[source,ruby]
|
|
217
|
+
----
|
|
218
|
+
Module.new do
|
|
219
|
+
extend Functionable
|
|
220
|
+
|
|
221
|
+
private
|
|
222
|
+
|
|
223
|
+
def demo = :demo
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Private visibility is disabled, use conceal instead. (NoMethodError)
|
|
227
|
+
----
|
|
228
|
+
|
|
229
|
+
==== Aliasing
|
|
230
|
+
|
|
231
|
+
Avoid aliasing as you are not meant to inherit methods within a functional module:
|
|
232
|
+
|
|
233
|
+
[source,ruby]
|
|
234
|
+
----
|
|
235
|
+
Module.new do
|
|
236
|
+
extend Functionable
|
|
237
|
+
|
|
238
|
+
def demo = :demo
|
|
239
|
+
alias_method :demo, :alt
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# Aliasing :alt as :demo is disabled. (NoMethodError)
|
|
243
|
+
----
|
|
244
|
+
|
|
245
|
+
==== Class Variables
|
|
246
|
+
|
|
247
|
+
Avoid using class variables since they are a code smell and introduce unwanted state:
|
|
248
|
+
|
|
249
|
+
[source,ruby]
|
|
250
|
+
----
|
|
251
|
+
demo = Module.new do
|
|
252
|
+
extend Functionable
|
|
253
|
+
|
|
254
|
+
def get = class_variable_get :@@bogus
|
|
255
|
+
|
|
256
|
+
def set = class_variable_set :@@bogus, :bogus
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
demo.get # Getting class variable :@@bogus is disabled. (NoMethodError)
|
|
260
|
+
demo.set # Setting class variable :@@bogus is disabled. (NoMethodError)
|
|
261
|
+
----
|
|
262
|
+
|
|
263
|
+
==== Class Methods
|
|
264
|
+
|
|
265
|
+
Avoid class methods, use instance methods instead:
|
|
266
|
+
|
|
267
|
+
[source,ruby]
|
|
268
|
+
----
|
|
269
|
+
Module.new do
|
|
270
|
+
extend Functionable
|
|
271
|
+
|
|
272
|
+
def self.bogus = :bogus
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Avoid defining :bogus as a class method because the method will be automatically converted to a class method for you. (NoMethodError)
|
|
276
|
+
----
|
|
277
|
+
|
|
278
|
+
==== Constants
|
|
279
|
+
|
|
280
|
+
Avoid dynamically setting constants since you can add constants directly to the top of the module:
|
|
281
|
+
|
|
282
|
+
[source,ruby]
|
|
283
|
+
----
|
|
284
|
+
demo = Module.new do
|
|
285
|
+
extend Functionable
|
|
286
|
+
|
|
287
|
+
def bogus = const_set :BOGUS, :bogus
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
demo.bogus # Setting constant :BOGUS is disabled. (NoMethodError)
|
|
291
|
+
----
|
|
292
|
+
|
|
293
|
+
==== Define Method
|
|
294
|
+
|
|
295
|
+
Avoid dynamically defining a method since you can explicitly define your method instead:
|
|
296
|
+
|
|
297
|
+
[source,ruby]
|
|
298
|
+
----
|
|
299
|
+
Module.new do
|
|
300
|
+
extend Functionable
|
|
301
|
+
|
|
302
|
+
define_method :bogus, :bogus
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Defining method :bogus is disabled. (NoMethodError)
|
|
306
|
+
----
|
|
307
|
+
|
|
308
|
+
==== Remove Method
|
|
309
|
+
|
|
310
|
+
Avoid dynamically removing a method since you can explicitly delete the method instead.
|
|
311
|
+
|
|
312
|
+
[source,ruby]
|
|
313
|
+
----
|
|
314
|
+
Module.new do
|
|
315
|
+
extend Functionable
|
|
316
|
+
|
|
317
|
+
remove_method :bogus
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# Removing method :bogus is disabled. (NoMethodError)
|
|
321
|
+
----
|
|
322
|
+
|
|
323
|
+
==== Undef Method
|
|
324
|
+
|
|
325
|
+
Avoid dynamically undefining a method since you can explicitly delete the method instead.
|
|
326
|
+
|
|
327
|
+
[source,ruby]
|
|
328
|
+
----
|
|
329
|
+
Module.new do
|
|
330
|
+
extend Functionable
|
|
331
|
+
|
|
332
|
+
undef_method :bogus
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# Undefining method :bogus is disabled. (NoMethodError)
|
|
336
|
+
----
|
|
337
|
+
|
|
338
|
+
== Benchmarks
|
|
339
|
+
|
|
340
|
+
When you lean into the power of functional programming in Ruby, you gain performance and lower your memory footprint since you are creating the minimal amount of objects necessary. In terms of CPU performance, here's a comparison:
|
|
341
|
+
|
|
342
|
+
[source,ruby]
|
|
343
|
+
----
|
|
344
|
+
require "bundler/inline"
|
|
345
|
+
|
|
346
|
+
gemfile true do
|
|
347
|
+
source "https://rubygems.org"
|
|
348
|
+
|
|
349
|
+
gem "benchmark-ips"
|
|
350
|
+
gem "functionable"
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
ProcExample = proc { |message = "benchmark"| message }
|
|
354
|
+
LambdaExample = -> message = "benchmark" { message }
|
|
355
|
+
|
|
356
|
+
module ModuleExample
|
|
357
|
+
extend Functionable
|
|
358
|
+
|
|
359
|
+
def call(message = "benchmark") = message
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
class ClassExample
|
|
363
|
+
def initialize message = "benchmark"
|
|
364
|
+
@message = message
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def call = message
|
|
368
|
+
|
|
369
|
+
private
|
|
370
|
+
|
|
371
|
+
attr_reader :message
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
memoized_function = ClassExample.new
|
|
375
|
+
|
|
376
|
+
Benchmark.ips do |benchmark|
|
|
377
|
+
benchmark.config time: 5, warmup: 2
|
|
378
|
+
|
|
379
|
+
benchmark.report("Proc") { ProcExample.call }
|
|
380
|
+
benchmark.report("Lambda") { LambdaExample.call }
|
|
381
|
+
benchmark.report("Module") { ModuleExample.call }
|
|
382
|
+
benchmark.report("Class (new)") { ClassExample.new.call }
|
|
383
|
+
benchmark.report("Class (memoized)") { memoized_function.call }
|
|
384
|
+
|
|
385
|
+
benchmark.compare!
|
|
386
|
+
end
|
|
387
|
+
----
|
|
388
|
+
|
|
389
|
+
When you run the above benchmark, you should see the following results:
|
|
390
|
+
|
|
391
|
+
----
|
|
392
|
+
ruby 3.4.7 (2025-10-08 revision 7a5688e2a2) +YJIT +PRISM [arm64-darwin24.6.0]
|
|
393
|
+
Warming up --------------------------------------
|
|
394
|
+
Proc 2.370M i/100ms
|
|
395
|
+
Lambda 2.335M i/100ms
|
|
396
|
+
Module 4.153M i/100ms
|
|
397
|
+
Class (new) 1.394M i/100ms
|
|
398
|
+
Class (memoized) 4.341M i/100ms
|
|
399
|
+
Calculating -------------------------------------
|
|
400
|
+
Proc 25.039M (± 0.5%) i/s (39.94 ns/i) - 125.624M in 5.017274s
|
|
401
|
+
Lambda 25.657M (± 0.3%) i/s (38.98 ns/i) - 128.450M in 5.006465s
|
|
402
|
+
Module 59.812M (± 2.0%) i/s (16.72 ns/i) - 303.169M in 5.070730s
|
|
403
|
+
Class (new) 15.891M (± 1.3%) i/s (62.93 ns/i) - 80.870M in 5.089769s
|
|
404
|
+
Class (memoized) 58.271M (± 1.5%) i/s (17.16 ns/i) - 295.161M in 5.066395s
|
|
405
|
+
|
|
406
|
+
Comparison:
|
|
407
|
+
Module: 59812197.4 i/s
|
|
408
|
+
Class (memoized): 58271006.1 i/s - same-ish: difference falls within error
|
|
409
|
+
Lambda: 25657201.2 i/s - 2.33x slower
|
|
410
|
+
Proc: 25039056.2 i/s - 2.39x slower
|
|
411
|
+
Class (new): 15891475.5 i/s - 3.76x slower
|
|
412
|
+
----
|
|
413
|
+
|
|
414
|
+
As you can see, a functional module is the fastest while a memoized class comes in at a very close second (despite creating an additional object). Everything else is much slower.
|
|
415
|
+
|
|
416
|
+
== Development
|
|
417
|
+
|
|
418
|
+
To contribute, run:
|
|
419
|
+
|
|
420
|
+
[source,bash]
|
|
421
|
+
----
|
|
422
|
+
git clone https://github.com/bkuhlmann/functionable
|
|
423
|
+
cd functionable
|
|
424
|
+
bin/setup
|
|
425
|
+
----
|
|
426
|
+
|
|
427
|
+
You can also use the IRB console for direct access to all objects:
|
|
428
|
+
|
|
429
|
+
[source,bash]
|
|
430
|
+
----
|
|
431
|
+
bin/console
|
|
432
|
+
----
|
|
433
|
+
|
|
434
|
+
== Tests
|
|
435
|
+
|
|
436
|
+
To test, run:
|
|
437
|
+
|
|
438
|
+
[source,bash]
|
|
439
|
+
----
|
|
440
|
+
bin/rake
|
|
441
|
+
----
|
|
442
|
+
|
|
443
|
+
== link:https://alchemists.io/policies/license[License]
|
|
444
|
+
|
|
445
|
+
== link:https://alchemists.io/policies/security[Security]
|
|
446
|
+
|
|
447
|
+
== link:https://alchemists.io/policies/code_of_conduct[Code of Conduct]
|
|
448
|
+
|
|
449
|
+
== link:https://alchemists.io/policies/contributions[Contributions]
|
|
450
|
+
|
|
451
|
+
== link:https://alchemists.io/policies/developer_certificate_of_origin[Developer Certificate of Origin]
|
|
452
|
+
|
|
453
|
+
== link:https://alchemists.io/projects/functionable/versions[Versions]
|
|
454
|
+
|
|
455
|
+
== link:https://alchemists.io/community[Community]
|
|
456
|
+
|
|
457
|
+
== Credits
|
|
458
|
+
|
|
459
|
+
* Built with link:https://alchemists.io/projects/gemsmith[Gemsmith].
|
|
460
|
+
* Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "functionable"
|
|
5
|
+
spec.version = "0.0.0"
|
|
6
|
+
spec.authors = ["Brooke Kuhlmann"]
|
|
7
|
+
spec.email = ["brooke@alchemists.io"]
|
|
8
|
+
spec.homepage = "https://alchemists.io/projects/functionable"
|
|
9
|
+
spec.summary = "Enhances modules to be functional by default."
|
|
10
|
+
spec.license = "Hippocratic-2.1"
|
|
11
|
+
|
|
12
|
+
spec.metadata = {
|
|
13
|
+
"bug_tracker_uri" => "https://github.com/bkuhlmann/functionable/issues",
|
|
14
|
+
"changelog_uri" => "https://alchemists.io/projects/functionable/versions",
|
|
15
|
+
"homepage_uri" => "https://alchemists.io/projects/functionable",
|
|
16
|
+
"funding_uri" => "https://github.com/sponsors/bkuhlmann",
|
|
17
|
+
"label" => "Functionable",
|
|
18
|
+
"rubygems_mfa_required" => "true",
|
|
19
|
+
"source_code_uri" => "https://github.com/bkuhlmann/functionable"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
spec.signing_key = Gem.default_key_path
|
|
23
|
+
spec.cert_chain = [Gem.default_cert_path]
|
|
24
|
+
|
|
25
|
+
spec.required_ruby_version = "~> 3.4"
|
|
26
|
+
|
|
27
|
+
spec.extra_rdoc_files = Dir["README*", "LICENSE*"]
|
|
28
|
+
spec.files = Dir["*.gemspec", "lib/**/*"]
|
|
29
|
+
end
|
data/lib/functionable.rb
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Main namespace.
|
|
4
|
+
module Functionable
|
|
5
|
+
@mutex = Mutex.new
|
|
6
|
+
|
|
7
|
+
def self.extended descendant
|
|
8
|
+
descendant.singleton_class.class_eval do
|
|
9
|
+
def extended(*) = fail NoMethodError, "Module extend is disabled."
|
|
10
|
+
|
|
11
|
+
def included(*) = fail NoMethodError, "Module include is disabled."
|
|
12
|
+
|
|
13
|
+
def prepended(*) = fail NoMethodError, "Module prepend is disabled."
|
|
14
|
+
|
|
15
|
+
def module_function(*) = fail NoMethodError, "Module function behavior is disabled."
|
|
16
|
+
|
|
17
|
+
def public(*) = fail NoMethodError, "Public visibility is disabled."
|
|
18
|
+
|
|
19
|
+
def protected(*) = fail NoMethodError, "Protected visibility is disabled."
|
|
20
|
+
|
|
21
|
+
def private(*) = fail NoMethodError, "Private visibility is disabled, use conceal instead."
|
|
22
|
+
|
|
23
|
+
def conceal(*) = private_class_method(*)
|
|
24
|
+
|
|
25
|
+
def alias_method to, from
|
|
26
|
+
fail NoMethodError, "Aliasing #{from.inspect} as #{to.inspect} is disabled."
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def class_variable_set(name, ...)
|
|
30
|
+
fail NoMethodError, "Setting class variable #{name.inspect} is disabled."
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def class_variable_get name
|
|
34
|
+
fail NoMethodError, "Getting class variable #{name.inspect} is disabled."
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def const_set(name, ...) = fail NoMethodError, "Setting constant #{name.inspect} is disabled."
|
|
38
|
+
|
|
39
|
+
def define_method(name, ...)
|
|
40
|
+
fail NoMethodError, "Defining method #{name.inspect} is disabled."
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def remove_method name
|
|
44
|
+
return super if instance_variable_get :@functionable
|
|
45
|
+
|
|
46
|
+
fail NoMethodError, "Removing method #{name.inspect} is disabled."
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def undef_method(name) = fail NoMethodError, "Undefining method #{name.inspect} is disabled."
|
|
50
|
+
|
|
51
|
+
def singleton_method_added name, allowed: %i[method_added singleton_method_added].freeze
|
|
52
|
+
return super(name) if allowed.include?(name) || instance_variable_get(:@functionable)
|
|
53
|
+
|
|
54
|
+
fail NoMethodError,
|
|
55
|
+
"Avoid defining #{name.inspect} as a class method because the method will be " \
|
|
56
|
+
"automatically converted to a class method for you."
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def method_added name
|
|
60
|
+
unbound = instance_method name
|
|
61
|
+
|
|
62
|
+
instance_variable_set :@functionable, true
|
|
63
|
+
remove_method name
|
|
64
|
+
define_singleton_method name, unbound
|
|
65
|
+
instance_variable_set :@functionable, false
|
|
66
|
+
|
|
67
|
+
super
|
|
68
|
+
ensure
|
|
69
|
+
instance_variable_set :@functionable, false
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.included(*) = fail NoMethodError, "Module include is disabled, use extend instead."
|
|
75
|
+
|
|
76
|
+
def self.prepended(*) = fail NoMethodError, "Module prepend is disabled, use extend instead."
|
|
77
|
+
end
|
data.tar.gz.sig
ADDED
|
Binary file
|
metadata
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: functionable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Brooke Kuhlmann
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain:
|
|
10
|
+
- |
|
|
11
|
+
-----BEGIN CERTIFICATE-----
|
|
12
|
+
MIIENjCCAp6gAwIBAgIBAjANBgkqhkiG9w0BAQsFADBBMQ8wDQYDVQQDDAZicm9v
|
|
13
|
+
a2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQBGRYC
|
|
14
|
+
aW8wHhcNMjUwMzIyMTQ1NDE3WhcNMjYwMzIyMTQ1NDE3WjBBMQ8wDQYDVQQDDAZi
|
|
15
|
+
cm9va2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQB
|
|
16
|
+
GRYCaW8wggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQCro8tj5/E1Hg88
|
|
17
|
+
f4qfiwPVd2zJQHvdYt4GHVvuHRRgx4HGhJuNp+4BId08RBn7V6V1MW6MY3kezRBs
|
|
18
|
+
M+7QOQ4b1xNLTvY7FYQB1wGK5a4x7TTokDrPYQxDB2jmsdDYCzVbIMrAvUfcecRi
|
|
19
|
+
khyGZCdByiiCl4fKv77P12tTT+NfsvXkLt/AYCGwjOUyGKTQ01Z6eC09T27GayPH
|
|
20
|
+
QQvIkakyFgcJtzSyGzs8bzK5q9u7wQ12MNTjJoXzW69lqp0oNvDylu81EiSUb5S6
|
|
21
|
+
QzzPxZBiRB1sgtbt1gUbVI262ZDq1gR+HxPFmp+Cgt7ZLIJZAtesQvtcMzseXpfn
|
|
22
|
+
hpmm0Sw22KGhRAy/mqHBRhDl5HqS1SJp2Ko3lcnpXeFResp0HNlt8NSu13vhC08j
|
|
23
|
+
GUHU9MyIXbFOsnp3K3ADrAVjPWop8EZkmUR3MV/CUm00w2cZHCSGiXl1KMpiVKvk
|
|
24
|
+
Ywr1gd2ZME4QLSo+EXUtLxDUa/W3xnBS8dBOuMMz02FPWYr3PN8CAwEAAaM5MDcw
|
|
25
|
+
CQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFAFgmv0tYMZnItuPycSM
|
|
26
|
+
F5wykJEVMA0GCSqGSIb3DQEBCwUAA4IBgQBlzRfyAYx/fCFjizS0Npxw4+4T3aYL
|
|
27
|
+
hbXoDqQRWjxuhFZcXUymhz3r8/Ltyri9lSof8grzB+8/+mrMVms7Gwt5qolk6zdn
|
|
28
|
+
FkySGy/jmpN12ldOHFbBEnyVBZNBvOBVb8zkkw8PhiHdBdXOUm4Jy39yJvBLfjcC
|
|
29
|
+
iM1aeWPmgPy1GbvZU+leRGZLt6dRIR9oCDXcWLRjha8xLMoz6Yn9fJBYexBA3iEz
|
|
30
|
+
h5S7pn4AX/JhVRiSyl8pAy4jEKydpyQrliH3gHkpNmUS/XDczP+9xX1bAB4BvqL2
|
|
31
|
+
NCxMcQ+hiJNqCKpPgHxaOOHZfIxV33logIuPEQ8NryHAwZ9ZWnwtYDE8kQGGKskI
|
|
32
|
+
Kkm6QT474hZl7MpwiJjWgW313CR7jUEekQahX1QxCxHPI7LSrKpno0plH3uWIOQp
|
|
33
|
+
KUlkb9uyACBgyRO52ZHiDVI8YvtU5O/j9pSes9/3XgvBeC1onx4qWp+uRX7eVsYS
|
|
34
|
+
GiijocTc3enZVrXERetaXj8/9XWs3fB3HWY=
|
|
35
|
+
-----END CERTIFICATE-----
|
|
36
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
37
|
+
dependencies: []
|
|
38
|
+
email:
|
|
39
|
+
- brooke@alchemists.io
|
|
40
|
+
executables: []
|
|
41
|
+
extensions: []
|
|
42
|
+
extra_rdoc_files:
|
|
43
|
+
- LICENSE.adoc
|
|
44
|
+
- README.adoc
|
|
45
|
+
files:
|
|
46
|
+
- LICENSE.adoc
|
|
47
|
+
- README.adoc
|
|
48
|
+
- functionable.gemspec
|
|
49
|
+
- lib/functionable.rb
|
|
50
|
+
homepage: https://alchemists.io/projects/functionable
|
|
51
|
+
licenses:
|
|
52
|
+
- Hippocratic-2.1
|
|
53
|
+
metadata:
|
|
54
|
+
bug_tracker_uri: https://github.com/bkuhlmann/functionable/issues
|
|
55
|
+
changelog_uri: https://alchemists.io/projects/functionable/versions
|
|
56
|
+
homepage_uri: https://alchemists.io/projects/functionable
|
|
57
|
+
funding_uri: https://github.com/sponsors/bkuhlmann
|
|
58
|
+
label: Functionable
|
|
59
|
+
rubygems_mfa_required: 'true'
|
|
60
|
+
source_code_uri: https://github.com/bkuhlmann/functionable
|
|
61
|
+
rdoc_options: []
|
|
62
|
+
require_paths:
|
|
63
|
+
- lib
|
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.4'
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '0'
|
|
74
|
+
requirements: []
|
|
75
|
+
rubygems_version: 3.7.2
|
|
76
|
+
specification_version: 4
|
|
77
|
+
summary: Enhances modules to be functional by default.
|
|
78
|
+
test_files: []
|
metadata.gz.sig
ADDED
|
Binary file
|