others 0.0.1 → 0.0.2

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: 82f1673e95d5864489442a00188a0cc4aa1afcc6bba9ac5c91d9b29126511057
4
+ data.tar.gz: 99e235fe2e4da529835e871c16dc856a5ef1412858ad6f7b18d48d17a68647e8
5
5
  SHA512:
6
- metadata.gz: 6ea51ec72f96c046587e4d44ef6aef4b7a1900f8b25718a5a53f4be3e1f21094788a11e95ec415a3ea75717e25017c5af9afb3754d4207e4c54d7ef1b1a01c5d
7
- data.tar.gz: d50ed164b2c75b0c0966adc3f3fc471450c04297e6b2f232f40a50b2dc21a2673535f5e921de5d7734df0b02edf49698d1642e2f1eed1b25b6813f8412063fcc
6
+ metadata.gz: 7d969e6b6e188401ed29c04e22e964707ad0b33f7224de643a26e3c6b5b8ed19d9b32af761064f5227bf0a6e4b9b81e3279df228b02a41b9525dcdd379fe2f47
7
+ data.tar.gz: c7937f5bd9ff9d5e8e76c4816aeb6a3a43865f4b3f4289f58fc3183c3077113dba662280fd1fa13489d00bde8d5439a64dfde05dede3fedd43e48bbe81de8905
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,14 @@
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'])
26
- ```
27
-
28
- You may also turn an existing class into a decorator:
29
-
30
- ```ruby
31
- require 'others'
32
- class MyString
33
- def initialize(s, br)
34
- @s = s
35
- @br = br
36
- end
37
- others(:s)
38
- def parts
39
- @origin.strip.split(@br)
40
- end
41
- end
42
- d = MyString.new('Jeff Lebowski')
43
- assert(d.parts == ['Jeff', 'Lebowski'])
21
+ assert(x.bar(10) == 52)
44
22
  ```
45
23
 
46
24
  That's it.
data/lib/others.rb CHANGED
@@ -26,31 +26,48 @@
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
36
- end
37
- # rubocop:enable Style/HashEachMethods
38
- # rubocop:enable Lint/UnusedBlockArgument
29
+ if is_a?(Class)
30
+ class_exec(block) do
39
31
  @block = block
40
- end
32
+ def self.method_missing(*args)
33
+ raise 'Block cannot be provided' if block_given?
34
+ instance_exec(*args, &@block)
35
+ end
41
36
 
42
- def method_missing(*args)
43
- raise 'Block cannot be provided' if block_given?
44
- instance_exec(*args, &@block)
45
- end
37
+ def self.respond_to?(_mtd, _inc = false)
38
+ true
39
+ end
46
40
 
47
- def respond_to?(_mtd, _inc = false)
48
- true
41
+ def self.respond_to_missing?(_mtd, _inc = false)
42
+ true
43
+ end
49
44
  end
45
+ else
46
+ c = Class.new do
47
+ def initialize(attrs, &block)
48
+ # rubocop:disable Style/HashEachMethods
49
+ # rubocop:disable Lint/UnusedBlockArgument
50
+ attrs.each do |k, v|
51
+ instance_eval("@#{k} = v", __FILE__, __LINE__) # @foo = v
52
+ end
53
+ # rubocop:enable Style/HashEachMethods
54
+ # rubocop:enable Lint/UnusedBlockArgument
55
+ @block = block
56
+ end
57
+
58
+ def method_missing(*args)
59
+ raise 'Block cannot be provided' if block_given?
60
+ instance_exec(*args, &@block)
61
+ end
50
62
 
51
- def respond_to_missing?(_mtd, _inc = false)
52
- true
63
+ def respond_to?(_mtd, _inc = false)
64
+ true
65
+ end
66
+
67
+ def respond_to_missing?(_mtd, _inc = false)
68
+ true
69
+ end
53
70
  end
71
+ c.new(attrs, &block)
54
72
  end
55
- c.new(attrs, &block)
56
73
  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.2'
31
31
  s.license = 'MIT'
32
32
  s.summary = 'others'
33
33
  s.description =
data/test/test_others.rb CHANGED
@@ -50,4 +50,17 @@ class TestOthers < Minitest::Test
50
50
  x.bar { |i| i + 1 }
51
51
  end
52
52
  end
53
+
54
+ def test_as_class
55
+ x = Class.new do
56
+ def self.foo(abc)
57
+ abc + 1
58
+ end
59
+ others do |*args|
60
+ args[1] + 2
61
+ end
62
+ end
63
+ assert_equal(43, x.foo(42))
64
+ assert_equal(44, x.bar(42))
65
+ end
53
66
  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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko