Module: Elibri::ONIX::Generator::ClassMethods
- Defined in:
- lib/elibri_onix_generator.rb
Instance Method Summary collapse
-
#onix_sections_docs ⇒ Object
Zwróć dokumentację dla metod sekcji ONIX z bieżącego pliku.
- #render_header(builder, options = {}, &block) ⇒ Object
- #tag(builder, short_tags, tag_id, *args, &block) ⇒ Object
Instance Method Details
#onix_sections_docs ⇒ Object
Zwróć dokumentację dla metod sekcji ONIX z bieżącego pliku. Dzięki temu dokumentacja ONIX jest generowana w locie i nie rozjedzie się z kodem. Dokumentacje można wygenerować tylko dla metod ‘def export_XXX!’.
Przykład dokumentowania:
# @hidden_tags RecordReference NotificationType
# @title Wymiary produktu
# eLibri zachowuje tylko <strong>wysokość, szerokość, grubość oraz masę produktu.</strong> Dla produktów typu mapa, eksportujemy również jej skalę
# w tagu <MapScale>
def export_measurement!(product)
[...]
end
okre
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/elibri_onix_generator.rb', line 79 def onix_sections_docs # Wczytaj bieżący plik code_lines = File.readlines(__FILE__) section_docs = Array.new # Dla każdej metody o sygnaturze 'def export_NAZWA_SEKCJI!' czytaj otaczające linie komentarzy: instance_methods.grep(/export_/).each_with_index do |method_name, section_idx| section_docs[section_idx] ||= Hash.new section_docs[section_idx][:section_name] = method_name[/export_(.*)!/, 1] # "export_supplier_identifier!" => "supplier_identifier" section_docs[section_idx][:hidden_tags] = Array.new section_docs[section_idx][:auto_render] = true # Znajdź numer linii z definicją metody: method_definition_line_idx = code_lines.find_index {|line| line.match(/^\s*def #{method_name}/)} # Cofamy się w gorę kodu, aż do początku pliku w poszukiwaniu linii zawierającej tag @title w komentarzu. (method_definition_line_idx-1).downto(0).each do |line_idx| # Oczyść linię kodu ze znaku komentarza i zbędnych spacji: line = code_lines[line_idx].strip.sub(/^\s*#\s*/, '#') raise "Nieprawidłowy format dokumentacji dla metody #{method_name}" if line.match(/^end$/) if md = line.match(/^\s*$/) break elsif md = line.match(/@render (.*)$/) section_docs[section_idx][:auto_render] = false render_call = "\n= render :partial => 'example', :locals => { :method => '#{md[1].strip}' }\n" section_docs[section_idx][:description] = [render_call, section_docs[section_idx][:description]].join("\n") elsif md = line.match(/@title (.*)$/) section_docs[section_idx][:section_title] = md[1].gsub(/^#/, '') elsif md = line.match(/@hidden_tags (.*)$/) section_docs[section_idx][:hidden_tags] = md[1].gsub(/^#/, '').scan(/\w+/) elsif line == '#' section_docs[section_idx][:description] = ["\n%br/\n", section_docs[section_idx][:description]].join("\n") else section_docs[section_idx][:description] = [line.gsub(/^#/, ''), section_docs[section_idx][:description]].join("\n") end end end # Zwróć posortowane według kolejności sekcji: section_docs.find_all { |section_hash| DOC_ORDER.index(section_hash[:section_name]) }.sort_by {|section_hash| DOC_ORDER.index(section_hash[:section_name]) } end |
#render_header(builder, options = {}, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/elibri_onix_generator.rb', line 37 def render_header(builder, = {}, &block) .reverse_merge!({:short_tags => false, :elibri_onix_dialect => '3.0.1'}) = [:short_tags] builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8" = {:release => "3.0", :xmlns => "http://ns.editeur.org/onix/3.0/reference", "xmlns:elibri" => "http://elibri.com.pl/ns/extensions"} .delete('xmlns:elibri') if [:pure_onix] builder.ONIXMessage do unless [:pure_onix] builder.elibri :Dialect, [:elibri_onix_dialect] # potrzebne, aby parser wiedział jak interpretować niektóre tagi end tag(builder, , :Header) do tag(builder, , :Sender) do tag(builder, , :SenderName, "Elibri.com.pl") tag(builder, , :ContactName, "Tomasz Meka") tag(builder, , :EmailAddress, "[email protected]") end tag(builder, , :SentDateTime, Date.today.strftime("%Y%m%d")) end yield(builder) if block_given? end end |
#tag(builder, short_tags, tag_id, *args, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/elibri_onix_generator.rb', line 23 def tag(builder, , tag_id, *args, &block) if if SHORT_TAGS[tag_id] tag_id = SHORT_TAGS[tag_id] elsif tag_id.starts_with?("elibri") tag_id else raise "Unknow short tag for: #{tag_id}" end end builder.__send__(tag_id, *args, &block) end |