Class: FileRecord::Record
- Inherits:
-
Object
- Object
- FileRecord::Record
- Defined in:
- lib/file_record/record.rb
Class Method Summary collapse
-
.create(file, record, options = {}) ⇒ Object
record text as a string, and all objects as yaml don’t write over an existing document unless :force = true options - force: true, overwrite file - format: ‘yaml’, ‘string’.
- .log_dir(model) ⇒ Object
- .log_dirname(model) ⇒ Object
- .log_filename(model, time) ⇒ Object
- .model_filename(model) ⇒ Object
- .read_instances(model, file) ⇒ Object
- .read_log(model, time) ⇒ Object
- .read_model(model) ⇒ Object
-
.save_log(model) ⇒ Object
record a child of Tempo::Model::Log.
-
.save_model(model) ⇒ Object
record a child of Tempo::Model::Base.
Instance Method Summary collapse
Class Method Details
.create(file, record, options = {}) ⇒ Object
record text as a string, and all objects as yaml don’t write over an existing document unless :force = true options
- force: true, overwrite file
- format: 'yaml', 'string'
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/file_record/record.rb', line 12 def create( file, record, ={} ) if record.is_a?(String) format = .fetch(:format, 'string') else format = .fetch(:format, 'yaml') end if File.exists?(file) raise ArgumentError.new "file already exists" unless [:force] end File.open( file,'w' ) do |f| case format when 'yaml' f.puts YAML::dump( record ) when 'string' f.puts record else f.puts record end end end |
.log_dir(model) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/file_record/record.rb', line 42 def log_dir( model ) dir_name = log_dirname model dir = File.join(Dir.home,'tempo', dir_name) Dir.mkdir(dir, 0700) unless File.exists?(dir) dir end |
.log_dirname(model) ⇒ Object
37 38 39 40 |
# File 'lib/file_record/record.rb', line 37 def log_dirname( model ) dir_name = model.name[14..-1].gsub(/([A-Z])/, '_\1').downcase dir = "tempo#{dir_name}s" end |
.log_filename(model, time) ⇒ Object
49 50 51 |
# File 'lib/file_record/record.rb', line 49 def log_filename( model, time ) file = "#{model.day_id( time )}.yaml" end |
.model_filename(model) ⇒ Object
53 54 55 56 |
# File 'lib/file_record/record.rb', line 53 def model_filename( model ) file_name = model.name[14..-1].gsub(/([A-Z])/, '_\1').downcase file = "tempo#{file_name}s.yaml" end |
.read_instances(model, file) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/file_record/record.rb', line 91 def read_instances( model, file ) instances = YAML::load_stream( File.open( file ) ) instances.each do |i| model.new( i ) end end |
.read_log(model, time) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/file_record/record.rb', line 103 def read_log( model, time ) dir = File.join(Dir.home,'tempo', model.dir) file = File.join(dir, model.file( time )) if File.exists? file read_instances model, file end end |
.read_model(model) ⇒ Object
98 99 100 101 |
# File 'lib/file_record/record.rb', line 98 def read_model( model ) file = File.join(Dir.home,'tempo', model.file) read_instances model, file end |
.save_log(model) ⇒ Object
record a child of Tempo::Model::Log
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/file_record/record.rb', line 72 def save_log( model ) dir = log_dir model model.days_index.each do |day, days_logs| file = "#{day.to_s}.yaml" file_path = File.join(dir, file) File.delete( file_path ) if File.exists?( file_path ) # don't write to an empty file next if days_logs.empty? File.open( file_path,'a' ) do |f| days_logs.each do |log| f.puts YAML::dump( log.freeze_dry ) end end end end |
.save_model(model) ⇒ Object
record a child of Tempo::Model::Base
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/file_record/record.rb', line 59 def save_model( model ) file = model_filename model file_path = File.join(Dir.home,'tempo', file) File.delete( file_path ) if File.exists?( file_path ) File.open( file_path,'a' ) do |f| model.index.each do |m| f.puts YAML::dump( m.freeze_dry ) end end end |
Instance Method Details
#delete ⇒ Object
116 117 118 |
# File 'lib/file_record/record.rb', line 116 def delete end |
#update ⇒ Object
112 113 114 |
# File 'lib/file_record/record.rb', line 112 def update end |