mumukit-core 1.13.0 → 1.14.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f1171e9bc75ea6096afe03f8a30b15c32efafa0abbd2aef71f5ba96a32da23f
4
- data.tar.gz: fad237169e5d66e359d831f3abe2456d42e82d2765ca821ebca8a4078357afc2
3
+ metadata.gz: cc881d626353e8ffa9fbd7f6710158db924bfcffd4ce3f7bed535a744d20c5e6
4
+ data.tar.gz: effc6a4fcd8de536ecd6eb52716d0aec3d6fe43f137d0fcd46a4f4f9c7944414
5
5
  SHA512:
6
- metadata.gz: 201065bf24ae05cc63f5bd631d7949a188a9f7fdcc87fce7516375b7e9e0403d007fb274d3add8561119a73efb1d10628df75eae308e679b389de692beb38251
7
- data.tar.gz: e45109ef518aa076021c23a3d04bb619b6934f6c5a05c6485373691ba88686ef07e6e99294bda722257cd26a3088a0ba30ca6ebf2ad8cd83b8b487e79b815c75
6
+ metadata.gz: 6f2038b0dd25424fc31c1e051bd49655a8965a0d80111a25ca351595597d6f453530850314d31a681ba52774531f62264ddd39f5b0705a2c57885ae42202e067
7
+ data.tar.gz: 5aac0b02e4eafea271f906400e98c0ee6dfc364edfd2de0fe3670f85e0c35741bcd987969cd1efaef0f023126159b88dc6016dc9bd486ef492b8c7e626d4b007
@@ -6,11 +6,86 @@ class Module
6
6
  end
7
7
  end
8
8
 
9
- def patch(method_name, &block)
10
- method_proc = instance_method method_name
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
- define_method method_name do |*args|
13
- instance_exec(*args, method_proc.bind(self), &block)
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
@@ -1,5 +1,5 @@
1
1
  module Mumukit
2
2
  module Core
3
- VERSION = '1.13.0'
3
+ VERSION = '1.14.0'
4
4
  end
5
5
  end
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.13.0
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: 2018-11-24 00:00:00.000000000 Z
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
- rubyforge_project:
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