Class: Bike::Storage::File
- Inherits:
-
Bike::Storage
- Object
- Bike::Storage
- Bike::Storage::File
- Defined in:
- lib/_storage/file.rb
Instance Attribute Summary
Attributes inherited from Bike::Storage
Class Method Summary collapse
- .available? ⇒ Boolean
- .load_skel ⇒ Object
- .traverse(dir = '/', root = Bike['storage']['File']['data_dir'], &block) ⇒ Object
Instance Method Summary collapse
- #build(v) ⇒ Object
- #clear ⇒ Object
- #delete(id) ⇒ Object
-
#initialize(sd) ⇒ File
constructor
A new instance of File.
- #move(old_id, new_id) ⇒ Object
- #store(id, v, ext = nil) ⇒ Object
- #val(id = nil) ⇒ Object
Methods inherited from Bike::Storage
instance, #last, #navi, #order, #persistent?, #select
Constructor Details
#initialize(sd) ⇒ File
Returns a new instance of File.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/_storage/file.rb', line 60 def initialize(sd) super unless @@loaded ||= false entries = ::Dir.glob ::File.join(Bike['storage']['File']['data_dir'], '*') self.class.load_skel if entries.empty? @@loaded = true end @dir = ::File.join(Bike['storage']['File']['data_dir'], @sd[:folder][:dir]) ::FileUtils.mkpath(@dir) unless ::File.directory? @dir end |
Class Method Details
.available? ⇒ Boolean
56 57 58 |
# File 'lib/_storage/file.rb', line 56 def self.available? Bike['storage']['File'] && Bike['storage']['File']['data_dir'] end |
.load_skel ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/_storage/file.rb', line 42 def self.load_skel self.traverse('/', Bike['skin_dir']) {|entry| dir = ::File.join(Bike['storage']['File']['data_dir'], entry[:dir]) unless ::File.exists? ::File.join(dir, entry[:base_name]) ::FileUtils.mkpath(dir) unless ::File.directory? dir ::FileUtils.cp( ::File.join(Bike['skin_dir'], entry[:dir], entry[:base_name]), ::File.join(dir, entry[:base_name]), {:preserve => true} ) end } end |
.traverse(dir = '/', root = Bike['storage']['File']['data_dir'], &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/_storage/file.rb', line 13 def self.traverse(dir = '/', root = Bike['storage']['File']['data_dir'], &block) ::Dir.glob(::File.join(root, dir, '*')).sort.collect {|file| ftype = ::File.ftype file base_name = ::File.basename file id, ext = base_name.split('.', 2) id = "main-#{id}" if id =~ Bike::REX::ID full_name = ::File.join(dir, id).gsub(::File::SEPARATOR, '-') if ftype == 'file' && id.sub(/^([^\d\-]+-)+/, '') =~ Bike::REX::ID val = nil ::File.open(file, 'r') {|f| f.flock ::File::LOCK_SH f.binmode val = f.read f.flock ::File::LOCK_UN } block.call( :dir => dir, :base_name => base_name, :full_name => full_name, :ext => ext, :val => (ext == 'yaml' ? YAML.load(val) : val) ) elsif ftype == 'directory' && base_name !~ /\A#{Bike::REX::DIR_STATIC}\z/ self.traverse(::File.join(dir, base_name), root, &block) end }.compact.flatten end |
Instance Method Details
#build(v) ⇒ Object
83 84 85 86 87 |
# File 'lib/_storage/file.rb', line 83 def build(v) clear v.each {|id, v| store(id, v) } self end |
#clear ⇒ Object
89 90 91 92 |
# File 'lib/_storage/file.rb', line 89 def clear glob.each {|file| ::File.unlink ::File.join(@dir, file) } self end |
#delete(id) ⇒ Object
98 99 100 |
# File 'lib/_storage/file.rb', line 98 def delete(id) remove_file(id) && id end |
#move(old_id, new_id) ⇒ Object
102 103 104 |
# File 'lib/_storage/file.rb', line 102 def move(old_id, new_id) rename_file(old_id, new_id) && new_id end |
#store(id, v, ext = nil) ⇒ Object
94 95 96 |
# File 'lib/_storage/file.rb', line 94 def store(id, v, ext = nil) save(id, v, ext) end |
#val(id = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/_storage/file.rb', line 71 def val(id = nil) if id load id else # this could be HUGE. _select_all({}).inject({}) {|v, id| v[id] = load id v } end end |