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 Cab be overrided in special adapter

record

Instance of record (ActiveRecord or Sequel)



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

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 Cab be overrided in special adapter



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

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



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

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



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

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