duplicate 1.0.0 → 1.1.1

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
  SHA1:
3
- metadata.gz: 29cbb854fa83777d931dc560658161596fab85b6
4
- data.tar.gz: 08e794079986fe6a70c5a54868e8a70169720355
3
+ metadata.gz: bf3e85ee6b06f58563948e537b8ff4d48501a5b1
4
+ data.tar.gz: e1ec3eb858cd0444ed7d420d761be2bd75a6b22d
5
5
  SHA512:
6
- metadata.gz: 3e002aef64da4ed778fbaa004943badf3eee40080bff687cdfd0a3c556490abd758b5c35fe41a1b2ff3be08222654129406fbec881ded6249050027cb1522b9c
7
- data.tar.gz: a191a8f040f05e4d249c33f4af8b218a343d3a4e164dff4f1da808259398ff83b13c325185ffa42bd4b0f952f9e148c54bab2a1e05430f28514ca065c7942049
6
+ metadata.gz: d815cd7adf6332b79eb2a8eac2aa503b6a99f227a5c188eebb6815c0f909de944e47b61832cab6a1faefbedc169fc09ab6c51ef33b86419e53f02fbb7a115205
7
+ data.tar.gz: 0bbc228173a3ad42b395c2e4172f57a8f85e1d8ef8942582d6b29b8e8be73ef206bc8e9c851782e6869ea912e6e9da6cdb6bfcba23df9be65ec943a2e19f4e34
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
- # Duplicate
1
+ # Duplicate [![Build Status][travis-image]][travis-link]
2
2
 
3
- Duplicate is Originally extracted from rack-app project.
3
+ [travis-image]: https://travis-ci.org/adamluzsi/duplicate.rb.svg?branch=master
4
+ [travis-link]: https://travis-ci.org/adamluzsi/duplicate.rb
5
+ [travis-home]: http://travis-ci.org/
6
+
7
+ Duplicate is Originally extracted from [rack-app](http://www.rack-app.com) project.
4
8
  Allows you to Deep duplicate any ruby objects in pure ruby way.
5
9
  That way you can recursively duplicate objects and even replacing original object references with in instance variable references
6
10
 
@@ -9,6 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Adam Luzsi"]
10
10
  spec.email = ["adamluzsi@gmail.com"]
11
11
 
12
+ spec.license = 'Apache License Version 2.0'
12
13
  spec.summary = %q{Deep duplication for ruby objects in pure ruby}
13
14
  spec.description = %q{Originally extracted from rack-app project. Deep duplication for ruby objects in pure ruby, that allow you recursively duplicate objects based on a source object, even replacing original object references}
14
15
  spec.homepage = "http://www.rack-app.com"
@@ -13,17 +13,18 @@ module Duplicate
13
13
  protected
14
14
 
15
15
  def registered(object, register)
16
- register[object.object_id]
16
+ register[object.__id__]
17
17
  end
18
18
 
19
19
  def register_duplication(register, object, duplicate)
20
- register[object.object_id]= duplicate
20
+ register[object.__id__]= duplicate
21
21
  duplicate
22
22
  end
23
23
 
24
24
  def dup(register, object)
25
25
 
26
26
  return registered(object, register) if registered(object, register)
27
+ return register_duplication(register, object, object) unless identifiable?(object)
27
28
 
28
29
  case object
29
30
 
@@ -48,6 +49,12 @@ module Duplicate
48
49
  end
49
50
  end
50
51
 
52
+ def identifiable?(object)
53
+ object.class && object.respond_to?(:is_a?)
54
+ rescue NoMethodError
55
+ false
56
+ end
57
+
51
58
  def dup_array(register, object)
52
59
  duplication = dup_object(register, object)
53
60
  duplication.map! { |e| dup(register, e) }
@@ -75,16 +82,43 @@ module Duplicate
75
82
  end
76
83
 
77
84
  def dup_object(register, object)
78
- dup_instance_variables(register, object, register_duplication(register, object, object.dup))
85
+ dup_instance_variables(register, object, register_duplication(register, object, try_dup(object)))
79
86
  end
80
87
 
81
- def dup_instance_variables(register, object, duplicate)
88
+ def dup_instance_variables(register, object, duplication)
89
+ return duplication unless respond_to_instance_variables?(object)
90
+
82
91
  object.instance_variables.each do |instance_variable|
83
- value = object.instance_variable_get(instance_variable)
84
- duplicate.instance_variable_set(instance_variable, dup(register, value))
92
+ value = get_instance_variable(object, instance_variable)
93
+
94
+ set_instance_variable(duplication, instance_variable, dup(register, value))
85
95
  end
86
96
 
87
- return duplicate
97
+ return duplication
98
+ end
99
+
100
+ def get_instance_variable(object, instance_variable_name)
101
+ object.instance_variable_get(instance_variable_name)
102
+ rescue NoMethodError
103
+ object.instance_eval("#{instance_variable_name}")
104
+ end
105
+
106
+ def set_instance_variable(duplicate, instance_variable_name, value_to_set)
107
+ duplicate.instance_variable_set(instance_variable_name, value_to_set)
108
+ rescue NoMethodError
109
+ duplicate.instance_eval("#{instance_variable_name} = Marshal.load(#{Marshal.dump(value_to_set).inspect})")
110
+ end
111
+
112
+ def try_dup(object)
113
+ object.dup
114
+ rescue NoMethodError, TypeError
115
+ object
116
+ end
117
+
118
+ def respond_to_instance_variables?(object)
119
+ object.respond_to?(:instance_variables) && object.instance_variables.is_a?(Array)
120
+ rescue NoMethodError
121
+ false
88
122
  end
89
123
 
90
124
  end
@@ -1,3 +1,3 @@
1
1
  module Duplicate
2
- VERSION = "1.0.0"
3
- end
2
+ VERSION = "1.1.1"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duplicate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-31 00:00:00.000000000 Z
11
+ date: 2016-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,7 +74,8 @@ files:
74
74
  - lib/duplicate/core_ext/object.rb
75
75
  - lib/duplicate/version.rb
76
76
  homepage: http://www.rack-app.com
77
- licenses: []
77
+ licenses:
78
+ - Apache License Version 2.0
78
79
  metadata: {}
79
80
  post_install_message:
80
81
  rdoc_options: []