veils 0.2.0 → 0.3.0

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: 19af8ae77a2a277d8a7ef77fd012b5bb32c3cfed92a7bd26230443d00f38b383
4
- data.tar.gz: 8a47dde0ce90a85e003d516dd2342750bfaf65dcf1aeed219225e1fdbb08dc0a
3
+ metadata.gz: d99efbe23df9b3d5cd3841fa0f9a0c2c718b0962f1099fba7f2643ea2aa56cfd
4
+ data.tar.gz: 2d02c6413c4cb4289266f42924b4d3649b1604e47c3e12bb404c605e3e55d124
5
5
  SHA512:
6
- metadata.gz: 9778a8b8ca3d9bfb8a791dadaac136dba1314a135d50d5cf8923c6fb32ded4664d59d4e4fd9693058c779440b6bd92811b6c4e7e7c781c2ad89453db659f815d
7
- data.tar.gz: 26bfde820b534a74f1a63ca73b8ca1bb77e760480eb4f236d24a9102b169041d2642b0d44ea0ac0ab5b4bf71310240880ce9f69538c32362b2dd26af4d720bb9
6
+ metadata.gz: d6a93d529548ef92f5a5b280b8fa12bf1febb125314f91debb2fdf626c66a9acc521a91c2109bb20d5a3b27c65499220b95a5f447793cbcaf68ac6c2443c7f91
7
+ data.tar.gz: 62cb2d7f4358289997c01c0632ea230fd0dbbb8e53af99cde363648fa046cbcc02202f069fcb0e66d125e9430693f61ea966e587b8c9b36b10326dc5c9ac3302
data/README.md CHANGED
@@ -13,6 +13,9 @@
13
13
  [![Hits-of-Code](https://hitsofcode.com/github/yegor256/veils)](https://hitsofcode.com/view/github/yegor256/veils)
14
14
  [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/veils/blob/master/LICENSE.txt)
15
15
 
16
+ Read this blog post first:
17
+ [Veil Objects to Replace DTOs](https://www.yegor256.com/2020/05/19/veil-objects.html)
18
+
16
19
  First, install it:
17
20
 
18
21
  ```bash
@@ -29,7 +32,26 @@ obj = Veil.new(obj, to_s: 'Hello, world!')
29
32
  The method `to_s` will return `Hello, world!` until some other
30
33
  method is called and the veil is "pierced."
31
34
 
32
- Keep in mind that `Veil` is thread-safe.
35
+ You can also use `Unpiercable` decorator, which will never be pierced:
36
+ a very good instrument for data memoization.
37
+
38
+ You can also try `AlterOut`, which lets you modify the output
39
+ of object methods on fly:
40
+
41
+ ```ruby
42
+ require 'alterout'
43
+ obj = AlterOut.new(obj, to_s: proc { |s| s + 'extra tail' })
44
+ ```
45
+
46
+ There is also `AlterIn` decorator, to modify incoming method arguments
47
+ (the result of the `proc` will replace the list of input arguments):
48
+
49
+ ```ruby
50
+ require 'alterin'
51
+ obj = AlterIn.new(obj, print: proc { |i| [i + 1] })
52
+ ```
53
+
54
+ Keep in mind that all classes are thread-safe.
33
55
 
34
56
  ## How to contribute
35
57
 
data/Rakefile CHANGED
@@ -24,8 +24,6 @@ require 'rubygems'
24
24
  require 'rake'
25
25
  require 'rake/clean'
26
26
 
27
- CLEAN = FileList['coverage']
28
-
29
27
  def name
30
28
  @name ||= File.basename(Dir['*.gemspec'].first, '.*')
31
29
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2020 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ # Decorator to modify method inputs.
26
+ #
27
+ # For more information read
28
+ # {README}[https://github.com/yegor256/veils/blob/master/README.md] file.
29
+ #
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2020 Yegor Bugayenko
32
+ # License:: MIT
33
+ class AlterIn
34
+ def initialize(origin, methods = {})
35
+ @origin = origin
36
+ @methods = methods
37
+ end
38
+
39
+ def to_s
40
+ method_missing(:to_s)
41
+ end
42
+
43
+ def to_json(options = nil)
44
+ return @origin.to_a.to_json(options) if @origin.is_a?(Array)
45
+ method_missing(:to_json, options)
46
+ end
47
+
48
+ def method_missing(*args)
49
+ method = args[0]
50
+ unless @origin.respond_to?(method)
51
+ raise "Method #{method} is absent in #{@origin}"
52
+ end
53
+ inputs = args[1..-1]
54
+ inputs = @methods[method].call(inputs) if @methods.key?(method)
55
+ if block_given?
56
+ @origin.__send__(*([method] + inputs)) do |*a|
57
+ yield(*a)
58
+ end
59
+ else
60
+ @origin.__send__(*([method] + inputs))
61
+ end
62
+ end
63
+
64
+ def respond_to?(method, include_private = false)
65
+ @origin.respond_to?(method, include_private) || @methods[method]
66
+ end
67
+
68
+ def respond_to_missing?(_method, _include_private = false)
69
+ true
70
+ end
71
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2020 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ # Decorator to modify method outputs.
26
+ #
27
+ # For more information read
28
+ # {README}[https://github.com/yegor256/veils/blob/master/README.md] file.
29
+ #
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2020 Yegor Bugayenko
32
+ # License:: MIT
33
+ class AlterOut
34
+ def initialize(origin, methods = {})
35
+ @origin = origin
36
+ @methods = methods
37
+ end
38
+
39
+ def to_s
40
+ method_missing(:to_s)
41
+ end
42
+
43
+ def to_json(options = nil)
44
+ return @origin.to_a.to_json(options) if @origin.is_a?(Array)
45
+ method_missing(:to_json, options)
46
+ end
47
+
48
+ def method_missing(*args)
49
+ method = args[0]
50
+ unless @origin.respond_to?(method)
51
+ raise "Method #{method} is absent in #{@origin}"
52
+ end
53
+ out =
54
+ if block_given?
55
+ @origin.__send__(*args) do |*a|
56
+ yield(*a)
57
+ end
58
+ else
59
+ @origin.__send__(*args)
60
+ end
61
+ out = @methods[method].call(out) if @methods.key?(method)
62
+ out
63
+ end
64
+
65
+ def respond_to?(method, include_private = false)
66
+ @origin.respond_to?(method, include_private) || @methods[method]
67
+ end
68
+
69
+ def respond_to_missing?(_method, _include_private = false)
70
+ true
71
+ end
72
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2020 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'minitest/autorun'
26
+ require 'json'
27
+ require_relative '../lib/alterin'
28
+
29
+ # AlterIn test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2020 Yegor Bugayenko
32
+ # License:: MIT
33
+ class AlterInTest < Minitest::Test
34
+ def test_simple
35
+ obj = Object.new
36
+ def obj.plus(first, second)
37
+ first + second
38
+ end
39
+ foo = AlterIn.new(obj, plus: proc { |a, b| [a + 1, b + 1] })
40
+ assert_equal(7, foo.plus(2, 3))
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # (The MIT License)
4
+ #
5
+ # Copyright (c) 2020 Yegor Bugayenko
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the 'Software'), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'minitest/autorun'
26
+ require 'json'
27
+ require_relative '../lib/alterout'
28
+
29
+ # AlterOut test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2020 Yegor Bugayenko
32
+ # License:: MIT
33
+ class AlterOutTest < Minitest::Test
34
+ def test_simple
35
+ obj = Object.new
36
+ def obj.read(val)
37
+ val
38
+ end
39
+ foo = AlterOut.new(obj, read: proc { |r| r + 1 })
40
+ assert_equal(6, foo.read(5))
41
+ end
42
+ end
@@ -24,13 +24,13 @@
24
24
 
25
25
  require 'minitest/autorun'
26
26
  require 'json'
27
- require_relative '../lib/veil'
27
+ require_relative '../lib/unpiercable'
28
28
 
29
29
  # Veil test.
30
30
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
31
  # Copyright:: Copyright (c) 2020 Yegor Bugayenko
32
32
  # License:: MIT
33
- class VeilTest < Minitest::Test
33
+ class UnpiercableTest < Minitest::Test
34
34
  def test_simple
35
35
  obj = Object.new
36
36
  def obj.read(foo)
@@ -42,7 +42,7 @@ class VeilTest < Minitest::Test
42
42
  assert_equal(1, foo.read(5))
43
43
  foo.to_s
44
44
  foo.touch
45
- assert_equal(5, foo.read(5))
45
+ assert_equal(1, foo.read(5))
46
46
  end
47
47
 
48
48
  def test_behaves_like_array_with_json
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.rubygems_version = '2.3.3'
32
32
  s.required_ruby_version = '>=2.3'
33
33
  s.name = 'veils'
34
- s.version = '0.2.0'
34
+ s.version = '0.3.0'
35
35
  s.license = 'MIT'
36
36
  s.summary = 'Ruby Veil Objects'
37
37
  s.description = 'Decorate your existing Ruby object with veils
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: veils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-12 00:00:00.000000000 Z
11
+ date: 2020-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -100,9 +100,13 @@ files:
100
100
  - README.md
101
101
  - Rakefile
102
102
  - appveyor.yml
103
+ - lib/alterin.rb
104
+ - lib/alterout.rb
103
105
  - lib/unpiercable.rb
104
106
  - lib/veil.rb
105
107
  - logo.svg
108
+ - test/test_alterin.rb
109
+ - test/test_alterout.rb
106
110
  - test/test_unpiercable.rb
107
111
  - test/test_veil.rb
108
112
  - veils.gemspec
@@ -131,5 +135,7 @@ signing_key:
131
135
  specification_version: 2
132
136
  summary: Ruby Veil Objects
133
137
  test_files:
138
+ - test/test_alterin.rb
139
+ - test/test_alterout.rb
134
140
  - test/test_unpiercable.rb
135
141
  - test/test_veil.rb