Module: Busket::Helper::ClassMethods

Defined in:
lib/busket/helper.rb

Instance Method Summary collapse

Instance Method Details

#bucket(name, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/busket/helper.rb', line 14

def bucket name, opts={}
  buckets[name] = opts 
  
  define_method "#{name}_collection" do
    Busket.mongo_database.collection(name)
  end
  
  define_method "#{name}" do
    linked_id     = send(self.buckets[name.to_sym][:identifier])
    linked_type   = self.class.to_s
    collection_with_name(name).save("linked_id" => linked_id, "linked_type" => linked_type) unless collection_exists?(name)
    self.instance_variable_get("@#{name}") || self.instance_variable_set("@#{name}", collection_with_name(name).find_one("linked_id" => linked_id, "linked_type" => linked_type))
  end
  
  define_method "save_#{name}" do
    collection_with_name(name).save self.instance_variable_get("@#{name}")
  end
end

#bucketsObject



32
33
34
# File 'lib/busket/helper.rb', line 32

def buckets
  read_inheritable_attribute(:buckets)
end