class PlainSite::Data::PostList

Public Class Methods

new(posts,site) click to toggle source

PostList,default sort by date desc and by name asc posts - The Post[]|String of posts or posts file path(abs or relative to site.data_path) array site - The Site

# File lib/PlainSite/Data/PostList.rb, line 11
def initialize(posts,site)
  if String===posts[0]
    @posts=posts.map {|f| Post.new f,site }
  else
    @posts=posts.map &:dup
  end
  @posts.sort! do |a,b|
    # sort by date desc and by name asc
    [b.date,a.slug] <=> [a.date,b.slug]
  end
  @site=site
  @custom_index= @posts.any? &:is_index
end

Public Instance Methods

+(other) click to toggle source
# File lib/PlainSite/Data/PostList.rb, line 121
def +(other)
  raise TypeError,"Except #{PostList} Type" unless PostList===other
  PostList.new @posts+other.to_a
end
/(page_size) click to toggle source

Install B `posts / 5` is same as `posts.paginate(page_size:5)`

# File lib/PlainSite/Data/PostList.rb, line 117
def /(page_size)
  paginate(page_size:page_size)
end
[](*args) click to toggle source

Array like slice method Return the Post or slice PostList

# File lib/PlainSite/Data/PostList.rb, line 27
def [](*args)
  if args.length==1
    i=args[0]
    if Range===i
      posts=@posts[i]
      return nil if posts.nil? || posts.empty?
      PostList.new posts,@site
    else
      post=@posts[i]
      if post
        post.next_post= i-1 < 0 ? nil : @posts[i-1] # Because -1 will index from last
        post.prev_post= @posts[i+1]
      end
      post
    end
  else
    start,len=args
    posts=@posts[start,len]
    return nil if posts.nil? || posts.empty?
    PostList.new posts,@site
  end
end
Also aliased as: slice
each() { |self| ... } click to toggle source
# File lib/PlainSite/Data/PostList.rb, line 143
def each(&block)
  block_given? or return enum_for __method__
  0.upto @posts.length-1 do |i|
    yield self[i]
  end
end
empty?() click to toggle source
# File lib/PlainSite/Data/PostList.rb, line 139
def empty?
  length==0
end
include?(p) click to toggle source

Check if contains one post p - The Post object or the String path or relpath or data_id of post

# File lib/PlainSite/Data/PostList.rb, line 128
def include?(p)
  if Post===p
    return @posts.include? p
  end
  return @posts.any? {|post| post.path==p || post.relpath==p || post.data_id==p}
end
length() click to toggle source
# File lib/PlainSite/Data/PostList.rb, line 135
def length
  @posts.length
end
paginate(opts={}) click to toggle source

Paginate post list This is a smart paginater. Version Control System friendly! What's VCS friendly? It's page nums use a revert order. The old the page's date is,the smaller the page's num is. Options:

page_size - The Integer page size number,must be more than zero,default is 10
revert_nos - The Boolean value to control if use revert order page num,default is true

Return: The PostListPage[]

# File lib/PlainSite/Data/PostList.rb, line 59
def paginate(opts={})
  revert_nos=opts[:revert_nos].nil? ? true : opts[:revert_nos]
  page_size=opts[:page_size] || 10
  total=@posts.length
  return [] if total==0

  # In revert nos case,the first page need padding to fit full page
  if revert_nos && total>page_size
    start=total % page_size
    pages=[self.slice(0,page_size)]
  else
    start=0
    pages=[]
  end

  while posts=self.slice(start,page_size)
    pages.push posts
    start+=page_size
  end

  nos_list=(1..pages.length).to_a
  display_nums=nos_list.dup
  slugs=('a'..'zzz').take pages.length # Use letters id instead of numbers

  if revert_nos
    nos_list.reverse!
    slugs.reverse!
  end
  slugs[0]='index' unless @custom_index # Category has its custom index post

  total_pages_count=pages.length
  pages= pages.zip(nos_list,display_nums,slugs).map do |a|
    posts,num,display_num,slug=a
    PostListPage.new(
      # num: num,  # It's useles
      slug: slug, display_num: display_num,
      posts: posts, site: @site,
      total_pages_count: total_pages_count,
      total_posts_count: total,
      page_size: page_size,
      revert_nos: revert_nos
    )
  end

  next_pages=[nil]+pages[0..-2]
  prev_pages=(pages[1..-1] or [])+[nil]
  pages.zip(prev_pages,next_pages) do |a|
    page,prev_page,next_page=a
    page.prev_page=prev_page
    page.next_page=next_page
    page.all_pages=pages
  end

  pages
end
slice(*args)
Alias for: []
to_a() click to toggle source
# File lib/PlainSite/Data/PostList.rb, line 150
def to_a
  @posts.dup
end
Also aliased as: to_ary
to_ary()
Alias for: to_a