others 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +16 -0
- data/lib/others.rb +10 -6
- data/others.gemspec +1 -1
- data/test/test_others.rb +35 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d7bb1572c4654244013489f1cd207efa045d899475343a7e89bd838eec809ae
|
4
|
+
data.tar.gz: 9049d9114eb4563878c6bdff1262b09165903189801d8186c028cfaf483f135e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cf475958b906f6dbdadeaf0713bf923ea1440f786facfbd0dea0fed10a3e468820d4efd1a38090542ffadd9c0882a12abeb440b7af5de723ddc01fc452d9bb0
|
7
|
+
data.tar.gz: d3323af7e30cac8789f0c6ac204e7dac49c45d6ca72142d8b564d3beddeee504a21f2c83a5cad7a0a6dc4d6310ec0f638de0040b52614157f4a3cc5d45657973
|
data/README.md
CHANGED
@@ -21,6 +21,22 @@ end
|
|
21
21
|
assert(x.bar(10) == 52)
|
22
22
|
```
|
23
23
|
|
24
|
+
You can also do this in a class:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'others'
|
28
|
+
class Foo
|
29
|
+
def foo(a)
|
30
|
+
a + 1
|
31
|
+
end
|
32
|
+
others do |*args|
|
33
|
+
args[1] + 10
|
34
|
+
end
|
35
|
+
end
|
36
|
+
assert(x.foo(10) == 11)
|
37
|
+
assert(x.bar(42) == 52)
|
38
|
+
```
|
39
|
+
|
24
40
|
That's it.
|
25
41
|
|
26
42
|
## How to contribute
|
data/lib/others.rb
CHANGED
@@ -27,18 +27,22 @@
|
|
27
27
|
# License:: MIT
|
28
28
|
def others(attrs = {}, &block)
|
29
29
|
if is_a?(Class)
|
30
|
-
class_exec(block) do
|
31
|
-
|
32
|
-
|
30
|
+
class_exec(block) do |b|
|
31
|
+
# rubocop:disable Style/ClassVars
|
32
|
+
class_variable_set(:@@__others_block__, b)
|
33
|
+
# rubocop:enable Style/ClassVars
|
34
|
+
|
35
|
+
def method_missing(*args)
|
33
36
|
raise 'Block cannot be provided' if block_given?
|
34
|
-
|
37
|
+
b = self.class.class_variable_get(:@@__others_block__)
|
38
|
+
instance_exec(*args, &b)
|
35
39
|
end
|
36
40
|
|
37
|
-
def
|
41
|
+
def respond_to?(_mtd, _inc = false)
|
38
42
|
true
|
39
43
|
end
|
40
44
|
|
41
|
-
def
|
45
|
+
def respond_to_missing?(_mtd, _inc = false)
|
42
46
|
true
|
43
47
|
end
|
44
48
|
end
|
data/others.gemspec
CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
|
28
28
|
s.required_ruby_version = '>=3.0'
|
29
29
|
s.name = 'others'
|
30
|
-
s.version = '0.0.
|
30
|
+
s.version = '0.0.3'
|
31
31
|
s.license = 'MIT'
|
32
32
|
s.summary = 'others'
|
33
33
|
s.description =
|
data/test/test_others.rb
CHANGED
@@ -35,6 +35,19 @@ class TestOthers < Minitest::Test
|
|
35
35
|
assert_equal(43, x.bar)
|
36
36
|
end
|
37
37
|
|
38
|
+
def test_as_function_setter
|
39
|
+
x = others(map: {}) do |*args|
|
40
|
+
k = args[0].to_s
|
41
|
+
if k.end_with?('=')
|
42
|
+
@map[k[..2]] = args[1]
|
43
|
+
else
|
44
|
+
@map[k]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
x.foo = 42
|
48
|
+
assert_equal(42, x.foo)
|
49
|
+
end
|
50
|
+
|
38
51
|
def test_as_function_with_args
|
39
52
|
x = others(foo: 42) do |*args|
|
40
53
|
@foo + args[1]
|
@@ -52,15 +65,35 @@ class TestOthers < Minitest::Test
|
|
52
65
|
end
|
53
66
|
|
54
67
|
def test_as_class
|
55
|
-
|
56
|
-
def
|
68
|
+
cx = Class.new do
|
69
|
+
def foo(abc)
|
57
70
|
abc + 1
|
58
71
|
end
|
59
72
|
others do |*args|
|
60
73
|
args[1] + 2
|
61
74
|
end
|
62
75
|
end
|
76
|
+
x = cx.new
|
63
77
|
assert_equal(43, x.foo(42))
|
64
78
|
assert_equal(44, x.bar(42))
|
65
79
|
end
|
80
|
+
|
81
|
+
def test_as_class_setter
|
82
|
+
cx = Class.new do
|
83
|
+
def initialize(map)
|
84
|
+
@map = map
|
85
|
+
end
|
86
|
+
others do |*args|
|
87
|
+
k = args[0].to_s
|
88
|
+
if k.end_with?('=')
|
89
|
+
@map[k[..2]] = args[1]
|
90
|
+
else
|
91
|
+
@map[k]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
x = cx.new({})
|
96
|
+
x.foo = 42
|
97
|
+
assert_equal(42, x.foo)
|
98
|
+
end
|
66
99
|
end
|