Module: CSVifier::CSVifierClassMethods

Defined in:
lib/datafy/CSVifier.rb

Overview

class << self

Instance Method Summary collapse

Instance Method Details

#attr_fields(fields) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datafy/CSVifier.rb', line 46

def attr_fields fields
  logger.debug "#{self.class}::#{__method__}"
  logger.debug "fields: #{fields}"
  return if fields.nil?
  props = properties
  fields.each do |name,hash|
      prop        = hash[:property]
      props[prop] = name
      self.instance_eval do
          attr_accessor hash[:property].to_s
      end
  end
end

#baseFileNameObject



173
174
175
176
# File 'lib/datafy/CSVifier.rb', line 173

def baseFileName
  logger.debug "#{self.class}::#{__method__}"
  "#{self}".strip
end

#closeCSVFilesObject



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/datafy/CSVifier.rb', line 226

def closeCSVFiles
  unless @csv_files.nil?
      @csv_files.each do |type,file|
          begin
              file.close
          rescue => e
              logger.warn "Closing CSV file #{type} | #{file} failed"
          end
      end
  end
  @csv_files = nil
end

#csv_file(type = :data) ⇒ Object



182
183
184
# File 'lib/datafy/CSVifier.rb', line 182

def csv_file type=:data
  csv_files[type]
end

#csv_filesObject



178
179
180
# File 'lib/datafy/CSVifier.rb', line 178

def csv_files
  @csv_files ||= loadCSVFiles
end

#data_fieldsObject



26
27
28
# File 'lib/datafy/CSVifier.rb', line 26

def data_fields
    @data_fields ||= register_fields :data
end

#details_fieldsObject



34
35
36
# File 'lib/datafy/CSVifier.rb', line 34

def details_fields
  @details_fields ||= register_fields :details
end

#details_fields=(fields) ⇒ Object



38
39
40
# File 'lib/datafy/CSVifier.rb', line 38

def details_fields= fields
    @details_fields = fields
end

#getCSVFiles(label = nil) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/datafy/CSVifier.rb', line 142

def getCSVFiles label=nil
  logger.debug "#{self}::#{__method__}"
  fqname = FileProvider.getDataFileName("")
  logger.debug fqname
  puts fqname
  raise HarvestError.new "ERROR: method '#{self}::#{__method__}' not implemented source: CSVifier"
end

#loadCSVFiles(type = :data) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/datafy/CSVifier.rb', line 186

def loadCSVFiles type=:data
  logger.debug "#{self.class}::#{__method__} type:'#{type}'"
  name_data    = FileProvider.getDataFileName("#{baseFileName} Data.csv")
  name_details = FileProvider.getDataFileName("#{baseFileName} Details.csv")
  logger.debug "base_name   : #{baseFileName} "
  logger.debug "name_data   : #{name_data}"
  logger.debug "name_details: #{name_details}"
  unless @csv_files.nil?
      @csv_files.each do |type,file|
          begin
              file.close
          rescue => e
              logger.error "Closing CSV file #{type} | #{file} failed"
          end
      end
  end
  @csv_files = {}
  @csv_files[:data]    = prep_csv_file(name_data,    data_fields.keys)
  @csv_files[:details] = prep_csv_file(name_details, details_fields.keys)
  data_msg = "CSV file with data as Fields     : #{name_data}"
  flds_msg = "CSV file with data as Keys/Values: #{name_details}"
  logger.debug data_msg
  logger.debug flds_msg
  return @csv_files
end

#logFileNameObject



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

def logFileName
    @logFileName ||= "#{FileProvider.logDir}/#{self}.log"
end

#loggerObject



18
19
20
# File 'lib/datafy/CSVifier.rb', line 18

def logger
    @logger ||= Logger.new(logFileName,5,1024000)
end

#persistWhenObject



239
240
241
# File 'lib/datafy/CSVifier.rb', line 239

def persistWhen
    @persistWhen ||= DateTime.now.strftime('%l:%M:%S%P %b %-d, %Y ').strip
end

#persistWhen=(persist_when = now) ⇒ Object



242
243
244
# File 'lib/datafy/CSVifier.rb', line 242

def persistWhen= persist_when=now
    @persistWhen = persist_when
end

#prep_csv_file(name, fields) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/datafy/CSVifier.rb', line 149

def prep_csv_file name, fields
  logger.debug "#{self.class}::#{__method__} name:'#{name}' fields:#{fields}"
  file = nil
  if File.file?(name)
      logger.debug 'Data file already exists'
      FileProvider.backupFile(name)
      fileFields = CSV.open(name, &:readline)
      if fields == fileFields
          logger.debug 'headers match'
          file  = CSV.open(name,'a', :headers => true)
      else
          logger.debug 'headers DO NOT match'
          file  = CSV.open(name,'w')
          file << fields
      end
  else
      logger.debug 'Data file DOES NOT already exist'
      puts 'Data file DOES NOT already exist'
      file  = CSV.open(name,'w')
      file << fields
  end
  return file
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/datafy/CSVifier.rb', line 100

def print_fields type=:data
  logger.debug "#{self}::#{__method__}"
  label = "#{self} #{type.capitalize} Fields"
  puts label
  puts '-' * label.length
  fields    = type.eql?(:details) ? details_fields : data_fields
  maxKeyLen = fields.keys.max_by(&:length).length
  maxSynLen = fields.values.map{|x| x[:property]}.max_by(&:length).length + 2
  comments  = fields.values.map{|x| x[:comment]}.compact
  maxComLen = comments.empty? ? "Comment".length : comments.max_by(&:length).length
  logger.debug "  class: #{self}\n  #{type} Fields [CSV]"
  logger.debug "  %-#{maxKeyLen}s  ->  %-#{maxSynLen}s   %-#{maxComLen}s" % ['Field Name', 'Symbol', "Comment"]
  logger.debug "  %-#{maxKeyLen}s      %-#{maxSynLen}s   %-#{maxComLen}s" % ['-' * maxKeyLen, '-' * maxSynLen, '-' * maxComLen ]
  # puts '-' * (maxKeyLen +3)
  fields.each do |f,v|
      puts "  %-#{maxKeyLen}s  ->  %-#{maxSynLen}s   %s" % [f,":#{v[:property]}",v[:comment]]
  end
end

#printDataFieldsObject



134
135
136
# File 'lib/datafy/CSVifier.rb', line 134

def printDataFields
  print_fields :data
end

#printDataFiles(show_fields = true) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/datafy/CSVifier.rb', line 212

def printDataFiles show_fields=true
    logger.debug "#{self.class}::#{__method__} show_fields:#{show_fields}"
    puts "CSV Files\n---------"
    type_len = show_fields ? 4 : 7
    csv_files.each do |type,file|
        puts "%#{type_len}s file -> %-s" % [type.to_s.capitalize,file.path]
        if show_fields
            printFields type
            puts ''
        end
    end
    puts "---------"
end

#printDetailsFieldsObject



138
139
140
# File 'lib/datafy/CSVifier.rb', line 138

def printDetailsFields
  print_fields :details
end

#printFields(*args) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/datafy/CSVifier.rb', line 119

def printFields  *args
  logger.debug "#{self}::#{__method__} -c- args: #{args} empty? #{args.empty?}"
  if args.empty?
      print_fields :data
      print_fields :details
      return
  end
  args.each do |type|
      case type.to_s.downcase
          when 'data'    then print_fields :data
          when 'details' then print_fields :details
      end
  end
end

#printPropertiesObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/datafy/CSVifier.rb', line 87

def printProperties
  logger.debug "#{self}::#{__method__}"
  if properties.empty?
      puts "no properties to print"
      return
  end
  maxLen = properties.keys.max_by(&:length).length
  maxLen += 2
  properties.each do |f,v|
      puts "  %-#{maxLen}s -> '%s' " % [f.inspect,v]
  end
end

#prop_accessor(*prop_groups) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/datafy/CSVifier.rb', line 64

def prop_accessor *prop_groups
    logger.debug "#{self}::#{__method__}"
    logger.debug *prop_groups.inspect
    prop_groups.each do |props|
        logger.debug props
        attr_fields props
    end
    register_fields
end

#propertiesObject



60
61
62
# File 'lib/datafy/CSVifier.rb', line 60

def properties
  @properties ||= Hash.new
end

#register_fields(type = :data) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/datafy/CSVifier.rb', line 74

def register_fields type=:data
    logger.debug "#{self}::#{__method__}"
    @data_fields    = self.const_defined?(:KEY_FIELDS) ? self::KEY_FIELDS.clone : {}
    @details_fields = @data_fields.clone
    # --
    data_props      = self.const_defined?(:PROPERTY_FIELDS) ? self::PROPERTY_FIELDS : {}
    @data_fields.merge!(    data_props,                            BaseDatafier::BASE_HARVEST_DATA_FIELDS )
    @details_fields.merge!( BaseDatafier::DETAILS_PROPERTY_FIELDS, BaseDatafier::BASE_HARVEST_DATA_FIELDS )
    attr_fields @data_fields
    attr_fields @details_fields
    return type.eql?(:data) ? @data_fields : @details_fields
end

#set_data_fields(fields) ⇒ Object



30
31
32
# File 'lib/datafy/CSVifier.rb', line 30

def set_data_fields fields
    @data_fields = fields
end

#set_details_fields(fields) ⇒ Object



42
43
44
# File 'lib/datafy/CSVifier.rb', line 42

def set_details_fields fields
    @details_fields = fields
end