Class: Clowne::Utils::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/clowne/utils/operation.rb

Overview

:nodoc: all

Direct Known Subclasses

Adapters::Sequel::Operation

Constant Summary collapse

DEFAULT_MAPPER =
Utils::CloneMapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapper) ⇒ Operation

Returns a new instance of Operation.



37
38
39
40
41
# File 'lib/clowne/utils/operation.rb', line 37

def initialize(mapper)
  @after_clone_blocks = []
  @after_persist_blocks = []
  @mapper = mapper
end

Instance Attribute Details

#clone=(value) ⇒ Object (writeonly)

Sets the attribute clone

Parameters:

  • value

    the value to set the attribute clone to.



34
35
36
# File 'lib/clowne/utils/operation.rb', line 34

def clone=(value)
  @clone = value
end

#mapperObject (readonly)

Returns the value of attribute mapper.



35
36
37
# File 'lib/clowne/utils/operation.rb', line 35

def mapper
  @mapper
end

Class Method Details

.clear!Object



29
30
31
# File 'lib/clowne/utils/operation.rb', line 29

def clear!
  Thread.current[THREAD_KEY] = nil
end

.currentObject



14
15
16
# File 'lib/clowne/utils/operation.rb', line 14

def current
  Thread.current[THREAD_KEY]
end

.wrap(mapper: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/clowne/utils/operation.rb', line 18

def wrap(mapper: nil)
  return yield if current

  Thread.current[THREAD_KEY] = new(mapper || DEFAULT_MAPPER.new)

  current.tap do |operation|
    operation.clone = yield
    clear!
  end
end

Instance Method Details

#add_after_clone(block) ⇒ Object



47
48
49
# File 'lib/clowne/utils/operation.rb', line 47

def add_after_clone(block)
  @after_clone_blocks.unshift(block)
end

#add_after_persist(block) ⇒ Object



43
44
45
# File 'lib/clowne/utils/operation.rb', line 43

def add_after_persist(block)
  @after_persist_blocks.unshift(block)
end

#add_mapping(origin, clone) ⇒ Object



51
52
53
# File 'lib/clowne/utils/operation.rb', line 51

def add_mapping(origin, clone)
  @mapper.add(origin, clone)
end

#persistObject



68
69
70
71
72
73
74
# File 'lib/clowne/utils/operation.rb', line 68

def persist
  to_record.save.tap do |result|
    next unless result

    run_after_persist
  end
end

#persist!Object



62
63
64
65
66
# File 'lib/clowne/utils/operation.rb', line 62

def persist!
  to_record.save!.tap do
    run_after_persist
  end
end

#run_after_cloneObject



90
91
92
# File 'lib/clowne/utils/operation.rb', line 90

def run_after_clone
  @after_clone_blocks.each(&:call)
end

#run_after_persistObject



86
87
88
# File 'lib/clowne/utils/operation.rb', line 86

def run_after_persist
  @after_persist_blocks.each(&:call)
end

#saveObject



76
77
78
79
# File 'lib/clowne/utils/operation.rb', line 76

def save
  warn "[DEPRECATION] `save` is deprecated.  Please use `persist` instead."
  @clone.save
end

#save!Object



81
82
83
84
# File 'lib/clowne/utils/operation.rb', line 81

def save!
  warn "[DEPRECATION] `save!` is deprecated.  Please use `persist!` instead."
  @clone.save!
end

#to_recordObject



55
56
57
58
59
60
# File 'lib/clowne/utils/operation.rb', line 55

def to_record
  return @_record if defined?(@_record)

  run_after_clone
  @_record = @clone
end