Class: Store

Inherits:
Object
  • Object
show all
Defined in:
lib/store.rb

Instance Method Summary collapse

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
# File 'lib/store.rb', line 9

def initialize(files)
    #直接子级的key
    @children_key = '__children__'
    #所有后代的key
    @posterity_key = '__posterity__'
    #所有文章
    @article = Article.new

    @data = {
        #首页
        'home' => nil,
        #完整的分类
        'categories' => [],
        #所有文章列表
        'articles' => self.get_articles(files),
        #目录树
        'tree' => {
            @children_key => []
        }
    }

    self.make_tree
    self.sort @data['tree']
end

Instance Method Details

#articlesObject



44
45
46
# File 'lib/store.rb', line 44

def articles
    @data['articles']
end

#categoriesObject

获取所有的分类



35
36
37
# File 'lib/store.rb', line 35

def categories
    return @data['categories']
end

#get_articles(files) ⇒ Object

获取所有的文章列表以hash的方式保存, key即是文件路径的md5值



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/store.rb', line 59

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_categories_node_children(parent, segment) ⇒ Object

从数组的分类中,获取节点, 如果节点不存在, 则创建一个



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/store.rb', line 160

def get_categories_node_children(parent, segment)
    parent.each { | current |
        return current if current['title'] == segment
    }

    #如果没有找到, 则创建一个
    node = {
        'title' => segment,
        @children_key => []
    }

    parent.push node
    node
end

#get_children(node = ) ⇒ Object

从节点数据中, 获取items



49
50
51
# File 'lib/store.rb', line 49

def get_children(node = @data['tree'])
    node[@children_key]
end

#is_children_key(key) ⇒ Object



53
54
55
# File 'lib/store.rb', line 53

def is_children_key(key)
    key == @children_key
end

#make_treeObject

创建树状结构的索引, 以及分类



72
73
74
75
76
77
78
79
80
# File 'lib/store.rb', line 72

def make_tree
    this = self
    @data['articles'].each{ |key, article|
        dir = File::dirname(article['relative_path'])
        relative_path_md5 = article['relative_path_md5']
        this.mount_node_to_index dir, relative_path_md5
        this.mount_node_to_categories dir, relative_path_md5
    }
end

#mount_node_to_categories(url, relative_path_md5) ⇒ Object

将节点挂到分类下



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/store.rb', line 124

def mount_node_to_categories(url, relative_path_md5)
    #回调的处理
    callback = lambda { |parent, segment, index, total|
        if segment != nil
            node = self.get_categories_node_children parent, segment
            #还不是最后一个节点
            return node if index < total
        else
            node = parent
        end

        #获取文章的信息
        article = @data['articles'][relative_path_md5]
        meta = article['meta']

        #分类中的文章
        item = {
            'title' => meta['title'],
            'relative_url' => article['relative_url']
        }

        if segment == nil
            node.push item
            return node
        end

        #还没有这个键
        node[@children_key] = [] if node[@children_key] == nil
        node[@children_key].push item
        return node
    }

    self.mount_node_to_tree @data['categories'], url, @children_key, callback
end

#mount_node_to_index(url, relative_path_md5) ⇒ Object

将节点挂到索引上



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/store.rb', line 98

def mount_node_to_index(url, relative_path_md5)
    key = @children_key
    #回调的处理
    callback = lambda { |node, segment, index, total|
        #根节点, 直接插入到items中
        if segment == nil
            node[@children_key].push relative_path_md5
            return node
        end

        #如果没有找到, 则创建新的节点
        current_node = node[segment]
        if not current_node
            current_node = Hash.new()
            current_node[key] = Array.new
            node[segment] = current_node
        end

        current_node[key].push relative_path_md5
        return current_node
    }

    self.mount_node_to_tree @data['tree'], url, @children_key, callback
end

#mount_node_to_tree(root, url, children_key, callback) ⇒ Object

当前的路径挂到正确的路径上



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/store.rb', line 83

def mount_node_to_tree(root, url, children_key, callback)
    #根目录下的, 直接挂上去
    return callback.call root, nil, 0, 0 if url == '.'

    node = root
    #将路径分成段, 如果没存在这个节点, 则创建
    index = 1
    segments = url.split('/')
    segments.each{ |segment|
        node = callback.call node, segment, index, segments.length
        index = index + 1
    }
end

#sort(node) ⇒ Object

给所有的文件夹排序



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/store.rb', line 204

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

#treeObject

获取



40
41
42
# File 'lib/store.rb', line 40

def tree
    @data['tree']
end