Sha256: 6dee2cf11de84cd3fc0f3d9a19a0bb7fdd9107199545ef2d8a6a61d09cf1135c

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

module Repobahn
  module Repository
    extend ActiveSupport::Concern

   included do
     include Hooks
     define_hook :after_conversion
   end


    module ClassMethods

      def entity_model
        @entity_model || default_entity_model
      end

      def entity_model=(klass)
        @entity_model = klass
      end

      def db_model
        @db_model || default_db_model
      end

      def db_model=(klass)
        @db_model = klass
      end

      def find(id)
        record = db_model.find id
        to_entity_from_db record if record
      end

      def to_entity_from_db(records)
        entities = Array(records).map do |record|
          entity = entity_model.new(record.attributes)
          run_hook :after_conversion, entity, record
          entity
        end
        entities.length > 1 ? entities : entities.first
      end

      private

      def default_entity_model
        full_name = name.split('::')
        model_name = full_name.pop.singularize
        full_name << model_name
        full_name.join('::').constantize
      end

      def default_db_model
        "DB::#{name.split('::').last.singularize}".constantize
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gummi-0.1.0 lib/repobahn/repository.rb
gummi-0.0.9 lib/repobahn/repository.rb
gummi-0.0.8 lib/repobahn/repository.rb
gummi-0.0.7 lib/repobahn/repository.rb
gummi-0.0.6 lib/repobahn/repository.rb