cow_proxy 0.1.1 → 0.1.2
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 +4 -4
- data/lib/cow_proxy.rb +23 -8
- data/lib/cow_proxy/base.rb +6 -6
- data/lib/cow_proxy/string.rb +3 -0
- data/lib/cow_proxy/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d055ad28e6ab0d7c88bb1543ab122b1b9dd310ba
|
4
|
+
data.tar.gz: 18b291113a0cd6b3b9662b4640e7087bd126297a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 607ea8e8a4a7d0bf713c65aeb55993e778f0fab06ebe9050de0a98ed7bd3e46c5c3c70cbe6388a1d03579baf45d756677954a6672a3b90ffaf95a2bf5bf280d8
|
7
|
+
data.tar.gz: 7a167b8d6f5df69eea9579e5ba161a0e780c1338411fb43ee3197dc9a98ae298289d26d6d7cfb64945f75c8d7449d82659ae4066fb459b47d53f78f1982fdc49
|
data/lib/cow_proxy.rb
CHANGED
@@ -14,7 +14,7 @@
|
|
14
14
|
module CowProxy
|
15
15
|
class << self
|
16
16
|
# @!visibility private
|
17
|
-
@@wrapper_classes = {}
|
17
|
+
@@wrapper_classes = {Integer: nil, Float: nil, Symbol: nil, TrueClass: nil, FalseClass: nil, NilClass: nil}
|
18
18
|
|
19
19
|
# Create new proxy class for klass, with copy on write enabled.
|
20
20
|
#
|
@@ -35,20 +35,26 @@ module CowProxy
|
|
35
35
|
# Register proxy to be used when wrapping an object of klass.
|
36
36
|
#
|
37
37
|
# It's called automatically when inheriting from class returned by WrapClass
|
38
|
+
# Can be called with nil proxy_klass to disable wrapping objects of klass, for
|
39
|
+
# example Integer is registered with nil because there is no point in wrapping
|
40
|
+
# immutable classes.
|
38
41
|
#
|
39
42
|
# @return proxy_klass
|
40
43
|
def register_proxy(klass, proxy_klass)
|
41
|
-
debug "register proxy for #{klass} with #{proxy_klass} < #{proxy_klass.superclass}" unless @@wrapper_classes[klass]
|
44
|
+
debug { "register proxy for #{klass} with #{proxy_klass} < #{proxy_klass.superclass}" } unless @@wrapper_classes[klass]
|
42
45
|
@@wrapper_classes[klass] ||= proxy_klass
|
43
46
|
end
|
44
47
|
|
45
48
|
# Returns a proxy wrapping obj, using registered class for obj's class.
|
46
49
|
# If no class is registered for obj's class, it uses default proxy, without
|
47
|
-
# copy on write
|
50
|
+
# copy on write.
|
51
|
+
#
|
52
|
+
# If class is registered with nil Proxy, return obj.
|
48
53
|
#
|
49
54
|
# @return wrapped obj with CowProxy class
|
50
55
|
def wrap(obj)
|
51
|
-
wrapper_class(obj)
|
56
|
+
klass = wrapper_class(obj)
|
57
|
+
klass ? klass.new(obj) : obj
|
52
58
|
end
|
53
59
|
|
54
60
|
# Returns proxy wrapper class for obj.
|
@@ -59,20 +65,29 @@ module CowProxy
|
|
59
65
|
# if none is registered
|
60
66
|
def wrapper_class(obj)
|
61
67
|
# only classes with defined wrapper and Structs has COW enabled by default
|
62
|
-
@@wrapper_classes
|
68
|
+
if @@wrapper_classes.has_key?(obj.class)
|
69
|
+
@@wrapper_classes[obj.class]
|
70
|
+
else
|
71
|
+
_WrapClass(obj.class, obj.class < Struct, true)
|
72
|
+
end
|
63
73
|
end
|
64
74
|
|
65
75
|
# Print debug line if debug is enabled (ENV['DEBUG'] true)
|
76
|
+
# Accepts a block instead of line, so interpolation is skipped
|
77
|
+
# when debug is disabled
|
78
|
+
#
|
66
79
|
# @param [String] line debug line to print
|
67
80
|
# @return nil
|
68
|
-
def debug(line)
|
69
|
-
|
81
|
+
def debug(line = nil)
|
82
|
+
return unless ENV['DEBUG']
|
83
|
+
line ||= yield if block_given?
|
84
|
+
Kernel.puts line
|
70
85
|
end
|
71
86
|
|
72
87
|
private
|
73
88
|
def _WrapClass(klass, cow = true, register = false)
|
74
89
|
proxy_superclass = get_proxy_klass_for(klass.superclass) || Base
|
75
|
-
debug "create new proxy class for #{klass}#{" from #{proxy_superclass}" if proxy_superclass}"
|
90
|
+
debug { "create new proxy class for #{klass}#{" from #{proxy_superclass}" if proxy_superclass}" }
|
76
91
|
proxy_klass = Class.new(proxy_superclass) do |k|
|
77
92
|
k.wrapped_class = klass
|
78
93
|
end
|
data/lib/cow_proxy/base.rb
CHANGED
@@ -31,7 +31,7 @@ module CowProxy
|
|
31
31
|
inst_var = "@#{method}" if method.to_s =~ /^\w+$/
|
32
32
|
return _instance_variable_get(inst_var) if inst_var && _instance_variable_defined?(inst_var)
|
33
33
|
if method.to_s =~ /^(\w+)=$/ && _instance_variable_defined?("@#{$1}")
|
34
|
-
CowProxy.debug "remove #{$1}"
|
34
|
+
CowProxy.debug { "remove #{$1}" }
|
35
35
|
_remove_instance_variable "@#{$1}"
|
36
36
|
end
|
37
37
|
__wrapped_method__(inst_var, cow_enabled, method, *args, &block)
|
@@ -61,7 +61,7 @@ module CowProxy
|
|
61
61
|
# another CowProxy.
|
62
62
|
# @return duplicated wrapped object
|
63
63
|
def __copy_on_write__(parent = true)
|
64
|
-
CowProxy.debug "copy on write on #{__getobj__.class.name}"
|
64
|
+
CowProxy.debug { "copy on write on #{__getobj__.class.name}" }
|
65
65
|
return @delegate_dc_obj if @dc_obj_duplicated
|
66
66
|
@delegate_dc_obj = @delegate_dc_obj.dup.tap do |new_target|
|
67
67
|
@dc_obj_duplicated = true
|
@@ -83,7 +83,7 @@ module CowProxy
|
|
83
83
|
|
84
84
|
def __wrap__(value, inst_var = nil)
|
85
85
|
if value.frozen?
|
86
|
-
CowProxy.debug "wrap #{value.class.name} with parent #{
|
86
|
+
CowProxy.debug { "wrap #{value.class.name} with parent #{__getobj__.class.name}" }
|
87
87
|
wrap_value = CowProxy.wrapper_class(value).new(value, self, inst_var)
|
88
88
|
_instance_variable_set(inst_var, wrap_value) if inst_var
|
89
89
|
wrap_value
|
@@ -91,7 +91,7 @@ module CowProxy
|
|
91
91
|
end
|
92
92
|
|
93
93
|
def __wrapped_value__(inst_var, method, *args, &block)
|
94
|
-
CowProxy.debug "run on #{__getobj__.class.name} (#{__getobj__.object_id}) #{method} #{args.inspect unless args.empty?}"
|
94
|
+
CowProxy.debug { "run on #{__getobj__.class.name} (#{__getobj__.object_id}) #{method} #{args.inspect unless args.empty?}" }
|
95
95
|
value = __getobj__.__send__(method, *args, &block)
|
96
96
|
wrap_value = __wrap__(value, inst_var) if inst_var && args.empty? && block.nil?
|
97
97
|
wrap_value || value
|
@@ -101,9 +101,9 @@ module CowProxy
|
|
101
101
|
__wrapped_value__(inst_var, method, *args, &block)
|
102
102
|
rescue => e
|
103
103
|
raise unless cow && e.message =~ /^can't modify frozen/
|
104
|
-
CowProxy.debug "copy on write to run #{method} #{args.inspect unless args.empty?} (#{e.message})"
|
104
|
+
CowProxy.debug { "copy on write to run #{method} #{args.inspect unless args.empty?} (#{e.message})" }
|
105
105
|
__copy_on_write__
|
106
|
-
CowProxy.debug "new target #{__getobj__.class.name} (#{__getobj__.object_id})"
|
106
|
+
CowProxy.debug { "new target #{__getobj__.class.name} (#{__getobj__.object_id})" }
|
107
107
|
__wrapped_value__(inst_var, method, *args, &block)
|
108
108
|
end
|
109
109
|
|
data/lib/cow_proxy/string.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.2
|
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-23 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
|