Class: Clowne::Adapters::Base

Inherits:
Object
  • Object
show all
Includes:
Registry::Container
Defined in:
lib/clowne/adapters/base.rb,
lib/clowne/adapters/base/association.rb

Overview

ORM-independant adapter (just calls #dup). Works with nullify/finalize.

Direct Known Subclasses

ActiveRecord, Sequel

Defined Under Namespace

Classes: Association

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Registry::Container

included

Class Method Details

.dup_record(record) ⇒ Object

Duplicate record and remember record <-> clone relationship in operation Can be overrided in special adapter

record

Instance of record (ActiveRecord or Sequel)



22
23
24
25
26
27
# File 'lib/clowne/adapters/base.rb', line 22

def dup_record(record)
  record.dup.tap do |clone|
    operation = operation_class.current
    operation.add_mapping(record, clone)
  end
end

.operation_classObject

Operation class which using for cloning Can be overrided in special adapter



31
32
33
# File 'lib/clowne/adapters/base.rb', line 31

def operation_class
  Clowne::Utils::Operation
end

Instance Method Details

#clone(source, plan, params: {}) ⇒ Object

Using a plan make full duplicate of record

source

Instance of cloned object (ex: User.new(posts: posts))

plan

Array of Declarations

params

Custom params hash



40
41
42
43
44
45
46
47
# File 'lib/clowne/adapters/base.rb', line 40

def clone(source, plan, params: {})
  declarations = plan.declarations
  init_record = init_record(self.class.dup_record(source))

  declarations.inject(init_record) do |record, (type, declaration)|
    resolver_for(type).call(source, record, declaration, params: params, adapter: self)
  end
end

#init_record(record) ⇒ Object



49
50
51
52
# File 'lib/clowne/adapters/base.rb', line 49

def init_record(record)
  # Override in custom adapters
  record
end