Class: WAB::Impl::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/wab/impl/model.rb

Overview

The Model class is used to store data when using the WAB::Impl::Shell. It is not intended for any other use. The get and query methods are the primary means of interacting with the model.

The Model is simple in that it stores data in a Hash references by ref numbers. Data is stores in a directory as separate JSON files that are named as the ref number as a 16 character hexidecimal.

Instance Method Summary collapse

Constructor Details

#initialize(dir, indent = 0) ⇒ Model

Create a new Model using the designated directory as the store.

dir

directory to store data in



20
21
22
23
24
25
26
27
28
# File 'lib/wab/impl/model.rb', line 20

def initialize(dir, indent=0)
  @dir = dir.nil? ? nil : File.expand_path(dir)
  @cnt = 0
  @indent = indent
  @map = {}
  @lock = Thread::Mutex.new
  FileUtils.mkdir_p(@dir) unless @dir.nil? || Dir.exist?(@dir)
  load_files unless @dir.nil?
end

Instance Method Details

#get(ref) ⇒ Object

Get a single record in the database. A WAB::Impl::Data object is returned if not nil.

ref

references number of the object to retrieve.



34
35
36
# File 'lib/wab/impl/model.rb', line 34

def get(ref)
  @map[ref]
end

#query(tql) ⇒ Object

Execute a TQL query.

_Note that the current implementation does not support nested data retrieval.

tql

query to execute



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wab/impl/model.rb', line 44

def query(tql)
  rid = tql[:rid]
  where = nil
  filter = nil
  if tql.has_key?(:where)
    w = tql[:where]
    where = w.is_a?(Array) ? ExprParser.parse(w) : w
  end
  filter = ExprParser.parse(tql[:filter]) if tql.has_key?(:filter)

  if tql.has_key?(:insert)
    insert(tql[:insert], rid, where, filter)
  elsif tql.has_key?(:update)
    update(tql[:update], rid, where, filter)
  elsif tql.has_key?(:delete)
    delete(tql[:delete], rid, where, filter)
  else
    select(tql[:select], rid, where, filter)
  end
end