Class: Twb::Util::FieldDomainLoader

Inherits:
Object
  • Object
show all
Includes:
TabTool
Defined in:
lib/twb/util/fielddomainloader.rb

Constant Summary collapse

@@xmlLocation =
'./ttdoc'

Instance Attribute Summary collapse

Attributes included from TabTool

#alerts, #docDir, #docfiles, #funcdoc, #id, #licensed, #logfilename, #logger, #loglevel, #metrics, #properties, #ttdocdir, #type, #uuid

Instance Method Summary collapse

Methods included from TabTool

#addDocFile, #alert, #closeDocFiles, #config, #docFile, #docFileMaxNameLen, #docfilesdoc, #docfilesdocto_s, #emit, #emitCSV, #finis, #hasConfig, #init, #initDocDir, #initLogger, #license=, #licensed?, #loadConfig

Constructor Details

#initializeFieldDomainLoader

Returns a new instance of FieldDomainLoader.



30
31
32
33
# File 'lib/twb/util/fielddomainloader.rb', line 30

def initialize
  init
  reset
end

Instance Attribute Details

#csvOptionObject

Returns the value of attribute csvOption.



28
29
30
# File 'lib/twb/util/fielddomainloader.rb', line 28

def csvOption
  @csvOption
end

#datasourceObject

Returns the value of attribute datasource.



28
29
30
# File 'lib/twb/util/fielddomainloader.rb', line 28

def datasource
  @datasource
end

#domainsObject (readonly)

Returns the value of attribute domains.



27
28
29
# File 'lib/twb/util/fielddomainloader.rb', line 27

def domains
  @domains
end

#workboookObject

Returns the value of attribute workboook.



28
29
30
# File 'lib/twb/util/fielddomainloader.rb', line 28

def workboook
  @workboook
end

#xmllocationObject

Returns the value of attribute xmllocation.



28
29
30
# File 'lib/twb/util/fielddomainloader.rb', line 28

def xmllocation
  @xmllocation
end

Instance Method Details

#initCSV(opt) ⇒ Object



42
43
44
45
46
47
# File 'lib/twb/util/fielddomainloader.rb', line 42

def initCSV opt
  @csvFile     = CSV.open(@xmllocation + '/FieldDomains.csv', opt )
  if 'w'.eql? opt
    @csvFile << ['Workbook', 'Data Source', 'Field', 'Value']
  end
end

#loadObject



49
50
51
52
53
# File 'lib/twb/util/fielddomainloader.rb', line 49

def load
  path = @xmllocation + '/*.xlsx'
  Dir.glob(path) do |fileName|
  end
end

#loadDataSource(ds) ⇒ Object



67
68
69
70
# File 'lib/twb/util/fielddomainloader.rb', line 67

def loadDataSource ds
  @datasource = ds.uiname
  fieldDomains = loadxlsx(@xmllocation + '/' + @datasource + '.xlsx')
end

#loadWorkbook(twb) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/twb/util/fielddomainloader.rb', line 55

def loadWorkbook twb
  @workbook = twb.name
  dsFieldDomains = {}
  dss = twb.datasources
  dsFieldDomains = {}
  dss.each do |ds|
    fieldDomains = loadDataSource ds
    dsFieldDomains[ds.uiname] = fieldDomains
  end
  return dsFieldDomains
end

#loadxlsx(fileName) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/twb/util/fielddomainloader.rb', line 72

def loadxlsx fileName
    fieldDomains = {}
    # TODO - fix this - when  Creek::Book is in place, the twb gem cannot be loaded in Ruby on Rails, as of Jan 26, 2019 
    #      - commenting out to sidestep problem giving up functionality for expedience in gtting online
    # if File.file?(fileName)
    #   xlsx = Creek::Book.new fileName
    #   sheets = xlsx.sheets
    #   sheets.each do |sheet|
    #       rows = sheet.rows.to_a
    #       if rows.count > 1
    #           fieldValues = parseRows(rows)
    #           unless fieldValues.empty? ||  fieldValues.values.first.empty?
    #               fieldDomains[fieldValues.keys.first] = fieldValues.values.first
    #           end
    #       end
    #   end
    # else
    #   alert  "#### ALERT #### Twb:'#{@workbook}' DS: '#{@datasource}' DomRef: #{fileName}' file does not exist."
    # end
    # return fieldDomains
end

#parseRows(rows) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/twb/util/fielddomainloader.rb', line 94

def parseRows rows
    fieldValues = {}
    unless rows.empty?
        firstRow  = rows[0].to_a[0]
        fieldName = firstRow[1].to_s
        fieldValues[fieldName] = SortedSet.new
        values = rows[1..-1]
        values.each do |row|
            value = row.to_a[0][1].to_s
            fieldValues[fieldName] << value
        end
    end
    return fieldValues
end

#resetObject



35
36
37
38
39
40
# File 'lib/twb/util/fielddomainloader.rb', line 35

def reset
  @workbok     = nil
  @xmllocation = @@xmlLocation
  @csvOption   = 'w'
  initCSV @csvOption
end