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/ Will also create the directory if not found This method does not require time to be present in options.
-
#log_records ⇒ Object
Returns the list of log records from a log directory.
-
#model_name ⇒ Object
Tempo::Model::Project -> “project”.
-
#module_name ⇒ Object
Tempo::Model::Project -> “tempo”.
- #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
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
113 114 115 116 117 118 119 120 |
# File 'lib/file_record/file_utility.rb', line 113 def clean_path(file_path) if @destroy and File.exists?(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
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/file_record/file_utility.rb', line 94 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 ⇒ 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/ Will also create the directory if not found This method does not require time to be present in options
79 80 81 82 83 84 85 86 87 |
# File 'lib/file_record/file_utility.rb', line 79 def log_directory_path dir = File.join(@directory, module_name, log_directory) if @create and !File.exists?(dir) Dir.mkdir(dir, 0700) end dir end |
#log_records ⇒ Object
Returns the list of log records from a log directory
108 109 110 |
# File 'lib/file_record/file_utility.rb', line 108 def log_records Dir[log_directory_path + "/*.yaml"].sort! 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 |
#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 |