Class: FileRecord::FileUtility
- Inherits:
-
Object
- Object
- FileRecord::FileUtility
- Defined in:
- lib/file_record/file_utility.rb
Instance Method Summary collapse
-
#clean_path(file_path) ⇒ Object
remove existing file when passed destroy:true in options.
-
#file_path ⇒ Object
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.
-
#filename ⇒ Object
Tempo::Model::Log on 12/1/2015 -> 20151201.yaml Tempo::Model::Base -> tempo_bases.yaml.
-
#initialize(model, options = {}) ⇒ FileUtility
constructor
A new instance of FileUtility.
-
#log_directory ⇒ Object
ex.
-
#log_directory_path ⇒ Object
Tempo::Model::Log -> Users/usrname/(alternate_directory/)tempo/tempo_logs/20XX Will also create the directory if not found.
-
#log_main_directory_path ⇒ Object
Tempo::Model::Log -> Users/usrname/(alternate_directory/)tempo/tempo_logs’ Will also create the directory if not found.
-
#log_records ⇒ Object
Returns the list of log records from a log directory.
- #log_year_directory ⇒ Object
-
#model_name ⇒ Object
Tempo::Model::Project -> “project”.
-
#module_name ⇒ Object
Tempo::Model::Project -> “tempo”.
- #move_old_records ⇒ Object
- #old_style_log_records_exists? ⇒ Boolean
- #save_instances_to_file(instances) ⇒ Object
-
#split_name ⇒ Object
split Tempo::Model::Project into [“tempo”, “model”, “project”] split Tempo::Model::TimeRecord into [“tempo”, “model”, “time_record”].
Constructor Details
#initialize(model, options = {}) ⇒ FileUtility
Returns a new instance of FileUtility.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/file_record/file_utility.rb', line 23 def initialize(model, ={}) @model = model @time = .fetch(:time, nil) @directory = .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 = .fetch( :create, false ) @destroy = .fetch( :destroy, false ) end |
Instance Method Details
#clean_path(file_path) ⇒ Object
remove existing file when passed destroy:true in options
155 156 157 158 159 160 161 162 |
# File 'lib/file_record/file_utility.rb', line 155 def clean_path(file_path) if @destroy and File.exist?(file_path) File.delete(file_path) end file_path end |
#file_path ⇒ Object
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
108 109 110 111 112 113 114 115 116 117 118 119 |
# 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.exist?(dir) Dir.mkdir(dir, 0700) end clean_path File.join(dir, filename) end |
#filename ⇒ Object
Tempo::Model::Log on 12/1/2015 -> 20151201.yaml Tempo::Model::Base -> tempo_bases.yaml
62 63 64 65 66 67 68 |
# 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 ⇒ Object
ex. Tempo::Model::Log -> tempo_logs
71 72 73 74 |
# File 'lib/file_record/file_utility.rb', line 71 def log_directory sn = split_name "#{sn[0]}_#{sn[-1]}s" end |
#log_directory_path ⇒ Object
Tempo::Model::Log -> Users/usrname/(alternate_directory/)tempo/tempo_logs/20XX Will also create the directory if not found
93 94 95 96 97 98 99 100 101 |
# 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.exist?(dir) FileUtils.mkdir_p dir end dir end |
#log_main_directory_path ⇒ Object
Tempo::Model::Log -> Users/usrname/(alternate_directory/)tempo/tempo_logs’ Will also create the directory if not found
87 88 89 |
# 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 ⇒ Object
Returns the list of log records from a log directory
144 145 146 147 148 149 150 151 152 |
# File 'lib/file_record/file_utility.rb', line 144 def log_records records = [] return records if !File.exist?(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 ⇒ Object
77 78 79 80 81 82 83 |
# 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 ⇒ Object
Tempo::Model::Project -> “project”
56 57 58 |
# File 'lib/file_record/file_utility.rb', line 56 def model_name split_name[-1] end |
#module_name ⇒ Object
Tempo::Model::Project -> “tempo”
51 52 53 |
# File 'lib/file_record/file_utility.rb', line 51 def module_name split_name[0] end |
#move_old_records ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/file_record/file_utility.rb', line 129 def move_old_records return false if !File.exist?(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.exist?(File.join(log_main_directory_path,year,File.basename(c))) FileUtils.mkdir_p File.join(log_main_directory_path,year) if !File.exist? 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? ⇒ Boolean
121 122 123 124 125 126 127 |
# File 'lib/file_record/file_utility.rb', line 121 def old_style_log_records_exists? return false if !File.exist?(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) ⇒ Object
35 36 37 38 39 40 41 42 |
# 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 ⇒ Object
split Tempo::Model::Project into [“tempo”, “model”, “project”] split Tempo::Model::TimeRecord into [“tempo”, “model”, “time_record”]
46 47 48 |
# 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 |