Sha256: 4eea19ddf863b98b0814fa9c69d615cfb89ebd7a6bcfb47e366c760555af0c51

Contents?: true

Size: 1.77 KB

Versions: 26

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

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.25 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.24 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.23 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.22 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.21 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.20 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.19 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.18 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.17 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.16 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.15 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.14 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.13 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.12 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.11 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.10 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.9 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.8 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb
foobara-0.0.7 projects/entity/src/extensions/type_declarations/handlers/extend_entity_type_declaration/to_type_transformer.rb