Class: Yuzu::Filters::Catalog

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/yuzu/filters/catalog.rb

Overview

Catalogs render one page of collected contents and have no knowledge of pagination. The auto-pagination happens earlier as part of the generation process.

Constant Summary collapse

@@kwd_defaults =

Define accessors for each of the given keyword arguments.

{
  :template => '_block.haml',
  :total => 1000,
  :per_page => 10,
  :per_row => 1,
  :page => 1,
  :offset => 0,
  :category => nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kwds, website_file, original_match_string) ⇒ Catalog

Returns a new instance of Catalog.



74
75
76
77
78
79
80
# File 'lib/yuzu/filters/catalog.rb', line 74

def initialize(kwds, website_file, original_match_string)
  @kwds = kwds
  @website_file = website_file
  @original_match_string = original_match_string

  @siteroot = website_file.root
end

Instance Attribute Details

#website_fileObject (readonly)

Returns the value of attribute website_file.



72
73
74
# File 'lib/yuzu/filters/catalog.rb', line 72

def website_file
  @website_file
end

Instance Method Details

#css_classObject



183
184
185
186
# File 'lib/yuzu/filters/catalog.rb', line 183

def css_class
  num_columns_12 = (12.0 / per_row).floor
  "span#{num_columns_12}"
end

#directive_for_page(page) ⇒ Object



140
141
142
143
144
# File 'lib/yuzu/filters/catalog.rb', line 140

def directive_for_page(page)
  with_page = @kwds.merge({:page => page})
  args = with_page.collect {|key, val| "#{key}:#{val}"}.join(", ")
  "INSERTCATALOG(#{args})"
end

#file_has_category?(file) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
224
225
226
227
228
# File 'lib/yuzu/filters/catalog.rb', line 221

def file_has_category?(file)
  file.categories.each do |cat|
    if cat.name == category.downcase.dasherize
      return true
    end
  end
  return false
end

#files_to_render_this_pageObject

Returns an array of WebsiteFile objects that are the contents of the catalog on this page.



231
232
233
234
235
# File 'lib/yuzu/filters/catalog.rb', line 231

def files_to_render_this_page
  start = (page - 1) * per_page + offset
  stop = page * per_page + offset
  target_files[start...stop]
end

#formatted_directiveObject



146
147
148
149
# File 'lib/yuzu/filters/catalog.rb', line 146

def formatted_directive
  args = @kwds.collect {|key, val| "#{key}:#{val}"}.join(", ")
  "INSERTCATALOG(#{args})"
end

#get_default_type(key) ⇒ Object



118
119
120
# File 'lib/yuzu/filters/catalog.rb', line 118

def get_default_type(key)
  @@kwd_defaults.has_key?(key) ? @@kwd_defaults[key].class : nil
end

#get_default_type_method(key) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/yuzu/filters/catalog.rb', line 105

def get_default_type_method(key)
  klass = get_default_type(key)
  if klass == String
    :to_s
  elsif klass == Fixnum
    :to_i
  elsif klass == Float
    :to_f
  else
    nil
  end
end

#get_target_filesObject



212
213
214
215
216
217
218
219
# File 'lib/yuzu/filters/catalog.rb', line 212

def get_target_files
  return [] if target_folder.nil?
  unsorted = target_folder.all_processable_children.reject {|node| node.index?}
  if not category.nil?
    unsorted = unsorted.select {|file| file_has_category?(file)}
  end
  unsorted.sort {|a, b| b. <=> a.}
end

#num_pagesObject



130
131
132
133
# File 'lib/yuzu/filters/catalog.rb', line 130

def num_pages
  return 1 if not should_paginate?
  (num_total_files.to_f / per_page.to_f).ceil
end

#num_rows_this_pageObject



237
238
239
# File 'lib/yuzu/filters/catalog.rb', line 237

def num_rows_this_page
  (number_of_files_to_show.to_f / per_row.to_f).ceil
end

#num_total_filesObject



126
127
128
# File 'lib/yuzu/filters/catalog.rb', line 126

def num_total_files
  [target_files.length, total].min
end

#number_of_files_to_showObject



241
242
243
# File 'lib/yuzu/filters/catalog.rb', line 241

def number_of_files_to_show
  [total, per_page, target_files.length, page * per_page].min
end

#original_directiveObject



135
136
137
138
# File 'lib/yuzu/filters/catalog.rb', line 135

def original_directive
  # TODO build a regex forgiving of whitespace
  "INSERTCATALOG(#{@original_match_string})"
end

#partialObject



188
189
190
# File 'lib/yuzu/filters/catalog.rb', line 188

def partial
  Yuzu::Core::HamlTemplate.new(template)
end

#renderObject



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/yuzu/filters/catalog.rb', line 151

def render
  if well_formed?
    rows = []
    num_rows_this_page.times do |row|
      rows.push(render_nth_row(row))
    end
    rows.join("\n")
  else
    Html::Comment.new << "Error in processing the catalog. Got #{formatted_directive}"
  end
end

#render_nth_row(row) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/yuzu/filters/catalog.rb', line 163

def render_nth_row(row)
  start = row * per_row
  stop = (row + 1) * per_row
  files_this_row = files_to_render_this_page[start...stop]
  if files_this_row.empty?
    Html::Div.new(:class => 'row') << Html::Comment.new << "No files in this row."
  else
    render_row(files_this_row)
  end
end

#render_post(file) ⇒ Object



179
180
181
# File 'lib/yuzu/filters/catalog.rb', line 179

def render_post(file)
  partial.render(file, {:klass => css_class})
end

#render_row(files) ⇒ Object



174
175
176
177
# File 'lib/yuzu/filters/catalog.rb', line 174

def render_row(files)
  rendered_files = files.collect {|file| render_post(file)}.join("\n")
  Html::Div.new(:class => 'row') << rendered_files
end

#should_paginate?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/yuzu/filters/catalog.rb', line 122

def should_paginate?
  num_total_files > number_of_files_to_show
end

#target_filesObject



208
209
210
# File 'lib/yuzu/filters/catalog.rb', line 208

def target_files
  @target_files ||= get_target_files
end

#target_folderObject



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/yuzu/filters/catalog.rb', line 196

def target_folder
  case
  when (not well_formed?)
    nil
  when @kwds[:path] == ""
    @siteroot
  else
    search_path = Path.new(@kwds[:path])
    @siteroot.find_file_by_path(search_path)
  end
end

#well_formed?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/yuzu/filters/catalog.rb', line 192

def well_formed?
  @kwds.has_key?(:path)
end