Class: ObjectStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/ObjectModel/Repository/ObjectStorage.rb

Constant Summary collapse

TYPES_TO_INITIALIZE =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, dir) ⇒ ObjectStorage

Returns a new instance of ObjectStorage.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 7

def initialize name, dir
  folder = File.expand_path "#{dir}/#{name}" #.sub(/^\.\//, "")
  Dir.mkdir folder unless File.exist? folder   
  @db = Sequel.connect("sqlite://#{folder}/#{name}.db")
  @db.logger = log
  
  unless @db.table_exists? :           
    initialize_db 
    TYPES_TO_INITIALIZE.each{|type| type.initialize_storage @db}
  end
  
   ||= @db[:metadata]   
   ||= @db[:custom_metadata]
  @tables = {}   
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 5

def 
  
end

Instance Method Details

#[](type) ⇒ Object



23
24
25
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 23

def [] type
  @tables[type] ||= @db[type]
end

#closeObject



53
54
55
56
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 53

def close
  @tables.clear
  @db.disconnect
end

#generate(generator_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 27

def generate generator_name
  generator_name = generator_name.to_s
  @db.transaction do 
    unless row = [:key => generator_name]
       << {:key => generator_name, :value => "0"}
      row = [:key => generator_name]
    end
    id = [:key => generator_name.to_s][:value].to_i
    .filter(:key => generator_name).update(:value => (id + 1).to_s)
    return id.to_s
  end
end

#get(key) ⇒ Object



66
67
68
69
70
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 66

def get key
  key.should! :be_a, String
  row = [:key => key]
  return row ? row[:value] : nil
end


62
63
64
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 62

def print name = nil
  TYPES_TO_INITIALIZE.each{|type| type.print_storage @db, name}
end

#put(key, value) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 72

def put key, value
  key.should! :be_a, String
  value.should! :be_a, String
  
  .filter(:key => key).delete
   << {:key => key, :value => value}
end

#sizeObject



40
41
42
43
44
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 40

def size
  @db.transaction do 
    id = [:key => "size"][:value].to_i     
  end
end

#size=(value) ⇒ Object



46
47
48
49
50
51
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 46

def size= value
  @db.transaction do 
    value.should! :be_a, Number
    .filter(:key => "size").update(:value => value.to_s)
  end
end

#transaction(&b) ⇒ Object



58
59
60
# File 'lib/ObjectModel/Repository/ObjectStorage.rb', line 58

def transaction &b
  @db.transaction &b
end