Class: Rivendell::Import::Cart

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization, ActiveModel::Serializers::JSON
Defined in:
lib/rivendell/import/cart.rb

Defined Under Namespace

Classes: ApiUpdater, DbUpdater, Updater

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task = nil) ⇒ Cart

Returns a new instance of Cart.



38
39
40
# File 'lib/rivendell/import/cart.rb', line 38

def initialize(task = nil)
  @task = task
end

Instance Attribute Details

#clear_cutsObject Also known as: clear_cuts?

Returns the value of attribute clear_cuts.



183
184
185
# File 'lib/rivendell/import/cart.rb', line 183

def clear_cuts
  @clear_cuts
end

#default_titleObject

Returns the value of attribute default_title.



35
36
37
# File 'lib/rivendell/import/cart.rb', line 35

def default_title
  @default_title
end

#groupObject

Returns the value of attribute group.



35
36
37
# File 'lib/rivendell/import/cart.rb', line 35

def group
  @group
end

#import_optionsObject

Returns the value of attribute import_options.



155
156
157
# File 'lib/rivendell/import/cart.rb', line 155

def import_options
  @import_options
end

#numberObject

Returns the value of attribute number.



35
36
37
# File 'lib/rivendell/import/cart.rb', line 35

def number
  @number
end

#scheduler_codesObject

Returns the value of attribute scheduler_codes.



35
36
37
# File 'lib/rivendell/import/cart.rb', line 35

def scheduler_codes
  @scheduler_codes
end

#taskObject (readonly)

Returns the value of attribute task.



36
37
38
# File 'lib/rivendell/import/cart.rb', line 36

def task
  @task
end

#titleObject

Returns the value of attribute title.



35
36
37
# File 'lib/rivendell/import/cart.rb', line 35

def title
  @title
end

Instance Method Details

#as_json(options = {}) ⇒ Object



7
8
9
# File 'lib/rivendell/import/cart.rb', line 7

def as_json(options = {})
  super options.merge(:root => false)
end

#attributesObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rivendell/import/cart.rb', line 11

def attributes
  attributes = {}
  %w{number group clear_cuts title default_title scheduler_codes import_options}.each do |attribute|
    value = send attribute
    attributes[attribute] = value if value.present?
  end
  if (cut_attributes = cut.attributes).present?
    attributes["cut"] = cut_attributes
  end
  attributes
end

#attributes=(attributes) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/rivendell/import/cart.rb', line 23

def attributes=(attributes)
  attributes.each do |k,v|
    unless k == "cut"
      send "#{k}=", v
    else
      cut.attributes = v
    end
  end
end

#cart_finderObject



190
191
192
193
194
195
196
197
# File 'lib/rivendell/import/cart.rb', line 190

def cart_finder
  @cart_finder ||=
    unless Database.enabled?
      Rivendell::Import::CartFinder::ByApi.new xport
    else
      Rivendell::Import::CartFinder::ByDb.new
    end
end

#clear_cuts!Object



186
187
188
# File 'lib/rivendell/import/cart.rb', line 186

def clear_cuts!
  self.clear_cuts = true
end

#createObject



50
51
52
53
54
55
# File 'lib/rivendell/import/cart.rb', line 50

def create
  unless number
    raise "Can't create Cart, Group isn't defined" unless group.present?
    self.number = xport.add_cart(:group => group).number
  end
end

#cutObject



151
152
153
# File 'lib/rivendell/import/cart.rb', line 151

def cut
  @cut ||= Cut.new(self)
end

#find_by_title(string, options = {}) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/rivendell/import/cart.rb', line 174

def find_by_title(string, options = {})
  Rivendell::Import.logger.debug "Looking for a Cart '#{string}'"
  if remote_cart = cart_finder.find_by_title(string, options)
    Rivendell::Import.logger.debug "Found Cart #{remote_cart.number}"
    self.number = remote_cart.number
    self.import_options[:use_metadata] = false
  end
end

#import(file) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rivendell/import/cart.rb', line 160

def import(file)
  raise "File #{file.path} not found" unless file.exists?

  if clear_cuts?
    Rivendell::Import.logger.debug "Clear cuts of Cart #{number}"
    xport.clear_cuts number
  end
  cut.create

  Rivendell::Import.logger.debug "Import #{file.path} in Cut #{cut.number}"
  xport.import number, cut.number, file.path, import_options.symbolize_keys
  cut.update
end

#updateObject



57
58
59
60
61
# File 'lib/rivendell/import/cart.rb', line 57

def update
  updaters.any? do |updater|
    updater.new(self).update
  end
end

#updatersObject



63
64
65
66
67
68
# File 'lib/rivendell/import/cart.rb', line 63

def updaters
  [].tap do |updaters|
    updaters << ApiUpdater if scheduler_codes.empty?
    updaters << DbUpdater if Database.enabled?
  end
end

#xportObject



46
47
48
# File 'lib/rivendell/import/cart.rb', line 46

def xport
  task.xport
end