Class: Sequel::Plugins::DeepDup::DeepDupper
- Inherits:
-
Object
- Object
- Sequel::Plugins::DeepDup::DeepDupper
- Defined in:
- lib/sequel_deep_dup.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#omit_records ⇒ Object
readonly
Returns the value of attribute omit_records.
Instance Method Summary collapse
- #dup ⇒ Object
- #dup_associations(instance, copy, associations) ⇒ Object
-
#initialize(instance, omit = []) ⇒ DeepDupper
constructor
A new instance of DeepDupper.
- #shallow_dup ⇒ Object
Constructor Details
#initialize(instance, omit = []) ⇒ DeepDupper
Returns a new instance of DeepDupper.
10 11 12 13 |
# File 'lib/sequel_deep_dup.rb', line 10 def initialize instance, omit = [] @instance = instance @omit_records = omit end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
8 9 10 |
# File 'lib/sequel_deep_dup.rb', line 8 def instance @instance end |
#omit_records ⇒ Object (readonly)
Returns the value of attribute omit_records.
8 9 10 |
# File 'lib/sequel_deep_dup.rb', line 8 def omit_records @omit_records end |
Instance Method Details
#dup ⇒ Object
22 23 24 25 26 27 |
# File 'lib/sequel_deep_dup.rb', line 22 def dup copy = shallow_dup.extend(InstanceHooks::InstanceMethods) omit_records << instance dup_associations(instance, copy, instance.class.associations) copy end |
#dup_associations(instance, copy, associations) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/sequel_deep_dup.rb', line 15 def dup_associations instance, copy, associations associations.each do |name| next unless refl = instance.class.association_reflection(name) [*instance.send(name)].compact.each { |rec| instantiate_associated(copy, refl, rec) } end end |
#shallow_dup ⇒ Object
29 30 31 32 33 34 |
# File 'lib/sequel_deep_dup.rb', line 29 def shallow_dup klass = instance.class attributes = instance.to_hash.dup [*klass.primary_key].each { |attr| attributes.delete(attr) } klass.new attributes end |