class FileRecord::FileUtility

Public Class Methods

new(model, options={}) click to toggle source
# File lib/file_record/file_utility.rb, line 23
def initialize(model, options={})
  @model = model
  @time = options.fetch(:time, nil)
  @directory = options.fetch(:directory, Dir.home)

  # options to allow for file creation and destruction,
  # default to false so that file path enquiries can't
  # change the directory structure
  @create = options.fetch( :create, false )
  @destroy = options.fetch( :destroy, false )
end

Public Instance Methods

clean_path(file_path) click to toggle source

remove existing file when passed destroy:true in options

# File lib/file_record/file_utility.rb, line 155
def clean_path(file_path)

  if @destroy and File.exists?(file_path)
    File.delete(file_path)
  end

  file_path
end
file_path() click to toggle source

returns full path and file for model Tempo::Model::Log on 11/12/2014 -> Users/usrname/tempo/tempo_logs/20141112.yaml Tempo::Model::Base -> Users/usrname/tempo/tempo_bases.yaml Will also create directory if not found and passed create:true in options Will destroy file if passed destroy:true in options

# File lib/file_record/file_utility.rb, line 108
def file_path

  return clean_path(File.join(log_directory_path, filename)) if @time

  dir = File.join(@directory, module_name)

  if @create and !File.exists?(dir)
    Dir.mkdir(dir, 0700)
  end

  clean_path File.join(dir, filename)
end
filename() click to toggle source

Tempo::Model::Log on 12/1/2015 -> 20151201.yaml Tempo::Model::Base -> tempo_bases.yaml

# File lib/file_record/file_utility.rb, line 62
def filename
  # return Log file name
  return "#{@model.day_id( @time )}.yaml" if @time

  sn = split_name
  file = "#{sn[0]}_#{sn[-1]}s.yaml"
end
log_directory() click to toggle source

ex. Tempo::Model::Log -> tempo_logs

# File lib/file_record/file_utility.rb, line 71
def log_directory
  sn = split_name
  "#{sn[0]}_#{sn[-1]}s"
end
log_directory_path() click to toggle source

Tempo::Model::Log -> Users/usrname/(alternate_directory/)tempo/tempo_logs/20XX Will also create the directory if not found

# File lib/file_record/file_utility.rb, line 93
def log_directory_path
  dir = File.join(log_main_directory_path, log_year_directory)

  if @create and !File.exists?(dir)
    FileUtils.mkdir_p dir
  end

  dir
end
log_main_directory_path() click to toggle source

Tempo::Model::Log -> Users/usrname/(alternate_directory/)tempo/tempo_logs' Will also create the directory if not found

# File lib/file_record/file_utility.rb, line 87
def log_main_directory_path
  dir = File.join(@directory, module_name, log_directory)
end
log_records() click to toggle source

Returns the list of log records from a log directory

# File lib/file_record/file_utility.rb, line 144
def log_records
  records = []
  return records if !File.exists?(log_main_directory_path)
  years = Pathname.new(log_main_directory_path).children.select { |c| c.directory? }
  years.each do |dir|
    records = records | Dir[dir.to_s + "/*.yaml"]
  end
  records.sort!
end
log_year_directory() click to toggle source
# File lib/file_record/file_utility.rb, line 77
def log_year_directory
  if @time.kind_of? Time
    @time.strftime("%Y")
  else
    @time[0..3]
  end
end
model_name() click to toggle source

Tempo::Model::Project -> “project”

# File lib/file_record/file_utility.rb, line 56
def model_name
  split_name[-1]
end
module_name() click to toggle source

Tempo::Model::Project -> “tempo”

# File lib/file_record/file_utility.rb, line 51
def module_name
  split_name[0]
end
move_old_records() click to toggle source
# File lib/file_record/file_utility.rb, line 129
def move_old_records
  return false if !File.exists?(log_main_directory_path)
  puts "moving files in #{log_main_directory_path}"
  Pathname.new(log_main_directory_path).children.each do |c|
    if c.to_s.match(/\.yaml/)
      year = File.basename(c).match(/(^\d{4})/)[1]
      raise Tempo::DuplicateRecordError.new(File.join(log_main_directory_path,year,File.basename(c))) if File.exists?(File.join(log_main_directory_path,year,File.basename(c)))
      FileUtils.mkdir_p File.join(log_main_directory_path,year) if !File.exists? File.join(log_main_directory_path,year)
      FileUtils.cp c, File.join(log_main_directory_path,year,File.basename(c))
      FileUtils.rm c
    end
  end
end
old_style_log_records_exists?() click to toggle source
# File lib/file_record/file_utility.rb, line 121
def old_style_log_records_exists?
  return false if !File.exists?(log_main_directory_path)
  Pathname.new(log_main_directory_path).children.each do |c|
    return true if c.to_s.match(/\.yaml/)
  end
  false
end
save_instances_to_file(instances) click to toggle source
# File lib/file_record/file_utility.rb, line 35
def save_instances_to_file(instances)

  File.open( file_path,'a' ) do |f|
    instances.each do |i|
      f.puts YAML::dump( i.freeze_dry )
    end
  end
end
split_name() click to toggle source

split Tempo::Model::Project into [“tempo”, “model”, “project”] split Tempo::Model::TimeRecord into [“tempo”, “model”, “time_record”]

# File lib/file_record/file_utility.rb, line 46
def split_name
  @model.name.to_s.split("::").each {|n| n.gsub!(/([a-z])([A-Z])/, '\1_\2'); n.downcase!}
end