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

Returns a new instance of ResourceOperator.



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

def initialize(attrs)
  super(attrs)
end

Instance Attribute Details

#typeObject (readonly)

资源类型



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

def type
  @type
end

Instance Method Details

#created_atTime

创建时间Time类型

Returns:

  • (Time)


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

def created_at
  Time.at(ctime.to_i)
end

#deleteObject

Note:

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

删除当前资源

Examples:

resource.delete

Raises:



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

def delete
  bucket.delete(path)
  self
end

#delete!Object

Note:

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

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

Examples:

puts resource.delete!


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

def delete!
  bucket.delete!(path)
end

#exist?Boolean Also known as: exists?

判断当前资源是否存在

Examples:

puts resource.exist?

Returns:

  • (Boolean)

    是否存在

Raises:



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

def exist?
  bucket.exist?(path)
end

#statCOSFile|COSDir

Note:

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

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

Examples:

puts resource.stat.name

Returns:

  • (COSFile|COSDir)

    如果是目录则返回COSDir资源对象,是文件则返回COSFile资源对象

Raises:



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

def stat
  bucket.stat(path)
end

#to_hashHash

参数转化为Hash类型

Returns:

  • (Hash)


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

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')

Parameters:

  • biz_attr (String)

    目录/文件属性,业务端维护

Raises:



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

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类型

Returns:

  • (Time)


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

def updated_at
  Time.at(mtime.to_i)
end