Module: Elibri::ONIX::Generator::ClassMethods

Defined in:
lib/elibri_onix_generator.rb

Instance Method Summary collapse

Instance Method Details

#onix_sections_docsObject

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 &lt;MapScale&gt;
 def export_measurement!(product)
   [...]
 end

@hidden_tags okre


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
# File 'lib/elibri_onix_generator.rb', line 64

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/elibri_onix_generator.rb', line 27

def render_header(builder, options = {}, &block)
  builder.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
  message_attributes = {:release => "3.0", :xmlns => "http://ns.editeur.org/onix/3.0/reference", "xmlns:elibri" => "http://elibri.com.pl/ns/extensions"}
  message_attributes.delete('xmlns:elibri') if options[:pure_onix] || options[:elibri_onix_dialect] == "3.0.2"

  builder.ONIXMessage message_attributes do
    unless options[:pure_onix] || options[:elibri_onix_dialect] == "3.0.2"
      builder.elibri :Dialect, options[:elibri_onix_dialect] || DEFAULT_DIALECT # potrzebne, aby parser wiedział jak interpretować niektóre tagi
    end
    tag(builder, :Header) do
      tag(builder, :Sender) do
        tag(builder, :SenderName, options[:sender_name] || "Elibri.com.pl")
      end
      tag(builder, :SentDateTime, Date.today.strftime("%Y%m%d"))
    end
    yield(builder) if block_given?
  end
end

#tag(builder, tag_id, *args, &block) ⇒ Object



22
23
24
# File 'lib/elibri_onix_generator.rb', line 22

def tag(builder, tag_id, *args, &block)
  builder.__send__(tag_id, *args, &block)
end