I am new here and I hope I will get answer to my question.
I have three classes.
- Runner
- Writer
- Company
In runner class I have
writer = Writer.new(directory + datasource.downcase + ".xml")
ds = ("Sitemap::" + datasource).constantize.new(country_version, directory, country_host)
writer.fill do
ds.yield_data {|entry| writer.write_entry(entry)}
end
Yeild_data is in class Company
write_entry is in class Writer
Following is the class Company code
class Company
def initialize(country_version, directory, country_host)
@country_version = country_version
@directory = directory
@country_host = country_host
end
def yield_data
::Company.find_each(:conditions => {:country_version_id => @country_version.id}) do |company|
output = yield entry(company)
puts output
end
end
private
def entry(company)
{
:url => ActionController::Integration::Session.new.url_for(:controller => 'companies', :action => 'show', :company_name => company.name, :host => @country_host.value),
:frequency => 0.8,
:priority => 'monthly',
:lastmod => company.updated_at
}
end
end
Following is the class Writer
class Writer
include ActionController::UrlWriter
def initialize(filepath)
@filepath = RAILS_ROOT + filepath
@xml_document = Nokogiri::XML::Document.new
end
def fill
File.open(@filepath,"w") do |f|
f.write(%[<?xml version="1.0" encoding="UTF-8"?>\n])
f.write(%[<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n])
yield self
f.write(%[</urlset>])
f.close
end
end
def write_entry(entry)
node = Nokogiri::XML::Node.new("url", @xml_document)
node["loc"]= entry[:url]
node["lastmod"]= entry[:lastmod].to_s
node["changefreq"] = entry[:frequency].to_s
node["priority"] = entry[:priority].to_s
node.to_xml
#@filepath.write(node)
end
end
Kindly answer me the following questions:
- what will yeild entry(company) return (in company class)
- what will yield self return (in writer class)
- How can I write the node to xml file
just look in what scope you are. Remember,
selfis implied :we are in an instance method, and it yields
self.entry(company). So it will return the output of a call toentry(company)on the present instance ( wich seems to bedsin the first code you posted )still an instance method, so it will return
self, a.k.a. the instanciatedWriteron whichfillis called, that is yourwriterobject in the first codeas @Frederick said, you should ask about the XML in another question and keep your question concise. Please read the FAQ to know more about how to use this site. Welcome to the community !
EDIT : some help with the blocks
blocks are somewhat like anonymous functions.
whenever you do this :
just imagine you pass a function to
some_method, and that function needsblock_args. Now when you do :just imagine you call the block with
block_args=some_args. It really helps to imagine a block like a simplified method signature, with no method name but with a list of arguments. In fact, this is like doing :with the notable exception that a block is bound to the context in which it is called:
now just imagine that
yieldis a function name that stands for the block ; if the block returns something, you can use it as the return of any other function. A little example with some dummy implementation of the already existingEnumerable#mapmethod :