Sha256: 27c8b5b6baf78f83b211b68b137bf0b69c60e4514f74bae820e83023c87318ba

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module Foobara
  module BuiltinTypes
    module Entity
      module Casters
        # TODO: We need a way of disabling/enabling this and it should probably be disabled by default.
        class Hash < Attributes::Casters::Hash
          class << self
            def requires_parent_declaration_data?
              true
            end
          end

          def cast(attributes)
            symbolized_attributes = super

            outcome = entity_class.attributes_type.process_value(symbolized_attributes)

            if outcome.success?
              entity_class.create(symbolized_attributes)
            else
              # we build an instance so that it can fail a validator later. But we already know we don't want to
              # persist this thing. So use build instead of create.
              entity_class.build(outcome.result)
            end
          end

          def entity_class
            type = parent_declaration_data[:type]

            if type == :entity
              Object.const_get(parent_declaration_data[:model_class])
            else
              Foobara::Namespace.current.foobara_lookup_type!(type).target_class
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
foobara-0.0.30 projects/entity/src/extensions/builtin_types/entity/casters/hash.rb
foobara-0.0.29 projects/entity/src/extensions/builtin_types/entity/casters/hash.rb