diff options
-rw-r--r-- | lib/efl/eet.rb | 43 | ||||
-rw-r--r-- | spec/eet_spec.rb | 137 |
2 files changed, 86 insertions, 94 deletions
diff --git a/lib/efl/eet.rb b/lib/efl/eet.rb index d546b5e..2e5bf0b 100644 --- a/lib/efl/eet.rb +++ b/lib/efl/eet.rb @@ -4,46 +4,38 @@ require 'efl/ffi/eet' # module Efl + # module Eet # - class << self + class REetFile # - def open path, mode=:eet_file_mode_read, &blk - if blk - f = Efl::FFI.eet_open path, mode - raise Exception.new "Unable to open file #{path}" if f.nil? - yield EetFile.new f - Efl::FFI.eet_close f - else - f = Efl::FFI.eet_open path, mode - return EetFile.new f unless f.nil? - end - end - end - # - class EetFile + include Efl::ClassHelper + @search_paths = [ [Efl::Eet,'eet_'] ].freeze # def initialize ptr @ptr=ptr end private :initialize # - def close - Efl::FFI.eet_close @ptr - @ptr=nil - end - # - def mode_get - Efl::FFI.eet_mode_get @ptr + def self.open path, mode=:eet_file_mode_read, &blk + if blk + f = Efl::Eet.eet_open path, mode + raise Exception.new "Unable to open file #{path}" if f.nil? + yield REetFile.new f + Efl::Eet.eet_close f + else + f = Efl::Eet.eet_open path, mode + return REetFile.new f unless f.nil? + end end # def write key, data, compress=false - Efl::FFI.eet_write @ptr, key, ::FFI::MemoryPointer.from_string(data), data.bytesize, ( compress ? 1 : 0 ) + Efl::Eet.eet_write @ptr, key, FFI::MemoryPointer.from_string(data), data.bytesize, ( compress ? 1 : 0 ) end # def read key - ptr = ::FFI::MemoryPointer.new(:int) - data = Efl::FFI.eet_read @ptr, key, ptr + ptr = FFI::MemoryPointer.new(:int) + data = Efl::Eet.eet_read @ptr, key, ptr s = ptr.read_int ptr.free return nil if s==0 @@ -53,6 +45,7 @@ module Efl end # end + # end # # EOF diff --git a/spec/eet_spec.rb b/spec/eet_spec.rb index a4c1997..048799a 100644 --- a/spec/eet_spec.rb +++ b/spec/eet_spec.rb @@ -5,7 +5,10 @@ require 'efl/eet' # describe Efl::Eet do # - before(:all) { Eet = Efl::Eet } + before(:all) { + Eet = Efl::Eet + REetFile = Efl::Eet::REetFile + } # FP = '/tmp/_eet.cfg' # @@ -28,80 +31,76 @@ describe Efl::Eet do end # it "should have good enums" do - Efl::FFI.enum_type(:eet_file_mode)[:eet_file_mode_invalid].should eql -1 - Efl::FFI.enum_type(:eet_file_mode)[:eet_file_mode_read].should eql 0 - Efl::FFI.enum_type(:eet_file_mode)[:eet_file_mode_write].should eql 1 - Efl::FFI.enum_type(:eet_file_mode)[:eet_file_mode_read_write].should eql 2 + Eet.enum_type(:eet_file_mode)[:eet_file_mode_invalid].should eql -1 + Eet.enum_type(:eet_file_mode)[:eet_file_mode_read].should eql 0 + Eet.enum_type(:eet_file_mode)[:eet_file_mode_write].should eql 1 + Eet.enum_type(:eet_file_mode)[:eet_file_mode_read_write].should eql 2 end # - it "should open and close" do - Eet.init - f = Eet.open FP, Efl::FFI.enum_type(:eet_file_mode)[:eet_file_mode_write] - f.write 'fake', 'value' - f.close - Eet.shutdown - end - # - it "should be able to get file access mode" do - Eet.init - Efl::FFI.enum_type(:eet_file_mode).symbols.each do |m| - next if m==:eet_file_mode_invalid - Eet.open FP, Efl::FFI.enum_type(:eet_file_mode)[m] do |f| - f.mode_get.should eql m + describe Efl::Eet::REetFile do + before(:each) { Eet.init } + after(:each) { Eet.shutdown } + # + it "should open and close" do + f = REetFile.open FP, Eet.enum_type(:eet_file_mode)[:eet_file_mode_write] + f.write 'fake', 'value' + f.close + end + # + it "should be able to get file access mode" do + Eet.enum_type(:eet_file_mode).symbols.each do |m| + next if m==:eet_file_mode_invalid + REetFile.open FP, Eet.enum_type(:eet_file_mode)[m] do |f| + f.mode_get.should eql m + end end end - Eet.shutdown - end - # - it "should write" do - Eet.init - f = Eet.open FP, :eet_file_mode_write - f.mode_get.should eql :eet_file_mode_write - f.write 'config', 'test key' - f.close - Eet.shutdown - end - # - it "should read" do - Eet.init - f = Eet.open FP, :eet_file_mode_read - f.mode_get.should eql :eet_file_mode_read - f.read('config').should eql 'test key' - f.close - Eet.shutdown - end - # - it "should read/write" do - Eet.init - f = Eet.open FP, :eet_file_mode_read_write - f.write 'configg', 'test key' - f.read('configg').should eql 'test key' - f.close - Eet.shutdown - end - # - it "should write in block" do - Eet.init - Eet.open FP, :eet_file_mode_write do |f| - f.write 'config2', 'test--key' + # + it "should write" do + f = REetFile.open FP, :eet_file_mode_write + f.mode_get.should eql :eet_file_mode_write + f.write 'config', 'test key' + f.close end - Eet.shutdown - end - # - it "should read in block" do - Eet.init - Eet.open FP, :eet_file_mode_read do |f| - f.read('config2').should eql 'test--key' + # + it "default mode should be read" do + f = REetFile.open FP + f.mode_get.should eql :eet_file_mode_read + f.close end - Eet.shutdown - end - # - it "should read/write in block" do - Eet.init - Eet.open FP, :eet_file_mode_read_write do |f| - f.write 'config22', 'test--key' - f.read('config22').should eql 'test--key' + # + it "should read" do + f = REetFile.open FP, :eet_file_mode_read + f.mode_get.should eql :eet_file_mode_read + f.read('config').should eql 'test key' + f.close + end + # + it "should read/write" do + f = REetFile.open FP, :eet_file_mode_read_write + f.write 'configg', 'test key' + f.read('configg').should eql 'test key' + f.close + end + # + it "should write in block" do + REetFile.open FP, :eet_file_mode_write do |f| + f.write 'config2', 'test--key' + end + end + # + it "should read in block" do + REetFile.open FP, :eet_file_mode_read do |f| + f.read('config2').should eql 'test--key' + end + end + # + it "should read/write in block" do + REetFile.open FP, :eet_file_mode_read_write do |f| + f.write 'config22', 'test--key' + f.read('config22').should eql 'test--key' + end end - Eet.shutdown + # end end |