Class: Gidget::FileMapper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gidget/file_mapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileMapper

Returns a new instance of FileMapper.



15
16
17
18
19
# File 'lib/gidget/file_mapper.rb', line 15

def initialize
  @pages = Hash.new
  @posts = Array.new
  @tags = Hash.new
end

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



10
11
12
# File 'lib/gidget/file_mapper.rb', line 10

def pages
  @pages
end

#postsObject (readonly)

Returns the value of attribute posts.



11
12
13
# File 'lib/gidget/file_mapper.rb', line 11

def posts
  @posts
end

#tagsObject (readonly)

Returns the value of attribute tags.



12
13
14
# File 'lib/gidget/file_mapper.rb', line 12

def tags
  @tags
end

Instance Method Details

#loadObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gidget/file_mapper.rb', line 22

def load
  page_paths = Dir.glob("pages/**/*.txt")

  # load the page index
  page_paths.each { |file_path|
    page = Page.new(file_path)
    @pages[page.request_path] = page
  }
  
  puts "Page Index created, size = " + @pages.keys.size.to_s
  
  
  post_paths = Dir.glob("posts/**/*.txt")

  # load the post index
  post_paths.each { |file_path|
    post = Post.new(file_path)
    @posts << post
  }
  
  # sort the post index by date descending (newest will be first)
  @posts.replace @posts.sort_by { |p| Time.parse(p.date.to_s).to_i }.reverse!
  
  puts "Post Index created, size = " + @posts.size.to_s
  
  
  # load the tag index
  @posts.each { |post|
    if (post..has_key? :tags)
      tags = post.[:tags].split(',')
      
      tags.each { |tag|
        tag.strip!
        
        if (!@tags.has_key? tag)
          @tags[tag] = []
        end
        
        @tags[tag] << post
      }
    end
  }
  
  puts "Tag Index created, size = " + @tags.keys.size.to_s
end