class COS::Resource
Attributes
bucket[R]
dir_count[R]
file_count[R]
path[R]
Public Class Methods
new(bucket, path, options = {})
click to toggle source
实例化COS资源迭代器
# File lib/cos/resource.rb, line 10 def initialize(bucket, path, options = {}) @bucket = bucket @path = path @more = options @results = Array.new @dir_count = 0 @file_count = 0 end
Public Instance Methods
fetch()
click to toggle source
获取列表
# File lib/cos/resource.rb, line 39 def fetch client = bucket.client resp = client.api.list(path, @more.merge({bucket: bucket.bucket_name})) # 遍历结果转换为对应的对象 @results = resp[:infos].map do |r| if r[:filesize].nil? # 目录 COSDir.new(r.merge({ bucket: bucket, path: Util.get_list_path(path, r[:name]) })) else # 文件 COSFile.new(r.merge({ bucket: bucket, path: Util.get_list_path(path, r[:name], true) })) end end || [] @dir_count = resp[:dir_count] @file_count = resp[:file_count] @more[:context] = resp[:context] @more[:has_more] = resp[:has_more] end
next() { |r| ... }
click to toggle source
实现迭代器
# File lib/cos/resource.rb, line 20 def next loop do # 从接口获取下一页结果 fetch_more if @results.empty? # 取出结果 r = @results.shift break unless r yield r end end
to_enum()
click to toggle source
返回迭代器
# File lib/cos/resource.rb, line 34 def to_enum self.enum_for(:next) end
Private Instance Methods
fetch_more()
click to toggle source
如果有更多页继续获取下一页
# File lib/cos/resource.rb, line 70 def fetch_more return if @more[:has_more] == false fetch end