Class: FMPVC::DDR

Inherits:
Object
  • Object
show all
Defined in:
lib/fmpvc/DDR.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(summary_directory, summary_filename = FMPVC.configuration.ddr_filename) ⇒ DDR

Returns a new instance of DDR.

Raises:

  • (RuntimeError)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fmpvc/DDR.rb', line 13

def initialize(summary_directory, summary_filename = FMPVC.configuration.ddr_filename)
  
  @summary_filename      = summary_filename
  @base_dir_ddr          = File.expand_path(summary_directory)    ; raise(RuntimeError, "Error: can't find the DDR directory, #{@base_dir_ddr}")            unless File.readable?(@base_dir_ddr)
  summary_file_path      = "#{@base_dir_ddr}/#{summary_filename}" ; raise(RuntimeError, "Error: can't find the DDR Summary.xml file, #{summary_file_path}") unless File.readable?(summary_file_path)
  @base_dir_text_path    = @base_dir_ddr.gsub(%r{#{FMPVC.configuration.ddr_dirname}}, FMPVC.configuration.text_dirname)
  @summary_text_path     = "#{@base_dir_text_path}/#{summary_filename.gsub(%r{\.xml}, '.txt')}"

  @content               = IO.read(summary_file_path)
  @reports               = Array.new
  self.parse
  
end

Instance Attribute Details

#base_dir_ddrObject (readonly)

Returns the value of attribute base_dir_ddr.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def base_dir_ddr
  @base_dir_ddr
end

#base_dir_text_pathObject (readonly)

Returns the value of attribute base_dir_text_path.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def base_dir_text_path
  @base_dir_text_path
end

#contentObject (readonly)

Returns the value of attribute content.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def content
  @content
end

#creation_dateObject (readonly)

Returns the value of attribute creation_date.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def creation_date
  @creation_date
end

#creation_timeObject (readonly)

Returns the value of attribute creation_time.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def creation_time
  @creation_time
end

#fmp_filesObject (readonly)

Returns the value of attribute fmp_files.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def fmp_files
  @fmp_files
end

#fmpa_versionObject (readonly)

Returns the value of attribute fmpa_version.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def fmpa_version
  @fmpa_version
end

#reportsObject (readonly)

Returns the value of attribute reports.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def reports
  @reports
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def type
  @type
end

#xml_filesObject (readonly)

Returns the value of attribute xml_files.



11
12
13
# File 'lib/fmpvc/DDR.rb', line 11

def xml_files
  @xml_files
end

Instance Method Details

#element2yaml(xml_element) ⇒ Object



98
99
100
101
102
103
# File 'lib/fmpvc/DDR.rb', line 98

def element2yaml(xml_element)
  return '' unless FMPVC.configuration.yaml
element_xml							= xml_element.to_xml({:encoding => 'UTF-8'}) # REMEMBER: the encoding
element_hash						= Hash.from_xml(element_xml)
element_yaml						= element_hash.to_yaml
end

#parseObject

Raises:

  • (RuntimeError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fmpvc/DDR.rb', line 27

def parse
  post_notification('Summary', 'Parsing')
  summary          = Nokogiri::XML(@content)
  attrs            = summary.xpath("//FMPReport").first # "there can be only one"
  @type            = attrs["type"] ; raise RuntimeError, "Incorrect file type: not a DDR Summary.xml file!" unless @type == "Summary"
  @fmpa_version    = attrs["version"]
  @creation_time   = attrs["creationTime"]
  @creation_date   = attrs["creationDate"]
  @summary_yaml    = element2yaml(summary)
  
  fmp_reports      = summary.xpath("//FMPReport/File")
  @reports         = fmp_reports.map do |a_report|
    {              
      :name        => a_report['name'], 
      :link        => a_report['link'],
      :path        => a_report['path'],
      :attrs       => Hash[ a_report.xpath("./*").map {|v| [v.name, v['count']]} ]
    }
  end      
  @xml_files       = fmp_reports.collect {|node| node['link']}
  @fmp_files       = fmp_reports.collect {|node| node['name']}
end

#post_notification(object, verb = 'Updating') ⇒ Object



58
59
60
# File 'lib/fmpvc/DDR.rb', line 58

def post_notification(object, verb = 'Updating')
  $stdout.puts [verb, object].join(" ") unless FMPVC.configuration.quiet
end

#process_reportsObject



50
51
52
53
54
55
56
# File 'lib/fmpvc/DDR.rb', line 50

def process_reports
  @reports.each do |r| 
    # $stdout.puts
    post_notification(r[:link].gsub(%r{\./+},''), 'Processing')
    r[:report] = FMPReport.new(r[:link], self) 
  end
end

#stringer(n, str = " ") ⇒ Object



62
63
64
# File 'lib/fmpvc/DDR.rb', line 62

def stringer(n, str = " ")
  n.times.map {str}.join
end

#write_reportsObject



93
94
95
96
# File 'lib/fmpvc/DDR.rb', line 93

def write_reports
  self.process_reports if @reports.first[:report].nil?
  @reports.each { |r| r[:report].write_all_objects }
end

#write_summaryObject



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
# File 'lib/fmpvc/DDR.rb', line 66

def write_summary
  post_notification('Summary file', 'Writing')
  FileUtils.mkdir(@base_dir_text_path) unless File.directory?(@base_dir_text_path)
  summary_format      = "%25s  %-512s\n"
  # report_params       = ["BaseTables", "Tables", "Relationships", "Privileges", "ExtendedPrivileges", "FileAccess", "Layouts", "Scripts", "ValueLists", "CustomFunctions", "FileReferences", "CustomMenuSets", "CustomMenus"]
  report_params       = @reports.first[:attrs].keys # better to get the keys dynamically than a fixed list
  params_label        = report_params.map {|p| "%-2s" + stringer(p.length) }.join()
  report_format       = "%25s " + params_label
  header              = stringer(25 - "Report".length) + "Report" + "   " + report_params.join('  ')
  separator           = header.gsub(%r{\w}, '-')
  File.open(@summary_text_path, 'w') do |f|
    f.write format(summary_format, "Summary file:",           @summary_filename)
    f.write format(summary_format, "Summary path:",           @base_dir_ddr)
    f.write format(summary_format, "FileMaker Pro version:",  @fmpa_version)
    f.write format(summary_format, "Creation Date:",          @creation_date)
    f.write format(summary_format, "Creation Time:",          @creation_time)
    f.puts
    f.puts header
    f.puts separator
    @reports.each do |r|
      f.puts format(report_format, r[:name] + ' ', *report_params.map { |p| r[:attrs][p] }) 
    end
    f.puts
    f.puts @summary_yaml
  end
end