module Flextures::Dumper

defined data dump methods

Constants

PARENT

base configurations



TRANSLATER

data translaters

Public Class Methods

create_filter(attr_types, format, type) click to toggle source

filter is translate value safe YAML or CSV string @params [Class] klass ActiveRecord class @params [String] table_name table name @params [Hash] options options @params [Symbol] type format type (:yml or :csv) @return [Proc] filter function

# File lib/flextures/flextures_dumper.rb, line 195
def self.create_filter(attr_types, format, type)
  filter = DumpFilter[format[:table].to_s.to_sym] || {}
  ->(row) {
    attr_types.map do |h|
      v = filter[h[:name].to_sym] ? filter[h[:name].to_sym].call(row[h[:name]]) : trans(row[h[:name]], h[:type], type)
      [h[:name],v]
    end
  }
end
csv(format) click to toggle source

data dump to csv format @params [Hash] format file format data @params [Hash] options dump format options

# File lib/flextures/flextures_dumper.rb, line 208
def self.csv(format)
  klass = PARENT.create_model(format[:table])
  attr_types = self.dump_attributes klass, format
  filter = self.create_filter attr_types, format, :csv
  self.dump_csv klass, attr_types, filter, format
end
dump_attributes(klass, options) click to toggle source

dump attributes data @params klass dump table Model @params [Hash] options dump options @return [Array] columns format information

# File lib/flextures/flextures_dumper.rb, line 176
def self.dump_attributes(klass, options)
  columns = klass.columns.map do |column|
    type_name = klass.defined_enums[column.name.to_s] ? :enum : column.type
    { name: column.name, type: type_name }
  end
  # option[:minus] colum is delete columns
  columns.reject! { |column| options[:minus].include?(column[:name]) } if options[:minus]
  # option[:plus] colum is new columns
  # values is all nil
  plus = options[:plus].to_a.map { |colname| { name: colname, type: :null } }
  columns + plus
end
dump_csv(klass, attr_types, values_filter, format) click to toggle source

dump csv format data

# File lib/flextures/flextures_dumper.rb, line 216
def self.dump_csv(klass, attr_types, values_filter, format)
  # TODO: 拡張子は指定してもしなくても良いようにする
  file_name = format[:file] || format[:table]
  dir_name = File.join(Flextures::Configuration.dump_directory, format[:dir].to_s)
  FileUtils.mkdir_p(dir_name)
  outfile = File.join(dir_name, "#{file_name}.csv")
  CSV.open(outfile,'w') do |csv|
    # dump column names
    csv<< attr_types.map { |h| h[:name].to_s }
    # dump column datas
    klass.all.each do |row|
      csv<< values_filter.call(row).map(&:last)
    end
  end
  outfile
end
dump_yml(klass, values_filter, format) click to toggle source

dump yml format data

# File lib/flextures/flextures_dumper.rb, line 244
def self.dump_yml(klass, values_filter, format)
  # TODO: 拡張子は指定してもしなくても良いようにする
  table_name = format[:table]
  file_name = format[:file] || format[:table]
  dir_name = File.join(Flextures::Configuration.dump_directory, format[:dir].to_s)
  FileUtils.mkdir_p(dir_name)
  outfile = File.join(dir_name, "#{file_name}.yml")
  File.open(outfile,"w") do |f|
    klass.all.each.with_index do |row,idx|
      values = values_filter.call(row).map { |k,v| "  #{k}: #{v}\n" }.join
      f<< "#{table_name}_#{idx}:\n" + values
    end
  end
  outfile
end
trans(v, type, format) click to toggle source

translate data @params [Object] value @params [Symbol] type datatype @params [Symbol] format data type (:yml or :csv) @return translated value

# File lib/flextures/flextures_dumper.rb, line 166
def self.trans(v, type, format)
  translater = TRANSLATER[type]
  return translater.call(v, format) if translater
  v
end
translate_creater(val, rules) click to toggle source

create data translater

# File lib/flextures/flextures_dumper.rb, line 25
def self.translate_creater(val, rules)
  rule_map = {
    nullstr: proc { |d|
      return "null" if d.nil?
      d
    },
    null: proc { |d|
      return nil if d.nil?
      d
    },
    blank2null: proc { |d|
      return "null" if d==""
      d
    },
    blankstr: proc { |d|
      return '""' if d==""
      d
    },
    false2nullstr: proc { |d|
      return "null" if d==false
      d
    },
    blank2num: proc { |d|
      return 0 if d==""
      d
    },
    null2blankstr: proc { |d|
      return "" if d.nil?
      d
    },
    bool2num: proc { |d|
      return 0 if d==false
      return 1 if d==true
      d
    },
    ymlspecialstr: proc { |s|
      if s.kind_of?(String)
        s = s.gsub(/\t/,"  ") if s["\t"]
        s = s.sub(/ +/, "")   if s[0]==' '
        is_nl = false
        is_nl |= s["\n"]
        is_nl |= ["[","]","{","}","|","#","@","~","!","'","$","&","^","<",">","?","-","+","=",";",":",".",",","*","`","(",")"].member?(s[0])
        s = s.gsub(/\r\n/,"\n").gsub(/\r/,"\n") # 改行方法統一
        s = "|-\n    " + s.gsub(/\n/,"\n    ") if is_nl
      end
      s
    },
    ymlnulltime: proc { |d|
      return "null" if d.nil? or d=="" or d==false
      d
    },
  }
  procs = rules.reduce(proc{ |d| d }) { |sum,i| sum * (rule_map[i] || i) }
  procs.call(val)
end
yml(format) click to toggle source

data dump to yaml format @params [Hash] format file format data @params [Hash] options dump format options

# File lib/flextures/flextures_dumper.rb, line 236
def self.yml(format)
  klass = PARENT::create_model(format[:table])
  attr_types = self.dump_attributes klass, format
  filter = self.create_filter attr_types, format, :yml
  self.dump_yml(klass, filter, format)
end