others 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51ad48737df6d72aa97a373e215129281d16ffd15a9d06b1250c0c5eafe7363d
4
- data.tar.gz: e62287a2f22a7a4840e5b97de44da4e3d27b1e911f2e5ef3ae8025c53e3f995a
3
+ metadata.gz: 4d7bb1572c4654244013489f1cd207efa045d899475343a7e89bd838eec809ae
4
+ data.tar.gz: 9049d9114eb4563878c6bdff1262b09165903189801d8186c028cfaf483f135e
5
5
  SHA512:
6
- metadata.gz: 6ea51ec72f96c046587e4d44ef6aef4b7a1900f8b25718a5a53f4be3e1f21094788a11e95ec415a3ea75717e25017c5af9afb3754d4207e4c54d7ef1b1a01c5d
7
- data.tar.gz: d50ed164b2c75b0c0966adc3f3fc471450c04297e6b2f232f40a50b2dc21a2673535f5e921de5d7734df0b02edf49698d1642e2f1eed1b25b6813f8412063fcc
6
+ metadata.gz: 6cf475958b906f6dbdadeaf0713bf923ea1440f786facfbd0dea0fed10a3e468820d4efd1a38090542ffadd9c0882a12abeb440b7af5de723ddc01fc452d9bb0
7
+ data.tar.gz: d3323af7e30cac8789f0c6ac204e7dac49c45d6ca72142d8b564d3beddeee504a21f2c83a5cad7a0a6dc4d6310ec0f638de0040b52614157f4a3cc5d45657973
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # True Object-Oriented Decorator
1
+ # Functions as Objects
2
2
 
3
3
  [![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/others)](http://www.rultor.com/p/yegor256/others)
4
4
  [![We recommend RubyMine](https://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
@@ -11,36 +11,30 @@
11
11
  [![Hits-of-Code](https://hitsofcode.com/github/yegor256/others)](https://hitsofcode.com/view/github/yegor256/others)
12
12
  [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/others/blob/master/LICENSE.txt)
13
13
 
14
- Let's say, you have an object that you want to decorate, thus
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
- s = ' Jeff Lebowski '
20
- d = others(s, br: ' ') do
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(d.parts == ['Jeff', 'Lebowski'])
21
+ assert(x.bar(10) == 52)
26
22
  ```
27
23
 
28
- You may also turn an existing class into a decorator:
24
+ You can also do this in a class:
29
25
 
30
26
  ```ruby
31
27
  require 'others'
32
- class MyString
33
- def initialize(s, br)
34
- @s = s
35
- @br = br
28
+ class Foo
29
+ def foo(a)
30
+ a + 1
36
31
  end
37
- others(:s)
38
- def parts
39
- @origin.strip.split(@br)
32
+ others do |*args|
33
+ args[1] + 10
40
34
  end
41
35
  end
42
- d = MyString.new('Jeff Lebowski')
43
- assert(d.parts == ['Jeff', 'Lebowski'])
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
- raise 'A block is expected' unless block_given?
30
- c = Class.new do
31
- def initialize(attrs, &block)
32
- # rubocop:disable Style/HashEachMethods
33
- # rubocop:disable Lint/UnusedBlockArgument
34
- attrs.each do |k, v|
35
- instance_eval("@#{k} = v", __FILE__, __LINE__) # @foo = v
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
- def method_missing(*args)
43
- raise 'Block cannot be provided' if block_given?
44
- instance_exec(*args, &@block)
45
- end
41
+ def respond_to?(_mtd, _inc = false)
42
+ true
43
+ end
46
44
 
47
- def respond_to?(_mtd, _inc = false)
48
- true
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
- def respond_to_missing?(_mtd, _inc = false)
52
- true
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.1'
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: others
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko