Sha256: 4eea19ddf863b98b0814fa9c69d615cfb89ebd7a6bcfb47e366c760555af0c51

Contents?: true

Size: 1.77 KB

Versions: 27

Compression:

Stored size: 1.77 KB

Contents

module Foobara
  module TypeDeclarations
    module Handlers
      class ExtendEntityTypeDeclaration < ExtendModelTypeDeclaration
        class ToTypeTransformer < ExtendModelTypeDeclaration::ToTypeTransformer
          # TODO: move this to a more appropriate place
          class EntityPrimaryKeyCaster < Value::Caster
            class << self
              def requires_declaration_data?
                true
              end
            end

            def entity_class
              declaration_data
            end

            def primary_key_type
              entity_class.primary_key_type
            end

            def applicable?(value)
              primary_key_type.applicable?(value)
            end

            def transform(primary_key)
              entity_class.thunk(primary_key)
            end

            def applies_message
              primary_key_type.value_caster.applies_message
            end
          end

          def non_processor_keys
            [:primary_key, *super]
          end

          def process_value(strict_declaration_type)
            super.tap do |outcome|
              if outcome.success?
                type = outcome.result

                entity_class = type.target_class

                unless entity_class.primary_key_attribute
                  entity_class.primary_key(strict_declaration_type[:primary_key])
                end

                unless entity_class.can_be_created_through_casting?
                  type.casters = type.casters.reject do |caster|
                    caster.is_a?(Foobara::BuiltinTypes::Entity::Casters::Hash)
                  end
                end

                type.casters << EntityPrimaryKeyCaster.new(entity_class)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
foobara-0.0.7 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.6 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.5 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.4 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.3 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.2 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.1 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb