deep_clonable 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Justin Balthrop
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = DeepClonable
2
+
3
+ DeepClonable is an extension that adds deep clone support to any Class. Just call
4
+ deep_clonable in the class definition. You can also define special behavior when cloning
5
+ by overriding the clone_fields method if you want, but by default, all Arrays and Hashes
6
+ will be deep cloned.
7
+
8
+ == Usage:
9
+
10
+ class Foo
11
+ deep_clonable
12
+
13
+ attr_reader :array
14
+
15
+ def initialize(array)
16
+ @array = array
17
+ end
18
+
19
+ def clone_fields
20
+ # Only deep clone a single variable, all other variables will be just be copied directly.
21
+ @array = @array.clone
22
+ end
23
+ end
24
+
25
+ foo = Foo.new([1,2,3])
26
+ bar = Foo.clone
27
+
28
+ foo.array
29
+ # => [1,2,3]
30
+
31
+ bar.array
32
+ # => [1,2,3]
33
+
34
+ bar.array << 4
35
+ bar.array
36
+ # => [1,2,3,4]
37
+
38
+ foo.array
39
+ # => [1,2,3]
40
+
41
+ == Install:
42
+
43
+ sudo gem install ninjudd-deep_clonable -s http://gems.github.com
44
+
45
+ == License:
46
+
47
+ Copyright (c) 2008 Justin Balthrop, Geni.com; Published under The MIT License, see LICENSE
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |s|
8
+ s.name = "deep_clonable"
9
+ s.summary = %Q{Add support for deep cloning to objects}
10
+ s.email = "code@justinbalthrop.com"
11
+ s.homepage = "http://github.com/ninjudd/deep_clonable"
12
+ s.description = " Add support for deep cloning to objects"
13
+ s.authors = ["Justin Balthrop"]
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ Rake::TestTask.new do |t|
21
+ t.libs << 'lib'
22
+ t.pattern = 'test/**/*_test.rb'
23
+ t.verbose = false
24
+ end
25
+
26
+ Rake::RDocTask.new do |rdoc|
27
+ rdoc.rdoc_dir = 'rdoc'
28
+ rdoc.title = 'deep_clonable'
29
+ rdoc.options << '--line-numbers' << '--inline-source'
30
+ rdoc.rdoc_files.include('README*')
31
+ rdoc.rdoc_files.include('lib/**/*.rb')
32
+ end
33
+
34
+ begin
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |t|
37
+ t.libs << 'test'
38
+ t.test_files = FileList['test/**/*_test.rb']
39
+ t.verbose = true
40
+ end
41
+ rescue LoadError
42
+ end
43
+
44
+ task :default => :test
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :build:
3
+ :patch: 2
4
+ :major: 1
5
+ :minor: 0
@@ -0,0 +1,49 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{deep_clonable}
8
+ s.version = "1.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Justin Balthrop"]
12
+ s.date = %q{2010-04-13}
13
+ s.description = %q{ Add support for deep cloning to objects}
14
+ s.email = %q{code@justinbalthrop.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION.yml",
24
+ "deep_clonable.gemspec",
25
+ "lib/deep_clonable.rb",
26
+ "test/deep_clonable_test.rb",
27
+ "test/test_helper.rb"
28
+ ]
29
+ s.homepage = %q{http://github.com/ninjudd/deep_clonable}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.5}
33
+ s.summary = %q{Add support for deep cloning to objects}
34
+ s.test_files = [
35
+ "test/deep_clonable_test.rb",
36
+ "test/test_helper.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
49
+
@@ -0,0 +1,58 @@
1
+ class Class
2
+ def deep_clonable
3
+ include DeepClonable::InstanceMethods
4
+ extend DeepClonable::ClassMethods
5
+ end
6
+ end
7
+
8
+ module DeepClonable
9
+ module InstanceMethods
10
+ def clone
11
+ cloned_object = super
12
+ cloned_object.clone_fields
13
+ cloned_object
14
+ end
15
+
16
+ def dup
17
+ cloned_object = super
18
+ cloned_object.clone_fields
19
+ cloned_object
20
+ end
21
+
22
+ # You can override clone_fields in your class to do deep clone in place.
23
+ # As it is, all Arrays and Hashes are deep cloned.
24
+ def clone_fields
25
+ instance_variables.each do |variable|
26
+ value = instance_variable_get(variable)
27
+ if value.kind_of?(Array) or value.kind_of?(Hash) or value.kind_of?(DeepClonable::InstanceMethods)
28
+ instance_variable_set(variable, value.clone)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ module ClassMethods
35
+ # Use this to define cloning version of the given in-place methods.
36
+ #
37
+ # This method supports a common idiom where you create in-place and
38
+ # cloning versions of most operations. This method allows you to
39
+ # define only the in-place version (with the trailing bang (!),
40
+ # that part is important) and specify the cloning versions by name
41
+ # using this method.
42
+ def clone_method(clone_method_name, method_name = nil)
43
+ clone_method_name = clone_method_name.to_s.gsub(/\!$/,'')
44
+ method_name ||= "#{clone_method_name}!"
45
+ class_eval %{
46
+ def #{clone_method_name}(*args)
47
+ object = self.clone
48
+ object.#{method_name}(*args)
49
+ if block_given?
50
+ yield object
51
+ else
52
+ object
53
+ end
54
+ end
55
+ }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TestClass
4
+ deep_clonable
5
+
6
+ attr_reader :num
7
+
8
+ def initialize(n)
9
+ @num = n
10
+ end
11
+
12
+ clone_method :+, :add!
13
+ def add!(other)
14
+ @num += other.num
15
+ end
16
+
17
+ clone_method :invert
18
+ def invert!
19
+ @num = -(@num)
20
+ end
21
+ end
22
+
23
+ class DeepClonableTest < Test::Unit::TestCase
24
+ should 'clone method' do
25
+ a = TestClass.new(10)
26
+ b = a.invert
27
+
28
+ assert_equal 10, a.num
29
+ assert_equal 10, -b.num
30
+ end
31
+
32
+ should 'clone method with rename' do
33
+ a = TestClass.new(10)
34
+ b = TestClass.new(20)
35
+ c = a + b
36
+
37
+ assert_equal 30, c.num
38
+ assert_equal 20, b.num
39
+ assert_equal 10, a.num
40
+ end
41
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib/')
7
+ require 'deep_clonable'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deep_clonable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Justin Balthrop
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "\tAdd support for deep cloning to objects"
17
+ email: code@justinbalthrop.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - deep_clonable.gemspec
31
+ - lib/deep_clonable.rb
32
+ - test/deep_clonable_test.rb
33
+ - test/test_helper.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/ninjudd/deep_clonable
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Add support for deep cloning to objects
62
+ test_files:
63
+ - test/deep_clonable_test.rb
64
+ - test/test_helper.rb