Sha256: f2b7654b3a87769bc83b6b193866b227f5f71e68e0106e1f48bfc48f69ada911

Contents?: true

Size: 1.17 KB

Versions: 18

Compression:

Stored size: 1.17 KB

Contents

# -*- encoding : utf-8 -*-
module Dao
  class << Conducer
    def autocrud!
      include(Conducer::AutoCRUD)
    end
    alias_method('crud!', 'autocrud!')
  end

  class Conducer
    module AutoCRUD
      Code = proc do
        class << self
          def db
            @db ||= Db.instance
          end

          def db_collection
            db.collection(collection_name)
          end

          def all(*args)
            hashes = db_collection.all()
            hashes.map{|hash| new(hash)}
          end

          def find(*args)
            options = args.extract_options!.to_options!
            id = args.shift || options[:id]
            hash = db_collection.find(id)
            new(hash) if hash
          end
        end

        def save
          id = self.class.db_collection.save(@attributes)
          @attributes.set(:id => id)
          true
        end

        def destroy
          id = self.id
          if id
            self.class.db_collection.destroy(id)
            @attributes.rm(:id)
          end
          id
        end
      end

      def AutoCRUD.included(other)
        super
      ensure
        other.module_eval(&Code)
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
dao-8.0.1 lib/dao/conducer/autocrud.rb
dao-8.0.0 lib/dao/conducer/autocrud.rb
dao-7.0.0 lib/dao/conducer/autocrud.rb
dao-5.6.1 lib/dao/conducer/autocrud.rb
dao-5.5.2 lib/dao/conducer/autocrud.rb
dao-5.5.1 lib/dao/conducer/autocrud.rb
dao-5.5.0 lib/dao/conducer/autocrud.rb
dao-5.4.0 lib/dao/conducer/autocrud.rb
dao-5.3.3 lib/dao/conducer/autocrud.rb
dao-5.3.2 lib/dao/conducer/autocrud.rb
dao-5.3.0 lib/dao/conducer/autocrud.rb
dao-5.2.1 lib/dao/conducer/autocrud.rb
dao-5.1.2 lib/dao/conducer/autocrud.rb
dao-5.1.1 lib/dao/conducer/autocrud.rb
dao-5.0.0 lib/dao/conducer/autocrud.rb
dao-4.6.4 lib/dao/conducer/autocrud.rb
dao-4.4.4 lib/dao/conducer/autocrud.rb
dao-4.4.2 lib/dao/conducer/autocrud.rb