Class: Clowne::Cloner

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/clowne/cloner.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from DSL

adapter

Class Attribute Details

.declarationsObject



28
29
30
31
32
# File 'lib/clowne/cloner.rb', line 28

def declarations
  return @declarations if instance_variable_defined?(:@declarations)

  @declarations = []
end

Class Method Details

.call(object, **options) ⇒ Object

rubocop: disable Metrics/AbcSize, Metrics/MethodLength



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/clowne/cloner.rb', line 47

def call(object, **options)
  raise(UnprocessableSourceError, 'Nil is not cloneable object') if object.nil?

  options = Clowne::Utils::Options.new(options)
  current_adapter = current_adapter(options.adapter)

  raise(ConfigurationError, 'Adapter is not defined') if current_adapter.nil?

  plan =
    if options.traits.empty?
      default_plan(current_adapter: current_adapter)
    else
      plan_with_traits(options.traits, current_adapter: current_adapter)
    end

  plan = Clowne::Planner.enhance(plan, Proc.new) if block_given?

  plan = Clowne::Planner.filter_declarations(plan, options.only)

  call_operation(current_adapter, object, plan, options)
end

.default_plan(current_adapter: adapter) ⇒ Object



74
75
76
77
78
# File 'lib/clowne/cloner.rb', line 74

def default_plan(current_adapter: adapter)
  return @default_plan if instance_variable_defined?(:@default_plan)

  @default_plan = Clowne::Planner.compile(current_adapter, self)
end

.inherited(subclass) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/clowne/cloner.rb', line 17

def inherited(subclass)
  subclass.adapter(adapter) unless self == Clowne::Cloner
  subclass.declarations = declarations.dup

  return if traits.nil?

  traits.each do |name, trait|
    subclass.traits[name] = trait.dup
  end
end

.partial_apply(only, *args, **hargs) ⇒ Object

rubocop: enable Metrics/AbcSize, Metrics/MethodLength



70
71
72
# File 'lib/clowne/cloner.rb', line 70

def partial_apply(only, *args, **hargs)
  call(*args, **hargs, clowne_only_actions: prepare_only(only))
end

.plan_with_traits(ids, current_adapter: adapter) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/clowne/cloner.rb', line 80

def plan_with_traits(ids, current_adapter: adapter)
  # Cache plans for combinations of traits
  traits_id = ids.map(&:to_s).join(':')
  return traits_plans[traits_id] if traits_plans.key?(traits_id)

  traits_plans[traits_id] = Clowne::Planner.compile(
    current_adapter, self, traits: ids
  )
end

.register_trait(name, block) ⇒ Object



40
41
42
43
44
# File 'lib/clowne/cloner.rb', line 40

def register_trait(name, block)
  @traits ||= {}
  @traits[name] ||= Declarations::Trait.new
  @traits[name].extend_with(block)
end

.traitsObject



34
35
36
37
38
# File 'lib/clowne/cloner.rb', line 34

def traits
  return @traits if instance_variable_defined?(:@traits)

  @traits = {}
end