Sha256: 3cbca15d5db71a3c81c8de5980457c18d6c62165b85e5a6dcacd6102e6e68697

Contents?: true

Size: 1.9 KB

Versions: 33

Compression:

Stored size: 1.9 KB

Contents

module Husky

  module Repo

    class ActiveRecord

      def all
        entity.wrap(data_source.all)
      end

      def find(id)
        entity.new(data_source.find(id))
      end

      def new(attributes = {})
        entity.new(data_source.new(attributes))
      end

      # DEPRECATE
      def build(attributes)
        entity.new(data_source.build(attributes))
      end

      def save(item)
        item.save
      end

      def create(attributes)
        entity.new(data_source.create(attributes))
      end

      def update(item, attributes)
        entity.new(item.update_attributes(attributes))
      end

      def destroy(item)
        item.destroy
      end

      private

      def data_source
        raise NotImplementedError
      end

      def entity
        raise NotImplementedError
      end

    end

    class InMemory

      def initialize
        @records = {}
        @id      = 1
      end

      def all
        @records
      end

      def new(attributes = {})
        entity.new(data_source.new(attributes))
      end

      def find(id)
        entity.new(@records[id.to_i])
      end

      def save(object)
        object.id = @id
        @records[@id] = object
        @id += 1
        entity.new(object)
      end

      def create(attributes = {})
        save(new(attributes))
      end

      def update(item, attributes)
        entity.new(item.update_attributes(attributes))
      end

      def destroy(object)
        @records.delete(object.id)
      end

      private

      def data_source
        raise NotImplementedError
      end

      def entity
        raise NotImplementedError
      end

    end

    class Registry

      class << self

        def register(type, repo)
          repositories[type] = repo
        end

        def repositories
          @repositories ||= {}
        end

        def for(type)
          repositories[type]
        end

      end

    end

  end

end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
husky-0.5.4 lib/husky/repo.rb
husky-0.5.3 lib/husky/repo.rb
husky-0.5.1 lib/husky/repo.rb
husky-0.5.0 lib/husky/repo.rb
husky-0.4.9 lib/husky/repo.rb
husky-0.4.8 lib/husky/repo.rb
husky-0.4.7 lib/husky/repo.rb
husky-0.4.6 lib/husky/repo.rb
husky-0.4.5 lib/husky/repo.rb
husky-0.4.4 lib/husky/repo.rb
husky-0.4.3 lib/husky/repo.rb
husky-0.4.2 lib/husky/repo.rb
husky-0.4.1 lib/husky/repo.rb
husky-0.4.0 lib/husky/repo.rb
husky-0.3.8 lib/husky/repo.rb
husky-0.3.7 lib/husky/repo.rb
husky-0.3.6 lib/husky/repo.rb
husky-0.3.5 lib/husky/repo.rb
husky-0.3.4 lib/husky/repo.rb
husky-0.3.3 lib/husky/repo.rb