Class: Shart::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/shart.rb

Overview

Sync deals with the specifics of getting each file from the source to the target.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, target) ⇒ Sync

Returns a new instance of Sync.



81
82
83
# File 'lib/shart.rb', line 81

def initialize(source, target)
  @source, @target = source, target
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



79
80
81
# File 'lib/shart.rb', line 79

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



79
80
81
# File 'lib/shart.rb', line 79

def target
  @target
end

Instance Method Details

#clean(&block) ⇒ Object

Removes files from target that don’t exist on the source.



99
100
101
102
103
104
105
106
# File 'lib/shart.rb', line 99

def clean(&block)
  @target.files.each do |object|
    unless @source.files.include? object.key
      block.call(object)
      object.destroy
    end
  end
end

#upload(&block) ⇒ Object

Upload files from target to the source.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/shart.rb', line 86

def upload(&block)
  @source.files.each do |key, file|
    object = @target.files.create({
      :key => key,
      :body => file,
      :public => true,
      :cache_control => 'max-age=0' # Disable cache on S3 so that future sharts are visible if folks web browsers.
    })
    block.call file, object
  end
end