Class: FabulatorExhibitExtension::ItemCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/fabulator_exhibit_extension/database.rb

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ ItemCollection

Returns a new instance of ItemCollection.



25
26
27
28
# File 'lib/fabulator_exhibit_extension/database.rb', line 25

def initialize(db)
  @db = db
  @items = { }
end

Instance Method Details

#[](nom) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fabulator_exhibit_extension/database.rb', line 47

def [](nom)
  return @items[nom] if @items.include?(nom)
  ob = nil
  begin
    ob = @db.fabulator_exhibit_items.find(:first, [ 'uuid = ?', nom ])
  rescue
    ob = nil
  end
  if ob.nil? 
    ob = FabulatorExhibitItem.new({
      :uuid => nom,
      :fabulator_exhibit_id => @db.id
    })
  end
  @items[nom] = Item.new(ob)
end

#[]=(nom, hash) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/fabulator_exhibit_extension/database.rb', line 74

def []=(nom, hash)
  ob = self[nom]
  hash.each_pair do |k,v|
    ob[k] = v
  end
  ob.save
end

#delete_if(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/fabulator_exhibit_extension/database.rb', line 30

def delete_if(&block)
  self.each do |i|
    r = yield i['id'], i
    if r
      @items.delete(i['id'])
      i.delete
    end
  end
end

#each(&block) ⇒ Object



40
41
42
43
44
45
# File 'lib/fabulator_exhibit_extension/database.rb', line 40

def each(&block)
  @db.fabulator_exhibit_items.find(:all).each do |i|
    @items[i.id.to_s] ||= Item.new(i)
    yield @items[i.id.to_s]
  end
end

#include?(nom) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
# File 'lib/fabulator_exhibit_extension/database.rb', line 64

def include?(nom)
  ob = nil
  begin
    ob = @db.fabulator_exhibit_items.find(:first, :conditions => [ 'uuid = ?', nom ])
  rescue
    ob = nil
  end
  return !ob.nil?
end

#to_jsonObject



82
83
84
85
86
# File 'lib/fabulator_exhibit_extension/database.rb', line 82

def to_json
  '[' +
  @db.fabulator_exhibit_items.find(:all).collect{ |i| i.data }.join(", ") +
  ']'
end