Class: InlineData::DB
- Inherits:
-
Object
- Object
- InlineData::DB
- Defined in:
- lib/inline_data/db.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #file_lock ⇒ Object
-
#initialize(data_store) ⇒ DB
constructor
A new instance of DB.
- #save ⇒ Object
Constructor Details
#initialize(data_store) ⇒ DB
Returns a new instance of DB.
8 9 10 11 12 13 14 15 |
# File 'lib/inline_data/db.rb', line 8 def initialize(data_store) @file = data_store @db_pos = @file.pos @existed = @db_pos != @file.size @data = @existed ? load : nil @file.rewind @contents = @file.read @db_pos end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
36 37 38 |
# File 'lib/inline_data/db.rb', line 36 def data @data end |
Class Method Details
.default(file) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/inline_data/db.rb', line 17 def self.default(file) if Kernel.const_defined? :DATA new Kernel.const_get :DATA else from file end end |
.from(filename) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/inline_data/db.rb', line 25 def self.from(filename) f = File.new filename loop do line = f.gets break if line.nil? || line.chomp == '__END__' end new f end |
Instance Method Details
#close ⇒ Object
48 49 50 |
# File 'lib/inline_data/db.rb', line 48 def close @file.close unless @file.nil? || @file.closed? end |
#file_lock ⇒ Object
4 5 6 |
# File 'lib/inline_data/db.rb', line 4 def file_lock @file.flock(File::LOCK_NB | File::LOCK_EX) end |
#save ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/inline_data/db.rb', line 38 def save db_str = dump File.open @file.path, 'w+' do |f| f.puts @contents f.puts '__END__' unless @existed f.puts db_str end end |