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.



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

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.



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

def bucket
  @bucket
end

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#keysObject (readonly)

Returns the value of attribute keys.



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

def keys
  @keys
end

Instance Method Details

#<<(other_file) ⇒ Object



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

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



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

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

#content_type_for_key(key) ⇒ Object



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

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



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

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

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



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

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

#meta_equal?(other_file) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#missing?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def missing?(key)
  ! include? key
end

#modified?(other_file) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#sizeObject



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

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