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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82f1673e95d5864489442a00188a0cc4aa1afcc6bba9ac5c91d9b29126511057
4
- data.tar.gz: 99e235fe2e4da529835e871c16dc856a5ef1412858ad6f7b18d48d17a68647e8
3
+ metadata.gz: 4d7bb1572c4654244013489f1cd207efa045d899475343a7e89bd838eec809ae
4
+ data.tar.gz: 9049d9114eb4563878c6bdff1262b09165903189801d8186c028cfaf483f135e
5
5
  SHA512:
6
- metadata.gz: 7d969e6b6e188401ed29c04e22e964707ad0b33f7224de643a26e3c6b5b8ed19d9b32af761064f5227bf0a6e4b9b81e3279df228b02a41b9525dcdd379fe2f47
7
- data.tar.gz: c7937f5bd9ff9d5e8e76c4816aeb6a3a43865f4b3f4289f58fc3183c3077113dba662280fd1fa13489d00bde8d5439a64dfde05dede3fedd43e48bbe81de8905
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
- @block = block
32
- def self.method_missing(*args)
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
- instance_exec(*args, &@block)
37
+ b = self.class.class_variable_get(:@@__others_block__)
38
+ instance_exec(*args, &b)
35
39
  end
36
40
 
37
- def self.respond_to?(_mtd, _inc = false)
41
+ def respond_to?(_mtd, _inc = false)
38
42
  true
39
43
  end
40
44
 
41
- def self.respond_to_missing?(_mtd, _inc = false)
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.2'
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
- x = Class.new do
56
- def self.foo(abc)
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
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko