Class: COS::Tree

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

Constant Summary collapse

MAX_DEPTH =
5

Instance Method Summary collapse

Methods included from Struct::Base::AttrHelper

#optional_attrs, #required_attrs

Constructor Details

#initialize(options = {}) ⇒ Tree

Returns a new instance of Tree.



12
13
14
15
16
# File 'lib/cos/tree.rb', line 12

def initialize(options = {})
  super(options)
  @tree_str = ''
  @depth    = depth || MAX_DEPTH
end

Instance Method Details

命令行打印树结构



47
48
49
50
# File 'lib/cos/tree.rb', line 47

def print_tree
  create_tree(path, [])
  puts @tree_str
end

#to_hashObject

输出Hash格式, 可以直接.to_json转化为json string

:resource => {name: '', mtime: ''...,
:children => [
  => resource, :children => [...],
  => resource, :children => [...],
  ...
]

}



42
43
44
# File 'lib/cos/tree.rb', line 42

def to_hash
  create_tree(path, [], :hash)
end

#to_objectObject

输出Object格式, 可以直接用于链式调用

:resource => resource,
:children => [
  {:resource => resource, :children => [...],
  => resource, :children => [...],
  ...
]

}



28
29
30
# File 'lib/cos/tree.rb', line 28

def to_object
  create_tree(path, [], :object)
end