Class: Cynq::Directory

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

Direct Known Subclasses

Local, Remote

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name) ⇒ Directory

Returns a new instance of Directory.



8
9
10
11
12
# File 'lib/cynq/directory.rb', line 8

def initialize(bucket_name)
  establish_connection
  find_bucket(bucket_name)
  read_current_files
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



6
7
8
# File 'lib/cynq/directory.rb', line 6

def bucket
  @bucket
end

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/cynq/directory.rb', line 6

def connection
  @connection
end

#keysObject (readonly)

Returns the value of attribute keys.



6
7
8
# File 'lib/cynq/directory.rb', line 6

def keys
  @keys
end

Instance Method Details

#<<(other_file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cynq/directory.rb', line 26

def <<(other_file)
  if file = self[other_file.key]
    file.body = other_file.body
  else
    file = @bucket.files.new({
      :key => other_file.key,
      :body => other_file.body
    })
  end
  ct = content_type_for_key(other_file.key)
  file.content_type = ct if ct
  file.public = true
  file.save
end

#[](key) ⇒ Object



22
23
24
# File 'lib/cynq/directory.rb', line 22

def [](key)
  @current_files[key]
end

#content_type_for_key(key) ⇒ Object



63
64
65
66
# File 'lib/cynq/directory.rb', line 63

def content_type_for_key(key)
  types = MIME::Types.type_for(File.extname(key))
  types.empty? ? nil : types.first.to_s
end

#delete(key) ⇒ Object



41
42
43
44
45
# File 'lib/cynq/directory.rb', line 41

def delete(key)
  if file = self[key]
    file.destroy
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/cynq/directory.rb', line 14

def include?(key)
  keys.include?(key)
end

#inspectObject



55
56
57
# File 'lib/cynq/directory.rb', line 55

def inspect
  "#{self.class.name}: #{@bucket.key} (#{keys.size} files, #{size} bytes)"
end

#meta_equal?(other_file) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/cynq/directory.rb', line 51

def meta_equal?(other_file)
  self[other_file.key].content_type == other_file.content_type
end

#missing?(key) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cynq/directory.rb', line 18

def missing?(key)
  ! include? key
end

#modified?(other_file) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/cynq/directory.rb', line 47

def modified?(other_file)
  other_file.etag != self[other_file.key].etag
end

#sizeObject



59
60
61
# File 'lib/cynq/directory.rb', line 59

def size
  @current_files.values.inject(0) { |sum,file| sum + file.content_length }
end