Class: Awestruct::Extensions::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/extensions/paginator.rb

Defined Under Namespace

Modules: Paginated

Instance Method Summary collapse

Constructor Details

#initialize(prop_name, input_path, opts = {}) ⇒ Paginator

Returns a new instance of Paginator.



46
47
48
49
50
51
52
53
54
# File 'lib/awestruct/extensions/paginator.rb', line 46

def initialize(prop_name, input_path, opts={})
  @prop_name    = prop_name
  @input_path   = input_path
  @per_page     = opts[:per_page] || 20
  @window_size  = opts[:window_size] || 2
  @remove_input = opts.has_key?( :remove_input ) ? opts[:remove_input] : true
  @output_prefix = opts[:output_prefix] || File.dirname( @input_path )
  @collection    = opts[:collection]
end

Instance Method Details

#execute(site) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/awestruct/extensions/paginator.rb', line 56

def execute(site)
  removal_path = nil
  all = @collection || site.send( @prop_name )
  i = 1
  paginated_pages = []
  all.each_slice( @per_page ) do |slice|
    page = site.engine.find_and_load_site_page( @input_path )
    removal_path ||= page.output_path
    slice.extend( Paginated )
    page.send( "#{@prop_name}=", slice )
    if ( i == 1 )
      page.output_path = File.join( @output_prefix, File.basename( @input_path ) + ".html" )
    else
      page.output_path = File.join( @output_prefix, "page/#{i}.html" )
    end
    page.paginate_generated = true
    site.pages << page
    paginated_pages << page
    i = i + 1
  end

  if ( @remove_input )
    site.pages.reject!{|page|
      ( ! page.paginate_generated && ( page.output_path == removal_path ) )
    }
  end

  prev_page = nil
  paginated_pages.each_with_index do |page,i|
    slice = page.send( @prop_name )
    
    slice.current_page       = page
    slice.current_page_index = i
    slice.pages              = paginated_pages
    slice.window             = 1

    if ( prev_page != nil )
      prev_page.send( @prop_name ).next_page = page
      page.send( @prop_name ).previous_page  = prev_page
    end

    prev_page = page
  end

  paginated_pages.first
end