Class: Cabi::DataFile
- Inherits:
-
Object
- Object
- Cabi::DataFile
- Defined in:
- lib/datafile.rb
Class Method Summary collapse
- .bulk_selection(id, opts = {}) ⇒ Object
- .bulk_selection?(id) ⇒ Boolean
- .contents(id, opts = {}) ⇒ Object
- .create(id, content = "") ⇒ Object
- .exists?(id) ⇒ Boolean
- .file(id) ⇒ Object
- .file_exists?(id) ⇒ Boolean
- .file_parent(id) ⇒ Object
- .file_yaml_or_non_extension_file(id, opts = {}) ⇒ Object
- .has_yaml_ext?(id) ⇒ Boolean
- .id_array(id) ⇒ Object
- .non_extension_file(id) ⇒ Object
- .sub_yaml(id) ⇒ Object
- .write(id, content) ⇒ Object
- .yaml_exists?(id) ⇒ Boolean
- .yaml_file(id) ⇒ Object
Class Method Details
.bulk_selection(id, opts = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/datafile.rb', line 73 def self.bulk_selection(id, opts={}) contents = [] opts[:only_file] ||= false Dir.glob( File.join( *self.id_array(id) ) ).each do |f| next if f == '.' or f == '..' or File.directory?(f) contents << (opts[:only_file] ? f : File.read(f)) if File.exists?(f) end contents end |
.bulk_selection?(id) ⇒ Boolean
107 108 109 |
# File 'lib/datafile.rb', line 107 def self.bulk_selection?(id) self.id_array(id).join('').index /\*/ end |
.contents(id, opts = {}) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/datafile.rb', line 7 def self.contents(id, opts={}) return self.bulk_selection(id) if self.bulk_selection?(id) return YAML.load(File.read( self.yaml_file(id) )) if self.yaml_exists?(id) and !opts[:no_yaml] return File.read( self.file(id) ) if self.file_exists?(id) return File.read( self.non_extension_file(id) ) if self.non_extension_file(id) return self.sub_yaml(id) nil end |
.create(id, content = "") ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/datafile.rb', line 30 def self.create(id, content="") file = self.file(id) raise "File exists." if self.file_exists?(id) dir = File.dirname(file) FileUtils.mkdir_p(dir) unless File.exists?(dir) FileUtils.touch(file) Cabi.write(id, content) end |
.exists?(id) ⇒ Boolean
91 92 93 |
# File 'lib/datafile.rb', line 91 def self.exists?(id) self.file_exists?(id) or self.yaml_exists?(id) end |
.file(id) ⇒ Object
48 49 50 |
# File 'lib/datafile.rb', line 48 def self.file(id) File.join( *self.id_array(id) ) end |
.file_exists?(id) ⇒ Boolean
95 96 97 |
# File 'lib/datafile.rb', line 95 def self.file_exists?(id) File.exists?( self.file(id) ) end |
.file_parent(id) ⇒ Object
103 104 105 |
# File 'lib/datafile.rb', line 103 def self.file_parent(id) File.dirname( self.file(id) ) end |
.file_yaml_or_non_extension_file(id, opts = {}) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/datafile.rb', line 40 def self.file_yaml_or_non_extension_file(id, opts={}) return self.bulk_selection(id, only_file: true) if self.bulk_selection?(id) return self.yaml_file(id) if self.yaml_exists?(id) return self.file(id) if self.file_exists?(id) return self.non_extension_file(id) if self.non_extension_file(id) nil end |
.has_yaml_ext?(id) ⇒ Boolean
57 58 59 |
# File 'lib/datafile.rb', line 57 def self.has_yaml_ext?(id) (id[-4..-1] == YAML_EXT) end |
.id_array(id) ⇒ Object
86 87 88 89 |
# File 'lib/datafile.rb', line 86 def self.id_array(id) id = id.split( DELIMITER ) [Cabi.data_dir] + id end |
.non_extension_file(id) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/datafile.rb', line 61 def self.non_extension_file(id) base = id.split( DELIMITER ).last file = false Dir.glob( self.file_parent(id) + '/*' ).each do |f| file = File.join(f) if File.basename(f, File.extname(f)) == base break if file end file end |
.sub_yaml(id) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/datafile.rb', line 111 def self.sub_yaml(id) id = id.split( DELIMITER ) val = false id.each_with_index do |key, index| break if val a = [Cabi.data_dir] + id[0..index] last = a[a.length - 1] a[ a.length - 1 ] = last + YAML_EXT if !self.has_yaml_ext?(last) f = File.join( *a ) if File.exists? f data = YAML.load( File.read(f) ) val = data.access( id[index+1..-1].join(DELIMITER) ) end end val end |
.write(id, content) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/datafile.rb', line 16 def self.write(id, content) file = self.file_yaml_or_non_extension_file(id) new_file = self.file(id) if not file parent = File.( '..', new_file ) FileUtils.mkdir_p(parent) unless File.exists?(parent) FileUtils.touch(new_file) unless File.exists?(new_file) file = new_file end File.open( file, 'w') {|f| f.write(content) } end |
.yaml_exists?(id) ⇒ Boolean
99 100 101 |
# File 'lib/datafile.rb', line 99 def self.yaml_exists?(id) File.exists?( self.yaml_file(id) ) end |
.yaml_file(id) ⇒ Object
52 53 54 55 |
# File 'lib/datafile.rb', line 52 def self.yaml_file(id) path = self.has_yaml_ext?(id) ? self.file(id) : self.file(id) + YAML_EXT File.join( path ) end |