Skip to content
This repository has been archived by the owner on Apr 7, 2020. It is now read-only.

Commit

Permalink
Lazy-load category members
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas E. Rabenau committed Apr 24, 2012
1 parent 8b354a3 commit 9d671e0
Show file tree
Hide file tree
Showing 3 changed files with 1,986 additions and 30 deletions.
45 changes: 23 additions & 22 deletions lib/riki/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,20 @@ def find_by_title(titles)
result = []
Array(titles).each do |title|
cat = Category.new(title)

# Group results by type so we can call the finders coarse-grained
retrieve_categories(title).find('m:categorymembers/m:cm').group_by{|cm| cm['type']}.each do |type, members|
# TODO Store proxies instead of full-fledged objects in order to keep them lighweight
cat.members.concat(members.map{|cm| cm['title']})
end

result << cat
cat.members = retrieve_categories(title).find('m:categorymembers/m:cm')
result << cat
end

result
end

private

CLASS_MAP = {
'subcat' => Category
}

def class_for(type)
CLASS_MAP[type] || Riki.const_get(type.classify)
end

def retrieve_categories(title)

def retrieve_categories(title)
parms = {'action' => 'query',
'list' => 'categorymembers',
'cmtitle' => title["Category:"] ? title : "Category:#{title}",
'cmprop' => ['title', 'type'].join('|'),
'cmprop' => ['title', 'type', 'timestamp'].join('|'),
'cmlimit' => 100,
}
api_request(parms).first.find_first('/m:api/m:query')
Expand All @@ -53,8 +39,23 @@ def initialize(title)
end

def members
# TODO Resolve category and page proxies
@members
result = []

@members.group_by{|cm| cm['type']}.each do |type, members|
result.concat(class_for(type).find_by_title(members.map{|cm| cm['title']}))
end

result
end

private

CLASS_MAP = {
'subcat' => Category
}

def class_for(type)
CLASS_MAP[type] || Riki.const_get(type.classify)
end
end
end
Loading

0 comments on commit 9d671e0

Please sign in to comment.