Class: Store
- Inherits:
-
Object
- Object
- Store
- Defined in:
- lib/store.rb
Instance Method Summary collapse
- #articles ⇒ Object
-
#categories ⇒ Object
获取所有的分类.
-
#get_articles(files) ⇒ Object
获取所有的文章列表 以hash的方式保存, key即是文件路径的md5值.
-
#get_children(node = @data['tree']) ⇒ Object
从节点数据中, 获取items.
-
#initialize(files) ⇒ Store
constructor
A new instance of Store.
- #is_children_key(key) ⇒ Object
-
#make_categories ⇒ Object
生成分类的列表.
-
#make_tree_index ⇒ Object
创建树状结构的索引.
-
#mount_node_to_tree(path, relative_path_md5) ⇒ Object
挂到节点上, 如果不在则创建.
-
#sort(node) ⇒ Object
给所有的文件夹排序.
-
#tree ⇒ Object
获取.
Constructor Details
#initialize(files) ⇒ Store
Returns a new instance of Store.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/store.rb', line 9 def initialize(files) #直接子级的key @children_key = '__children__' #所有后代的key @posterity_key = '__posterity__' #所有文章 @article = Article.new @data = { #首页 'home' => nil, #完整的分类 'categories' => Array.new, #所有文章列表 'articles' => self.get_articles(files), #目录树 'tree' => { @children_key => Array.new } } self.make_tree_index self.make_categories self.sort @data['tree'] end |
Instance Method Details
#articles ⇒ Object
45 46 47 |
# File 'lib/store.rb', line 45 def articles @data['articles'] end |
#categories ⇒ Object
获取所有的分类
36 37 38 |
# File 'lib/store.rb', line 36 def categories @data['categories'] end |
#get_articles(files) ⇒ Object
获取所有的文章列表以hash的方式保存, key即是文件路径的md5值
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/store.rb', line 60 def get_articles(files) result = Hash.new files.each { | file | article = @article.convert(file) key = article['relative_path_md5'] result[key] = article; } result end |
#get_children(node = @data['tree']) ⇒ Object
从节点数据中, 获取items
50 51 52 |
# File 'lib/store.rb', line 50 def get_children(node = @data['tree']) node[@children_key] end |
#is_children_key(key) ⇒ Object
54 55 56 |
# File 'lib/store.rb', line 54 def is_children_key(key) key == @children_key end |
#make_categories ⇒ Object
生成分类的列表
73 74 75 |
# File 'lib/store.rb', line 73 def make_categories end |
#make_tree_index ⇒ Object
创建树状结构的索引
78 79 80 81 82 83 84 85 |
# File 'lib/store.rb', line 78 def make_tree_index this = self @data['articles'].each{ |key, article| dir = File::dirname(article['relative_path']) relative_path_md5 = article['relative_path_md5'] this.mount_node_to_tree dir, relative_path_md5 } end |
#mount_node_to_tree(path, relative_path_md5) ⇒ Object
挂到节点上, 如果不在则创建
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/store.rb', line 88 def mount_node_to_tree(path, relative_path_md5) node = @data['tree'] if path == '.' node[@children_key].push relative_path_md5 return end path.split('/').each{ |segment| current_node = node[segment] if not current_node current_node = Hash.new() current_node[@children_key] = Array.new node[segment] = current_node end node = current_node node[@children_key].push relative_path_md5 #所有的子级,都要向root插入数据 @data['tree'][@children_key].push relative_path_md5 } end |
#sort(node) ⇒ Object
给所有的文件夹排序
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/store.rb', line 114 def sort(node) this = self node.each { | key, current | #items, 需要进行排序 if key == @children_key #根据文章的最后修改日期进行排序 current.sort! {|left, right| left_article = @data['articles'][left] right_article = @data['articles'][right] right_article['mtime'] <=> left_article['mtime'] } else #递归调用进行排序 this.sort(current) end } end |
#tree ⇒ Object
获取
41 42 43 |
# File 'lib/store.rb', line 41 def tree @data['tree'] end |