Class: Rubydora::Transaction

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/rubydora/transactions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, &block) ⇒ Transaction

Returns a new instance of Transaction.



104
105
106
107
# File 'lib/rubydora/transactions.rb', line 104

def initialize repository, &block
  @repository = repository
  with_transactions(&block)
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



100
101
102
# File 'lib/rubydora/transactions.rb', line 100

def repository
  @repository
end

Instance Method Details

#rollbackObject

Roll-back transactions by reversing their outcomes (or, in some cases, re-ingesting the object at the previous state.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rubydora/transactions.rb', line 130

def rollback
  without_transactions do
  repository.transactions_log.delete_if do |(method, options)|

    begin
      case method
        when :ingest
          repository.purge_object :pid => options[:pid]

        when :modify_object
          repository.modify_object options

        when :add_datastream
          repository.purge_datastream :pid => options[:pid], :dsid => options[:dsid]

        when :add_relationship
          repository.purge_relationship options[:options].merge(:pid => options[:pid])

        when :purge_relationship
          repository.add_relationship options[:options].merge(:pid => options[:pid])

        when :purge_object
          repository.ingest :pid => options[:pid], :file => options[:foxml]

        when :set_datastream_options
          repository.set_datastream_options options

        when :modify_datastream
          repository.purge_object :pid => options[:pid] rescue nil
          repository.ingest :pid => options[:pid], :file => options[:foxml]

        when :purge_datastream
          repository.purge_object :pid => options[:pid] rescue nil
          repository.ingest :pid => options[:pid], :file => options[:foxml]
      end
    rescue
      # no-op
    end

    run_hook :after_rollback, :pid => options[:pid], :method => method, :options => options

  end
  end
  true
end

#with_transactions {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



109
110
111
112
113
114
115
116
# File 'lib/rubydora/transactions.rb', line 109

def with_transactions &block
  old_state = Rubydora::Transactions.use_transactions
  Rubydora::Transactions.use_transactions = true

  yield(self)

  Rubydora::Transactions.use_transactions = old_state
end

#without_transactions {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



118
119
120
121
122
123
124
125
# File 'lib/rubydora/transactions.rb', line 118

def without_transactions &block
  old_state = Rubydora::Transactions.use_transactions
  Rubydora::Transactions.use_transactions = false

  yield(self)

  Rubydora::Transactions.use_transactions = old_state
end