Class: NERA::Database

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/nera/nera_database.rb

Constant Summary collapse

VALUE_CLASSES =

search a data file in this order classes of values

[ String, Symbol, Integer, Float, Date, DateTime, TrueClass, FalseClass, NilClass ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_file, notify_observer_arg = nil) ⇒ Database




81
82
83
84
85
86
87
88
89
# File 'lib/nera/nera_database.rb', line 81

def initialize( path_to_file, notify_observer_arg = nil )
  @filename = path_to_file
  @in_transaction = nil
  @notify_observer_arg = notify_observer_arg
  @yml_path = nil
  unless File.exist?(@filename)
    raise "No such database : #{@filename}"
  end
end

Class Method Details

.create_table(filename) ⇒ Object




141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nera/nera_database.rb', line 141

def self.create_table( filename)
  if File.exists?( filename)
    $stderr.puts "Database #{filename} already exists."
    return nil
  end
  db = PStore.new( filename)
  db.transaction(false) do
    db[:max_id] = 0
  end
  return true
end

Instance Method Details

#add(new_rec) ⇒ Object




155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nera/nera_database.rb', line 155

def add( new_rec )
  unless valid_as_a_record?( new_rec)
    raise ArgumentError, "This record is not valid."
  end
  new_id = nil
  transaction do
    new_id = @pstore[:max_id] + 1
    new_rec[:id] = new_id
    @pstore[:max_id] = new_id
    @pstore[new_id] = new_rec
    changed
  end
  return new_id
end

#destroy(ids) ⇒ Object

  • destroy( id )

— destroy records — id may be an Integer, Array of Integers, Range — if the specified record is not found in the table, return nil.



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/nera/nera_database.rb', line 271

def destroy( ids)
  if ids.is_a?( Array)
    status = []
    transaction do
      ids.each do |id|
        status << destroy( id)
      end
    end
    return status
  elsif ids.is_a?( Range)
    flag = nil
    transaction do
      id_array = @pstore.roots.find_all do |id|
        ids.include?(id)
      end
      flag = destroy( id_array)
    end
    flag = flag.find do |f| f end
    return flag
  elsif ids.is_a?( Integer)
    stat = nil
    transaction do
      stat = @pstore.delete(ids)
      changed
    end
    return stat
  else
    raise ArgumentError, "argument must be specified by Integer, Array of Integer, or Range. : #{ids.class}"
  end
  return true
end

#find_allObject

— find all — usage: find_all { ..(condition).. }



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/nera/nera_database.rb', line 224

def find_all
  matched = []
  readonly_transaction do
    all_ids = @pstore.roots
    all_ids.delete( :max_id)
    all_ids.each do |id|
      if yield @pstore[id].dup
        matched << @pstore[id]
      end
    end
  end
  matched.sort! do |rec1,rec2|
    rec1[:id] <=> rec2[:id]
  end
  matched = nil if matched.size == 0
  return matched
end

#find_by_id(ids) ⇒ Object

— find methods ————————–



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/nera/nera_database.rb', line 186

def find_by_id( ids)
  if ids.is_a?(Integer)
    readonly_transaction do
      return @pstore[ids]
    end
  elsif ids.is_a?(Array)
    found = []
    readonly_transaction do
      # find all ----
      ids.each do |id|
        found << @pstore[id]
      end
      return found
    end
  elsif ids.is_a?(Range)
    found = []
    readonly_transaction do
      matched_ids = @pstore.roots.find_all do |id|
        ids.include?(id)
      end
      return nil unless matched_ids
      matched_ids.sort! do |id1,id2|
        id1 <=> id2
      end
      matched_ids.each do |id|
        found << @pstore[id]
      end
      found = nil if found.size == 0
      return found
    end
  else 
    raise ArgumentError, "Argument of the find method must be Integer, Array or Range. : #{ids.class}"
  end
end

#set_yaml_file(yml_path) ⇒ Object




92
93
94
# File 'lib/nera/nera_database.rb', line 92

def set_yaml_file( yml_path)
  @yml_path = yml_path.to_s
end

#transactionObject




97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nera/nera_database.rb', line 97

def transaction
  if @in_transaction     # nothing happens when the process is already in transaction
    yield
    return nil
  end
  @pstore = PStore.new(@filename)
  @pstore.transaction( false) do
    begin
      @in_transaction = true
      yield
      if @notify_observer_arg
        notify_observers( @notify_observer_arg)
      else
        notify_observers
      end
      if @yml_path
        File.open( @yml_path,'w') do |io|
          l = find_all do |r| true end
          YAML.dump( l, io)
          io.flush
        end
      end
    ensure
      @in_transaction = false
      nil
    end
  end
end

#update(rec) ⇒ Object

— update method —

  • update( rec )

— update a record — ex. db.update( :key1=>‘updated’, :key2=>DateTime.now ) — returns nil if the record of the specified ‘id’ does not exist.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/nera/nera_database.rb', line 248

def update( rec)
  unless valid_as_a_record?( rec)
    $stderr.puts "This record is not valid."
    return nil
  end
  id_to_update = rec[:id]
  flag = nil
  transaction do
    unless @pstore.root?( id_to_update)
      return nil
    end
    @pstore[id_to_update] = rec
    flag = true
    changed
  end
  return flag
end