Class: COS::ResourceOperator

Inherits:
Struct::Base show all
Defined in:
lib/cos/resource.rb

Overview

COS资源,文件与目录的共有操作

Direct Known Subclasses

COSDir, COSFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Struct::Base::AttrHelper

#optional_attrs, #required_attrs

Constructor Details

#initialize(attrs) ⇒ ResourceOperator


93
94
95
# File 'lib/cos/resource.rb', line 93

def initialize(attrs)
  super(attrs)
end

Instance Attribute Details

#typeObject (readonly)

资源类型


88
89
90
# File 'lib/cos/resource.rb', line 88

def type
  @type
end

Instance Method Details

#created_atTime

创建时间Time类型


100
101
102
# File 'lib/cos/resource.rb', line 100

def created_at
  Time.at(ctime.to_i)
end

#deleteObject

Note:

非空目录及根目录不可删除,会抛出异常

删除当前资源

Examples:

resource.delete

Raises:


184
185
186
187
# File 'lib/cos/resource.rb', line 184

def delete
  bucket.delete(path)
  self
end

#delete!Object

Note:

非空目录及根目录不可删除, 返回false

删除当前资源, 不会抛出异常而是返回布尔值

Examples:

puts resource.delete!

195
196
197
# File 'lib/cos/resource.rb', line 195

def delete!
  bucket.delete!(path)
end

#exist?Boolean Also known as: exists?

判断当前资源是否存在

Examples:

puts resource.exist?

Raises:


139
140
141
# File 'lib/cos/resource.rb', line 139

def exist?
  bucket.exist?(path)
end

#statCOSFile|COSDir

Note:

如查询根目录('/', '')可以获取到bucket信息, 返回COSDir

获取(刷新)当前资源的状态

Examples:

puts resource.stat.name

Raises:


155
156
157
# File 'lib/cos/resource.rb', line 155

def stat
  bucket.stat(path)
end

#to_hashHash

参数转化为Hash类型


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cos/resource.rb', line 114

def to_hash
  hash = {
      type:   type,
      bucket: bucket.bucket_name,
      path:   path,
      name:   name,
      ctime:  ctime,
      mtime:  mtime,
  }

  optional_attrs.each do |key|
    hash[key] = send(key.to_s) if respond_to?(key) and send(key.to_s) != nil
  end

  hash
end

#update(biz_attr) ⇒ Object

Note:

根目录('/') 不可更新, 否则会抛出异常

更新当前资源的属性

Examples:

resource.update('i am the attr')

Raises:


169
170
171
172
173
174
# File 'lib/cos/resource.rb', line 169

def update(biz_attr)
  bucket.update(path, biz_attr)
  @mtime    = Time.now.to_i.to_s
  @biz_attr = biz_attr
  self
end

#updated_atTime

更新时间Time类型


107
108
109
# File 'lib/cos/resource.rb', line 107

def updated_at
  Time.at(mtime.to_i)
end