Class: SDBTools::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/sdbtools/domain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdb, name) ⇒ Domain

Returns a new instance of Domain.



5
6
7
8
9
10
# File 'lib/sdbtools/domain.rb', line 5

def initialize(sdb, name)
  @sdb        = sdb
  @name       = name
  @item_names = nil
  @count      = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/sdbtools/domain.rb', line 3

def name
  @name
end

Instance Method Details

#[](item_name) ⇒ Object



12
13
14
# File 'lib/sdbtools/domain.rb', line 12

def [](item_name)
  @sdb.get_attributes(name, item_name)[:attributes]
end

#countObject



25
26
27
28
# File 'lib/sdbtools/domain.rb', line 25

def count
  return @count if @count
  @count = selection.count
end

#delete(item_name, attributes = {}) ⇒ Object



70
71
72
# File 'lib/sdbtools/domain.rb', line 70

def delete(item_name, attributes={})
  @sdb.delete_attributes(@name, item_name, attributes)
end

#get(item_name, attribute_name = nil) ⇒ Object



44
45
46
# File 'lib/sdbtools/domain.rb', line 44

def get(item_name, attribute_name=nil)
  @sdb.get_attributes(@name, item_name, attribute_name)
end

#item_namesObject



16
17
18
19
20
21
22
23
# File 'lib/sdbtools/domain.rb', line 16

def item_names
  return @item_names if @item_names
  query = Operation.new(@sdb, :query, @name, nil, nil)
  @item_names = query.inject([]) {|names, results| 
    names.concat(results[:items])
    names
  }
end

#items(item_names) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sdbtools/domain.rb', line 30

def items(item_names)
  names  = item_names.map{|n| "'#{n}'"}.join(', ')
  query  = "select * from #{name} where itemName() in (#{names})"
  select = Operation.new(@sdb, :select, query)
  select.inject({}) {|items, results|
    results[:items].each do |item|
      item_name  = item.keys.first
      item_value = item.values.first
      items[item_name] = item_value
    end
    items
  }
end

#put(item_name, attributes, options = {}) ⇒ Object



48
49
50
51
# File 'lib/sdbtools/domain.rb', line 48

def put(item_name, attributes, options={})
  replace = options[:replace] ? :replace : false
  @sdb.put_attributes(@name, item_name, attributes, replace)
end

#select(query) ⇒ Object

Somewhat deprecated. Use #selection() instead



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sdbtools/domain.rb', line 58

def select(query)
  op = Operation.new(@sdb, :select, "select * from #{name} where #{query}")
  op.inject([]){|items,(results, operation)|
    batch_items = results[:items].map{|pair|
      item = pair.values.first
      item.merge!({'itemName()' => pair.keys.first})
      item
    }
    items.concat(batch_items)
  }
end

#selection(options = {}) ⇒ Object



53
54
55
# File 'lib/sdbtools/domain.rb', line 53

def selection(options={})
  Selection.new(@sdb, name, options)
end