others 0.0.1 → 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 +13 -19
- data/lib/others.rb +41 -20
- data/others.gemspec +1 -1
- data/test/test_others.rb +46 -0
- 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
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Functions as Objects
|
2
2
|
|
3
3
|
[](http://www.rultor.com/p/yegor256/others)
|
4
4
|
[](https://www.jetbrains.com/ruby/)
|
@@ -11,36 +11,30 @@
|
|
11
11
|
[](https://hitsofcode.com/view/github/yegor256/others)
|
12
12
|
[](https://github.com/yegor256/others/blob/master/LICENSE.txt)
|
13
13
|
|
14
|
-
Let's say
|
15
|
-
adding new attributes and methods to it. Here is how:
|
14
|
+
Let's say you need an object that consists of a single function:
|
16
15
|
|
17
16
|
```ruby
|
18
17
|
require 'others'
|
19
|
-
|
20
|
-
|
21
|
-
def parts
|
22
|
-
@origin.strip.split(@br)
|
23
|
-
end
|
18
|
+
x = others(foo: 42) do |*args|
|
19
|
+
@foo + args[1]
|
24
20
|
end
|
25
|
-
assert(
|
21
|
+
assert(x.bar(10) == 52)
|
26
22
|
```
|
27
23
|
|
28
|
-
You
|
24
|
+
You can also do this in a class:
|
29
25
|
|
30
26
|
```ruby
|
31
27
|
require 'others'
|
32
|
-
class
|
33
|
-
def
|
34
|
-
|
35
|
-
@br = br
|
28
|
+
class Foo
|
29
|
+
def foo(a)
|
30
|
+
a + 1
|
36
31
|
end
|
37
|
-
others
|
38
|
-
|
39
|
-
@origin.strip.split(@br)
|
32
|
+
others do |*args|
|
33
|
+
args[1] + 10
|
40
34
|
end
|
41
35
|
end
|
42
|
-
|
43
|
-
assert(
|
36
|
+
assert(x.foo(10) == 11)
|
37
|
+
assert(x.bar(42) == 52)
|
44
38
|
```
|
45
39
|
|
46
40
|
That's it.
|
data/lib/others.rb
CHANGED
@@ -26,31 +26,52 @@
|
|
26
26
|
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
|
27
27
|
# License:: MIT
|
28
28
|
def others(attrs = {}, &block)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# rubocop:
|
34
|
-
|
35
|
-
|
29
|
+
if is_a?(Class)
|
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)
|
36
|
+
raise 'Block cannot be provided' if block_given?
|
37
|
+
b = self.class.class_variable_get(:@@__others_block__)
|
38
|
+
instance_exec(*args, &b)
|
36
39
|
end
|
37
|
-
# rubocop:enable Style/HashEachMethods
|
38
|
-
# rubocop:enable Lint/UnusedBlockArgument
|
39
|
-
@block = block
|
40
|
-
end
|
41
40
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
41
|
+
def respond_to?(_mtd, _inc = false)
|
42
|
+
true
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
|
45
|
+
def respond_to_missing?(_mtd, _inc = false)
|
46
|
+
true
|
47
|
+
end
|
49
48
|
end
|
49
|
+
else
|
50
|
+
c = Class.new do
|
51
|
+
def initialize(attrs, &block)
|
52
|
+
# rubocop:disable Style/HashEachMethods
|
53
|
+
# rubocop:disable Lint/UnusedBlockArgument
|
54
|
+
attrs.each do |k, v|
|
55
|
+
instance_eval("@#{k} = v", __FILE__, __LINE__) # @foo = v
|
56
|
+
end
|
57
|
+
# rubocop:enable Style/HashEachMethods
|
58
|
+
# rubocop:enable Lint/UnusedBlockArgument
|
59
|
+
@block = block
|
60
|
+
end
|
50
61
|
|
51
|
-
|
52
|
-
|
62
|
+
def method_missing(*args)
|
63
|
+
raise 'Block cannot be provided' if block_given?
|
64
|
+
instance_exec(*args, &@block)
|
65
|
+
end
|
66
|
+
|
67
|
+
def respond_to?(_mtd, _inc = false)
|
68
|
+
true
|
69
|
+
end
|
70
|
+
|
71
|
+
def respond_to_missing?(_mtd, _inc = false)
|
72
|
+
true
|
73
|
+
end
|
53
74
|
end
|
75
|
+
c.new(attrs, &block)
|
54
76
|
end
|
55
|
-
c.new(attrs, &block)
|
56
77
|
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]
|
@@ -50,4 +63,37 @@ class TestOthers < Minitest::Test
|
|
50
63
|
x.bar { |i| i + 1 }
|
51
64
|
end
|
52
65
|
end
|
66
|
+
|
67
|
+
def test_as_class
|
68
|
+
cx = Class.new do
|
69
|
+
def foo(abc)
|
70
|
+
abc + 1
|
71
|
+
end
|
72
|
+
others do |*args|
|
73
|
+
args[1] + 2
|
74
|
+
end
|
75
|
+
end
|
76
|
+
x = cx.new
|
77
|
+
assert_equal(43, x.foo(42))
|
78
|
+
assert_equal(44, x.bar(42))
|
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
|
53
99
|
end
|