Sha256: 9e8a12fd8c26b40a154acc583f19f5f6233d52f62abe5c029246a064851b36fc

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

module Repobahn
  module Repository
    extend ActiveSupport::Concern

    include ::Repobahn::Repository::ActiveRecord

    included do
      include Hooks
      define_hook :after_conversion
    end

    module ClassMethods

      def db_class(*args)
        @db_class = args.first unless args.empty?
        @db_class || default_db_class
      end

      def entity_class(*args)
        @entity_class = args.first unless args.empty?
        @entity_class || default_entity_class
      end

      def db_instances_to_entities(db_instances)
        entities = Array(db_instances).map { |db_instance| db_instance_to_entity(db_instance) }
        entities.length > 1 ? entities : entities.first
      end

      def db_instance_to_entity(db_instance)
        entity = entity_class.new(db_instance.attributes)
        run_hook :after_conversion, entity, db_instance
        entity
      end

      private

      def default_db_class
        parts = default_entity_class.to_s.split('::')
        parts.insert -2, 'DB'
        parts.join('::').constantize
      end

      def default_entity_class
        name.singularize.constantize
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gummi-0.2.3 lib/repobahn/repository.rb
gummi-0.2.2 lib/repobahn/repository.rb
gummi-0.2.1 lib/repobahn/repository.rb
gummi-0.2.0 lib/repobahn/repository.rb