cow_proxy 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/cow_proxy.rb +3 -2
- data/lib/cow_proxy/array.rb +35 -1
- data/lib/cow_proxy/enumerable.rb +69 -0
- data/lib/cow_proxy/hash.rb +58 -1
- data/lib/cow_proxy/indexable.rb +1 -1
- data/lib/cow_proxy/set.rb +1 -0
- data/lib/cow_proxy/string.rb +7 -1
- data/lib/cow_proxy/version.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5734151ca0f1283bf177dc381ba8d360542c8592
|
4
|
+
data.tar.gz: 72ff1be5e09e74f47913c3925770a1d90b6d517a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0495f7a2a83f85d17aa7a38f5c09483c4674dcc98dc8b11f6fb6f6756b63d0fac421f899e4d0c17651e1732b0d7c8b414eb4d436f821d31151738b51a247d26
|
7
|
+
data.tar.gz: 2c75c262bbc0eb327c26d220723e42eb1e244461c45da01e0001093bec66a437a3e8c458e9de2a6e79fae96581d736c01f1b428cf3efc3bcf1b9827fb848fd8e
|
data/README.md
CHANGED
@@ -51,11 +51,11 @@ end
|
|
51
51
|
|
52
52
|
If your custom class has some getters with arguments, such as [] method of Array or Hash, you will have to define it in your Proxy so it wraps returned values and memoizes them, and override _copy_on_write to set memoized proxies to duplicated object. Wrapped object can be accessed from proxy with \__getobj\__ method. You can see an example in CowProxy::Indexable module, which is used for Array and Hash classes.
|
53
53
|
|
54
|
-
If your custom class inherits from a class with CowProxy class, you don't need to create an own class, unless you need to override some method:
|
54
|
+
If your custom class inherits from a class with CowProxy class, you don't need to create an own class, unless you need to override some method. But you must inherit from WrapClass(CustomClass) so your new proxy class is registered:
|
55
55
|
|
56
56
|
```ruby
|
57
57
|
module CowProxy
|
58
|
-
class CustomClass <
|
58
|
+
class CustomClass < WrapClass(::CustomClass)
|
59
59
|
def custom_get(arg)
|
60
60
|
return @custom_var[index] if @custom_var && @custom_var.has_key?(arg)
|
61
61
|
|
data/lib/cow_proxy.rb
CHANGED
@@ -12,6 +12,8 @@
|
|
12
12
|
# proxy = CowProxy.wrap(obj)
|
13
13
|
|
14
14
|
module CowProxy
|
15
|
+
autoload :Enumerable, 'cow_proxy/enumerable.rb'
|
16
|
+
autoload :Indexable, 'cow_proxy/indexable.rb'
|
15
17
|
class << self
|
16
18
|
# @!visibility private
|
17
19
|
@@wrapper_classes = {}
|
@@ -93,7 +95,7 @@ module CowProxy
|
|
93
95
|
end
|
94
96
|
register_proxy klass, proxy_klass if register
|
95
97
|
methods = klass.instance_methods
|
96
|
-
methods -= [:__copy_on_write__, :__wrap__, :__wrapped_value__, :__wrapped_method__, :__getobj__, :send, :===, :frozen?]
|
98
|
+
methods -= [:__copy_on_write__, :__wrap__, :__wrapped_value__, :__wrapped_method__, :__getobj__, :enum_for, :send, :===, :frozen?]
|
97
99
|
methods -= proxy_superclass.wrapped_class.instance_methods if proxy_superclass.wrapped_class
|
98
100
|
methods -= [:inspect] if ENV['DEBUG']
|
99
101
|
|
@@ -133,7 +135,6 @@ if defined? Bignum
|
|
133
135
|
end
|
134
136
|
|
135
137
|
require 'cow_proxy/base.rb'
|
136
|
-
require 'cow_proxy/indexable.rb'
|
137
138
|
require 'cow_proxy/array.rb'
|
138
139
|
require 'cow_proxy/hash.rb'
|
139
140
|
require 'cow_proxy/string.rb'
|
data/lib/cow_proxy/array.rb
CHANGED
@@ -1,6 +1,41 @@
|
|
1
1
|
module CowProxy
|
2
|
+
# Wrapper class for Array
|
2
3
|
class Array < WrapClass(::Array)
|
3
4
|
include Indexable
|
5
|
+
include ::Enumerable
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
# Calls the given block once for each element in self,
|
9
|
+
# passing wrapped element as a parameter.
|
10
|
+
#
|
11
|
+
# @yield [item] Gives each element in self to the block
|
12
|
+
# @yieldparam item Wrapped item in self
|
13
|
+
# @return [CowProxy::Array] self if block given
|
14
|
+
# @return [Enumerator] if no block given
|
15
|
+
def each
|
16
|
+
return enum_for(:each) unless block_given?
|
17
|
+
__getobj__.each.with_index do |_, i|
|
18
|
+
yield self[i]
|
19
|
+
end
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
# Invokes the given block once for each element of self,
|
24
|
+
# replacing the element with the value returned by the block.
|
25
|
+
#
|
26
|
+
# @yield [item] Gives each element in self to the block
|
27
|
+
# @yieldparam item Wrapped item in self
|
28
|
+
# @yieldreturn item to replace
|
29
|
+
# @return [CowProxy::Array] self if block given
|
30
|
+
# @return [Enumerator] if no block given
|
31
|
+
def map!
|
32
|
+
__copy_on_write__
|
33
|
+
return enum_for(:map!) unless block_given?
|
34
|
+
__getobj__.each.with_index do |_, i|
|
35
|
+
self[i] = yield(self[i])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
alias collect! map!
|
4
39
|
|
5
40
|
# Used for concatenating into another Array
|
6
41
|
# needs to return unwrapped Array
|
@@ -10,5 +45,4 @@ module CowProxy
|
|
10
45
|
__getobj__
|
11
46
|
end
|
12
47
|
end
|
13
|
-
|
14
48
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module CowProxy
|
2
|
+
# A mixin to add mutable methods for keep_if, delete_if, select! and reject!
|
3
|
+
# to wrap block params with CowProxy
|
4
|
+
module Enumerable
|
5
|
+
# Invokes the given block passing in successive elements from self,
|
6
|
+
# deleting elements for which the block returns a false value.
|
7
|
+
#
|
8
|
+
# @yield [item] Gives each element in self to the block
|
9
|
+
# @yieldparam item Wrapped item in self
|
10
|
+
# @yieldreturn [Boolean] true if item must be kept
|
11
|
+
# @return [CowProxy::Array] self if block given
|
12
|
+
# @return [Enumerator] if no block given
|
13
|
+
def keep_if(&block)
|
14
|
+
mutable_selector(:select, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Invokes the given block passing in successive elements from self,
|
18
|
+
# deleting elements for which the block returns a false value.
|
19
|
+
#
|
20
|
+
# @yield [item] Gives each element in self to the block
|
21
|
+
# @yieldparam item Wrapped item in self
|
22
|
+
# @yieldreturn [Boolean] true if item must be kept
|
23
|
+
# @return [CowProxy::Array] self if block given and changes were made
|
24
|
+
# @return [nil] if block given and no changes were made
|
25
|
+
# @return [Enumerator] if no block given
|
26
|
+
def select!(&block)
|
27
|
+
mutable_selector!(:keep_if, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Deletes every element of self for which block evaluates to true.
|
31
|
+
#
|
32
|
+
# @yield [item] Gives each element in self to the block
|
33
|
+
# @yieldparam item Wrapped item in self
|
34
|
+
# @yieldreturn [Boolean] true if item must be deleted
|
35
|
+
# @return [CowProxy::Array] self if block given
|
36
|
+
# @return [Enumerator] if no block given
|
37
|
+
def delete_if(&block)
|
38
|
+
mutable_selector(:reject, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Deletes every element of self for which block evaluates to true.
|
42
|
+
#
|
43
|
+
# @yield [item] Gives each element in self to the block
|
44
|
+
# @yieldparam item Wrapped item in self
|
45
|
+
# @yieldreturn [Boolean] true if item must be deleted
|
46
|
+
# @return [CowProxy::Array] self if block given and changes were made
|
47
|
+
# @return [nil] if block given and no changes were made
|
48
|
+
# @return [Enumerator] if no block given
|
49
|
+
def reject!(&block)
|
50
|
+
mutable_selector!(:delete_if, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def mutable_selector(method, &block)
|
55
|
+
return send(method) unless block
|
56
|
+
@delegate_dc_obj = send(method, &block).tap do
|
57
|
+
@dc_obj_duplicated = true
|
58
|
+
end
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
def mutable_selector!(method, &block)
|
63
|
+
return send(method) unless block
|
64
|
+
size = __getobj__.size
|
65
|
+
send(method, &block)
|
66
|
+
self unless __getobj__.size == size
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/cow_proxy/hash.rb
CHANGED
@@ -1,6 +1,64 @@
|
|
1
1
|
module CowProxy
|
2
|
+
# Wrapper class for Hash
|
2
3
|
class Hash < WrapClass(::Hash)
|
3
4
|
include Indexable
|
5
|
+
include ::Enumerable
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
# Calls block once for each key in hash, passing the key-value pair
|
9
|
+
# as parameters.
|
10
|
+
#
|
11
|
+
# @yield [pair] Gives each key-value pair in self to the block
|
12
|
+
# @yieldparam pair Array of key and wrapped value
|
13
|
+
# @return [CowProxy::Hash] self if block given
|
14
|
+
# @return [Enumerator] if no block given
|
15
|
+
def each
|
16
|
+
return enum_for(:each) unless block_given?
|
17
|
+
__getobj__.each_key do |k|
|
18
|
+
yield [k, self[k]]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
alias each_pair each
|
22
|
+
|
23
|
+
# Calls block once for each key in hash, passing the value as parameter.
|
24
|
+
#
|
25
|
+
# @yield [value] Gives each value in hash to the block
|
26
|
+
# @yieldparam value Wrapped value
|
27
|
+
# @return [CowProxy::Hash] self if block given
|
28
|
+
# @return [Enumerator] if no block given
|
29
|
+
def each_value
|
30
|
+
return enum_for(:each) unless block_given?
|
31
|
+
each { |_, v| yield v }
|
32
|
+
end
|
33
|
+
|
34
|
+
# Returns a new hash consisting of entries for which the block returns true.
|
35
|
+
#
|
36
|
+
# @yield [pair] Gives each key-value pair in self to the block
|
37
|
+
# @yieldparam pair Array of key and wrapped value
|
38
|
+
# @yieldreturn [Boolean] true if item must be included
|
39
|
+
# @return [CowProxy::Hash] self if block given
|
40
|
+
# @return [Enumerator] if no block given
|
41
|
+
def select
|
42
|
+
::Hash[super]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns a new hash consisting of entries for which the block returns false.
|
46
|
+
#
|
47
|
+
# @yield [pair] Gives each key-value pair in self to the block
|
48
|
+
# @yieldparam pair Array of key and wrapped value
|
49
|
+
# @yieldreturn [Boolean] true if item must not be included
|
50
|
+
# @return [CowProxy::Hash] self if block given
|
51
|
+
# @return [Enumerator] if no block given
|
52
|
+
def reject
|
53
|
+
::Hash[super]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns a new array populated with the wrapped values from hash.
|
57
|
+
#
|
58
|
+
# @return [Array] Wrapped values from hash
|
59
|
+
def values
|
60
|
+
map(&:last)
|
61
|
+
end
|
4
62
|
|
5
63
|
# Used for merging into another Hash
|
6
64
|
# needs to return unwrapped Hash
|
@@ -10,5 +68,4 @@ module CowProxy
|
|
10
68
|
__getobj__
|
11
69
|
end
|
12
70
|
end
|
13
|
-
|
14
71
|
end
|
data/lib/cow_proxy/indexable.rb
CHANGED
data/lib/cow_proxy/set.rb
CHANGED
data/lib/cow_proxy/string.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
module CowProxy
|
2
|
+
# Wrapper class for String
|
2
3
|
class String < WrapClass(::String)
|
4
|
+
# returns the wrapped object.
|
5
|
+
#
|
6
|
+
# needed to used wrapped string as parameter for send
|
7
|
+
#
|
8
|
+
# @return the wrapped object.
|
3
9
|
def to_str
|
4
|
-
__getobj__
|
10
|
+
__getobj__
|
5
11
|
end
|
6
12
|
end
|
7
13
|
end
|
data/lib/cow_proxy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cow_proxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergio Cambra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Make a COW proxy for a frozen object (or deep frozen), it will delegate
|
14
14
|
every read method to proxied object, wrap value in COW proxy if frozen. Trying to
|
@@ -24,6 +24,7 @@ files:
|
|
24
24
|
- lib/cow_proxy.rb
|
25
25
|
- lib/cow_proxy/array.rb
|
26
26
|
- lib/cow_proxy/base.rb
|
27
|
+
- lib/cow_proxy/enumerable.rb
|
27
28
|
- lib/cow_proxy/hash.rb
|
28
29
|
- lib/cow_proxy/indexable.rb
|
29
30
|
- lib/cow_proxy/set.rb
|
@@ -49,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
50
|
version: '0'
|
50
51
|
requirements: []
|
51
52
|
rubyforge_project:
|
52
|
-
rubygems_version: 2.
|
53
|
+
rubygems_version: 2.5.1
|
53
54
|
signing_key:
|
54
55
|
specification_version: 4
|
55
56
|
summary: Copy-on-write proxy class, to use with frozen objects
|