Class: Rivendell::Import::Cut

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart) ⇒ Cut

Returns a new instance of Cut.



16
17
18
# File 'lib/rivendell/import/cut.rb', line 16

def initialize(cart)
  @cart = cart
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



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

def cart
  @cart
end

#datetimeObject

Returns the value of attribute datetime.



12
13
14
# File 'lib/rivendell/import/cut.rb', line 12

def datetime
  @datetime
end

#daypartObject

Returns the value of attribute daypart.



13
14
15
# File 'lib/rivendell/import/cut.rb', line 13

def daypart
  @daypart
end

#daysObject

Returns the value of attribute days.



14
15
16
# File 'lib/rivendell/import/cut.rb', line 14

def days
  @days
end

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/rivendell/import/cut.rb', line 10

def description
  @description
end

#isciObject

Returns the value of attribute isci.



10
11
12
# File 'lib/rivendell/import/cut.rb', line 10

def isci
  @isci
end

#isrcObject

Returns the value of attribute isrc.



10
11
12
# File 'lib/rivendell/import/cut.rb', line 10

def isrc
  @isrc
end

#numberObject

Returns the value of attribute number.



9
10
11
# File 'lib/rivendell/import/cut.rb', line 9

def number
  @number
end

#outcueObject

Returns the value of attribute outcue.



10
11
12
# File 'lib/rivendell/import/cut.rb', line 10

def outcue
  @outcue
end

Instance Method Details

#api_attributesObject



53
54
55
# File 'lib/rivendell/import/cut.rb', line 53

def api_attributes
  attributes.extract!("description", "outcue", "isrc", "isci").delete_if { |_,v| v.blank? }
end

#api_attributes?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rivendell/import/cut.rb', line 57

def api_attributes?
  api_attributes.present?
end

#as_json(options = {}) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/rivendell/import/cut.rb', line 20

def as_json(options = {})
  super(options.merge(:root => false)).tap do |as_json|
    as_json.each do |k,v|
      as_json[k] = [v.begin, v.end] if v.is_a?(Range)
    end
  end
end

#attributesObject



28
29
30
31
32
33
34
# File 'lib/rivendell/import/cut.rb', line 28

def attributes
  %w{description outcue isrc isci datetime daypart days}.inject({}) do |map, attribute|
    value = send attribute
    map[attribute] = value if value
    map
  end
end

#attributes=(attributes) ⇒ Object



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

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

#createObject



44
45
46
47
# File 'lib/rivendell/import/cut.rb', line 44

def create
  Rivendell::Import.logger.debug "Create Cut for Cart #{cart.number}"
  self.number = xport.add_cut(cart.number).number unless number
end

#db_attributes?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/rivendell/import/cut.rb', line 61

def db_attributes?
  [datetime, daypart, days].any? &:present?
end

#nameObject



49
50
51
# File 'lib/rivendell/import/cut.rb', line 49

def name
  "%06d_%03d" % [cart.number, number]
end

#updateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rivendell/import/cut.rb', line 65

def update
  if db_attributes?
    Rivendell::Import.logger.debug "Save Cut db attributes #{inspect}"
    Database.init

    db_cut = Rivendell::DB::Cut.get(name)
    if datetime
      db_cut.start_datetime, db_cut.end_datetime = datetime.begin, datetime.end
    end
    if daypart
      db_cut.start_daypart, db_cut.end_daypart = daypart.begin, daypart.end
    end
    if days
      db_cut.days = days
    end

    Rivendell::Import.logger.debug "Change Cut #{number} in DB #{db_cut.inspect}"

    db_cut.save
  end
  if api_attributes?
    Rivendell::Import.logger.debug "Change Cut #{number} via API #{api_attributes.inspect}"
    xport.edit_cut cart.number, number, api_attributes
  end
end

#xportObject



40
41
42
# File 'lib/rivendell/import/cut.rb', line 40

def xport
  cart.xport
end