mumukit-core 1.13.0 → 1.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mumukit/core/module.rb +79 -4
- data/lib/mumukit/core/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc881d626353e8ffa9fbd7f6710158db924bfcffd4ce3f7bed535a744d20c5e6
|
4
|
+
data.tar.gz: effc6a4fcd8de536ecd6eb52716d0aec3d6fe43f137d0fcd46a4f4f9c7944414
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f2038b0dd25424fc31c1e051bd49655a8965a0d80111a25ca351595597d6f453530850314d31a681ba52774531f62264ddd39f5b0705a2c57885ae42202e067
|
7
|
+
data.tar.gz: 5aac0b02e4eafea271f906400e98c0ee6dfc364edfd2de0fe3670f85e0c35741bcd987969cd1efaef0f023126159b88dc6016dc9bd486ef492b8c7e626d4b007
|
data/lib/mumukit/core/module.rb
CHANGED
@@ -6,11 +6,86 @@ class Module
|
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
# Redefines a previous definition of the given method.
|
10
|
+
# It takes a block with the original arguments and the `hyper`
|
11
|
+
# reference to the original definition
|
12
|
+
def patch(selector, &block)
|
13
|
+
revamp selector do |_, this, *args, hyper|
|
14
|
+
this.instance_exec(*args, hyper, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# `revamp` is a `patch` generalization
|
19
|
+
# that accepts multiple selectors and
|
20
|
+
# takes a more general callback, like the following:
|
21
|
+
#
|
22
|
+
# ```
|
23
|
+
# revamp :foo, :bar do |selector, this, *args, hyper|
|
24
|
+
# puts "sending #{selector} to #{this}..."
|
25
|
+
# result = hyper.call(*args)
|
26
|
+
# puts "done. result is #{result}"
|
27
|
+
# result
|
28
|
+
# end
|
29
|
+
# ```
|
30
|
+
#
|
31
|
+
# `revamp` should be prefered to `patch` when more control or performance
|
32
|
+
# is required
|
33
|
+
def revamp(*selectors, selector_transformer: nil, &block)
|
34
|
+
selectors.each do |selector|
|
35
|
+
method_proc = instance_method selector
|
36
|
+
selector_transfom = selector_transformer ? selector_transformer.call(selector) : selector
|
37
|
+
|
38
|
+
define_method selector do |*args|
|
39
|
+
block.call(selector_transfom, self, *args, method_proc.bind(self))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Revamps an accessor. This method is similar to `revamp`,
|
45
|
+
# but:
|
46
|
+
#
|
47
|
+
# * assumes a 0 arguments array
|
48
|
+
# * takes the accessor's original result instead of the `hyper` reference
|
49
|
+
#
|
50
|
+
# As a consecuence, `revamp_accessor` can not alter the way and the moment
|
51
|
+
# the original method is evaluated.
|
52
|
+
#
|
53
|
+
# ```
|
54
|
+
# revamp_accessor :foo, :bar do |selector, this, result|
|
55
|
+
# puts "result of sending #{selector} to #{this} is #{result}"
|
56
|
+
# result
|
57
|
+
# end
|
58
|
+
# ```
|
59
|
+
#
|
60
|
+
# :warning: the block will not be called on a `nil` result
|
61
|
+
def revamp_accessor(*selectors, &block)
|
62
|
+
revamp(*selectors) do |selector, this, hyper|
|
63
|
+
result = hyper.call
|
64
|
+
result && block.call(selector, this, result)
|
65
|
+
end
|
66
|
+
end
|
11
67
|
|
12
|
-
|
13
|
-
|
68
|
+
# Caches an accessor, using the idiom `@__foo__ ||= foo`. For example, the following code:
|
69
|
+
#
|
70
|
+
# ```
|
71
|
+
# def foo
|
72
|
+
# @__foo__ ||= #...implementation...
|
73
|
+
# end
|
74
|
+
# ```
|
75
|
+
#
|
76
|
+
# Can be turned into:
|
77
|
+
#
|
78
|
+
# ```
|
79
|
+
# def foo
|
80
|
+
# #...implementation...
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# cache_accessor :foo
|
84
|
+
# ```
|
85
|
+
#
|
86
|
+
def cache_accessor(*selectors)
|
87
|
+
revamp(*selectors, selector_transformer: proc { |it| "@__#{it}__".to_sym }) do |attr_name, this, hyper|
|
88
|
+
this.instance_variable_get(attr_name) || this.instance_variable_set(attr_name, hyper.call)
|
14
89
|
end
|
15
90
|
end
|
16
91
|
end
|
data/lib/mumukit/core/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mumukit-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Franco Leonardo Bulgarelli
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -186,8 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
186
186
|
- !ruby/object:Gem::Version
|
187
187
|
version: '0'
|
188
188
|
requirements: []
|
189
|
-
|
190
|
-
rubygems_version: 2.7.8
|
189
|
+
rubygems_version: 3.0.3
|
191
190
|
signing_key:
|
192
191
|
specification_version: 4
|
193
192
|
summary: Ruby core extensions for mumuki on top of active support
|