lib/wp2middleman/post_collection.rb in wp2middleman-0.0.1 vs lib/wp2middleman/post_collection.rb in wp2middleman-0.0.2
- old
+ new
@@ -1,15 +1,45 @@
require 'nokogiri'
module WP2Middleman
class PostCollection
- def initialize(wp_xml_export_file)
- @xml = Nokogiri::XML(File.open("#{Dir.pwd}/#{wp_xml_export_file}"))
+ include Enumerable
+
+ def self.from_file(wp_xml_export_file)
+ xml = Nokogiri::XML(File.open("#{Dir.pwd}/#{wp_xml_export_file}"))
+ new xml.css('item').collect { |raw_wp_post| WP2Middleman::Post.new(raw_wp_post) }
end
- def posts
- @xml.css('item')
- .map { |post| WP2Middleman::Post.new(post) }
- .reject { |post| post.type == 'attachment' }
+ def initialize(posts=[])
+ @posts = posts
end
+
+ def each(&block)
+ posts.each &block
+ end
+
+ def [] key
+ posts[key]
+ end
+
+ def empty?
+ posts.empty?
+ end
+
+ def without_attachments
+ self.class.new(reject(&:attachment?))
+ end
+
+ def only_valid
+ self.class.new(select(&:valid?))
+ end
+
+ def to_middleman(body_to_markdown: false, include_fields: [])
+ middleman_posts = collect { |p| WP2Middleman::MiddlemanPost.new(p, body_to_markdown: body_to_markdown, include_fields: include_fields) }
+ self.class.new(middleman_posts)
+ end
+
+ private
+
+ attr_reader :posts
end
end