Class: FabulatorExhibitExtension::TypeCollection

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

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ TypeCollection

Returns a new instance of TypeCollection.



130
131
132
133
# File 'lib/fabulator_exhibit_extension/database.rb', line 130

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

Instance Method Details

#[](nom) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fabulator_exhibit_extension/database.rb', line 135

def [](nom)
  return @types[nom] if @types.include?(nom)
  ob = nil
  begin
    ob = @db.fabulator_exhibit_types.find(:first, :conditions => [ 'name = ?', nom ])
  rescue
    ob = nil
  end
  if ob.nil?
    ob = FabulatorExhibitType.new({
      :name => nom,
      :fabulator_exhibit_id => @db.id
    })
  end
  @types[nom] ||= Type.new(ob)
end

#[]=(nom, hash) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/fabulator_exhibit_extension/database.rb', line 152

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

#to_jsonObject



160
161
162
163
164
165
166
# File 'lib/fabulator_exhibit_extension/database.rb', line 160

def to_json
  '{' +
  @db.fabulator_exhibit_types.find(:all).collect{ |t|
    t.name.to_json + ":" + t.data
  }.join(", ") +
  '}'
end