it
February 28, 2023
Принципы написание сервисов на Ruby
class PrintService attr_accessor :output_path, :template_path, :tracker_name def initialize @template_path = 'template.docx' @tracker_name = 'Комплект' super end def call render_doc temp_file end private def render_doc template.render_to_file(temp_file, context) end def temp_file @temp_file ||= Tempfile.new('output.docx') end def context { items: mapped_issues } end def mapped_issues @mapped_issues ||= issues.each_with_index.map{ |v, i| row.new(i, v.subject, v.description, v.created_on.strftime('%d.%m.%Y')) } end def row @row ||= Struct.new(:index, :subject, :description, :date) end def issues @issues ||= Issue.where(tracker: tracker) end def tracker @tracker ||= Tracker.find_by_name(tracker_name) end def template @template ||= Sablon.template(File.expand_path(template_path)) end end
Если пользоваться всеми возможностями язык, то получается хорошо.