Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Isolate #try_dup.
Browse files Browse the repository at this point in the history
  • Loading branch information
gix committed Mar 6, 2011
1 parent a169ac1 commit d971444
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 63 deletions.
4 changes: 2 additions & 2 deletions lib/dm-core/associations/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ def initialize(name, child_model, parent_model, options = {})
@options = options.dup.freeze
@child_repository_name = @options[:child_repository_name]
@parent_repository_name = @options[:parent_repository_name]
@child_properties = @options[:child_key].try_dup.freeze
@parent_properties = @options[:parent_key].try_dup.freeze
@child_properties = DataMapper::Ext.try_dup(@options[:child_key]).freeze
@parent_properties = DataMapper::Ext.try_dup(@options[:parent_key]).freeze
@min = @options[:min]
@max = @options[:max]
@reader_visibility = @options.fetch(:reader_visibility, :public)
Expand Down
44 changes: 0 additions & 44 deletions lib/dm-core/core_ext/try_dup.rb

This file was deleted.

12 changes: 12 additions & 0 deletions lib/dm-core/ext/try_dup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module DataMapper
module Ext
def self.try_dup(value)
case value
when ::TrueClass, ::FalseClass, ::NilClass, ::Module, ::Numeric, ::Symbol
value
else
value.dup
end
end
end
end
4 changes: 2 additions & 2 deletions lib/dm-core/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def update(other)
assert_valid_options(@options)

normalize = DataMapper::Ext::Hash.only(other_options, *OPTIONS - [ :conditions ]).map do |attribute, value|
instance_variable_set("@#{attribute}", value.try_dup)
instance_variable_set("@#{attribute}", DataMapper::Ext.try_dup(value))
attribute
end

Expand Down Expand Up @@ -748,7 +748,7 @@ def initialize_copy(*)
@fields = @fields.dup
@links = @links.dup
@conditions = @conditions.dup
@order = @order.try_dup
@order = DataMapper::Ext.try_dup(@order)
end

# Validate the options
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ def initialize(attributes = nil) # :nodoc:
# @api private
def initialize_copy(original)
instance_variables.each do |ivar|
instance_variable_set(ivar, instance_variable_get(ivar).try_dup)
instance_variable_set(ivar, DataMapper::Ext.try_dup(instance_variable_get(ivar)))
end

self.persisted_state = persisted_state.class.new(self)
Expand Down
8 changes: 4 additions & 4 deletions lib/dm-core/support/lazy_array.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'dm-core/core_ext/try_dup'
require 'dm-core/ext/try_dup'

class LazyArray # borrowed partially from StrokeDB
include Enumerable
Expand Down Expand Up @@ -358,9 +358,9 @@ def initialize
end

def initialize_copy(original)
@head = @head.try_dup
@tail = @tail.try_dup
@array = @array.try_dup
@head = DataMapper::Ext.try_dup(@head)
@tail = DataMapper::Ext.try_dup(@tail)
@array = DataMapper::Ext.try_dup(@array)
end

def lazy_load
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/support/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def default_for(resource)
if @default.respond_to?(:call)
@default.call(resource, self)
else
@default.try_dup
DataMapper::Ext.try_dup(@default)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/core_ext/inheritable_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.#{ivar}
return nil if self.object_id == #{self.object_id}
ivar = superclass.#{ivar}
return nil if ivar.nil?
@#{ivar} = ivar.try_dup
@#{ivar} = DataMapper::Ext.try_dup(ivar)
end
RUBY

Expand Down
16 changes: 8 additions & 8 deletions spec/unit/try_dup_spec.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
require 'spec_helper'
require 'dm-core/core_ext/try_dup'
require 'dm-core/ext/try_dup'

describe "try_dup" do
it "returns a duplicate version on regular objects" do
obj = Object.new
oth = obj.try_dup
oth = DataMapper::Ext.try_dup(obj)
obj.should_not === oth
end

it "returns self on Numerics" do
obj = 12
oth = obj.try_dup
oth = DataMapper::Ext.try_dup(obj)
obj.should === oth
end

it "returns self on Symbols" do
obj = :test
oth = obj.try_dup
oth = DataMapper::Ext.try_dup(obj)
obj.should === oth
end

it "returns self on true" do
obj = true
oth = obj.try_dup
oth = DataMapper::Ext.try_dup(obj)
obj.should === oth
end

it "returns self on false" do
obj = false
oth = obj.try_dup
oth = DataMapper::Ext.try_dup(obj)
obj.should === oth
end

it "returns self on nil" do
obj = nil
oth = obj.try_dup
oth = DataMapper::Ext.try_dup(obj)
obj.should === oth
end

it "returns self on modules" do
obj = Module.new
oth = obj.try_dup
oth = DataMapper::Ext.try_dup(obj)
obj.object_id.should == oth.object_id
end
end

0 comments on commit d971444

Please sign in to comment.