veils 0.1.1 → 0.2.0

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: 118e44a284298758283feec3e5f526724285c9c230ea18422bfae98c47c9df59
4
- data.tar.gz: f543413fd2516f9c39096da39b2686a93fd8d899a993696026e3faf5ac5d881d
3
+ metadata.gz: 19af8ae77a2a277d8a7ef77fd012b5bb32c3cfed92a7bd26230443d00f38b383
4
+ data.tar.gz: 8a47dde0ce90a85e003d516dd2342750bfaf65dcf1aeed219225e1fdbb08dc0a
5
5
  SHA512:
6
- metadata.gz: 83bf6ac8840069abd15abdd6a8c9579fbdd1eafb052aa698e29594510725e63d3e97bae0bb4b36b9cebef044dcdc9c44d96255b01be6279ee6967fc7a3d35512
7
- data.tar.gz: 254c110d254d7888e8f2cdc976889cce64f4995daa4589a775bacff00de991ac8e669a9b05282eec2cd0c14ad8ec005f27c878f5b8f53da98b9ecf4382eebeb1
6
+ metadata.gz: 9778a8b8ca3d9bfb8a791dadaac136dba1314a135d50d5cf8923c6fb32ded4664d59d4e4fd9693058c779440b6bd92811b6c4e7e7c781c2ad89453db659f815d
7
+ data.tar.gz: 26bfde820b534a74f1a63ca73b8ca1bb77e760480eb4f236d24a9102b169041d2642b0d44ea0ac0ab5b4bf71310240880ce9f69538c32362b2dd26af4d720bb9
@@ -0,0 +1,74 @@
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
+ # Unpiercable is a simple decorator of an existing object that works
26
+ # exactly like Veil, but can never be pierced.
27
+ #
28
+ # For more information read
29
+ # {README}[https://github.com/yegor256/veils/blob/master/README.md] file.
30
+ #
31
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
32
+ # Copyright:: Copyright (c) 2020 Yegor Bugayenko
33
+ # License:: MIT
34
+ class Unpiercable
35
+ def initialize(origin, methods = {})
36
+ @origin = origin
37
+ @methods = methods
38
+ end
39
+
40
+ def to_s
41
+ method_missing(:to_s)
42
+ end
43
+
44
+ def to_json(options = nil)
45
+ return @origin.to_a.to_json(options) if @origin.is_a?(Array)
46
+ method_missing(:to_json, options)
47
+ end
48
+
49
+ def method_missing(*args)
50
+ method = args[0]
51
+ if @methods.key?(method)
52
+ @methods[method]
53
+ else
54
+ unless @origin.respond_to?(method)
55
+ raise "Method #{method} is absent in #{@origin}"
56
+ end
57
+ if block_given?
58
+ @origin.__send__(*args) do |*a|
59
+ yield(*a)
60
+ end
61
+ else
62
+ @origin.__send__(*args)
63
+ end
64
+ end
65
+ end
66
+
67
+ def respond_to?(method, include_private = false)
68
+ @origin.respond_to?(method, include_private) || @methods[method]
69
+ end
70
+
71
+ def respond_to_missing?(_method, _include_private = false)
72
+ true
73
+ end
74
+ end
@@ -0,0 +1,74 @@
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/veil'
28
+
29
+ # Veil test.
30
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
+ # Copyright:: Copyright (c) 2020 Yegor Bugayenko
32
+ # License:: MIT
33
+ class VeilTest < Minitest::Test
34
+ def test_simple
35
+ obj = Object.new
36
+ def obj.read(foo)
37
+ foo
38
+ end
39
+
40
+ def obj.touch; end
41
+ foo = Unpiercable.new(obj, read: 1)
42
+ assert_equal(1, foo.read(5))
43
+ foo.to_s
44
+ foo.touch
45
+ assert_equal(5, foo.read(5))
46
+ end
47
+
48
+ def test_behaves_like_array_with_json
49
+ origin = [1, 2, 3]
50
+ foo = Unpiercable.new(origin)
51
+ assert(foo.respond_to?(:to_json))
52
+ assert_equal(JSON.pretty_generate(origin), JSON.pretty_generate(foo))
53
+ end
54
+
55
+ def test_iterates_array
56
+ origin = [1, 2, 3]
57
+ foo = Unpiercable.new(origin, count: 1)
58
+ assert_equal(1, foo.count)
59
+ assert(!foo.empty?)
60
+ assert_equal(1, foo.count)
61
+ observed = 0
62
+ foo.each { |_| observed += 1 }
63
+ assert_equal(3, observed)
64
+ end
65
+
66
+ def test_iterates_array_twice
67
+ origin = [1, 2, 3]
68
+ foo = Veil.new(origin, count: 1)
69
+ assert_equal(1, foo.count)
70
+ observed = 0
71
+ foo.each { |_| observed += 1 }
72
+ assert_equal(3, observed)
73
+ end
74
+ end
data/veils.gemspec CHANGED
@@ -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.1.1'
34
+ s.version = '0.2.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.1.1
4
+ version: 0.2.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-11 00:00:00.000000000 Z
11
+ date: 2020-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -100,8 +100,10 @@ files:
100
100
  - README.md
101
101
  - Rakefile
102
102
  - appveyor.yml
103
+ - lib/unpiercable.rb
103
104
  - lib/veil.rb
104
105
  - logo.svg
106
+ - test/test_unpiercable.rb
105
107
  - test/test_veil.rb
106
108
  - veils.gemspec
107
109
  homepage: http://github.com/yegor256/veils
@@ -129,4 +131,5 @@ signing_key:
129
131
  specification_version: 2
130
132
  summary: Ruby Veil Objects
131
133
  test_files:
134
+ - test/test_unpiercable.rb
132
135
  - test/test_veil.rb