Sha256: f3762d0eaae1eab6c6fe686f673edd0f12414eb09bd9432ddc064941cbde32a8

Contents?: true

Size: 1.05 KB

Versions: 27

Compression:

Stored size: 1.05 KB

Contents

module Foobara
  module BuiltinTypes
    module Attributes
      module SupportedTransformers
        class Defaults < Value::Transformer
          class << self
            def requires_parent_declaration_data?
              true
            end
          end

          def transform(attributes_hash)
            to_apply = {}

            defaults.each_pair do |attribute_name, default|
              if attributes_hash.key?(attribute_name)
                value = attributes_hash[attribute_name]

                if value.nil?
                  allow_nil = parent_declaration_data[:element_type_declarations][attribute_name][:allow_nil]

                  unless allow_nil
                    to_apply[attribute_name] = default
                  end
                end
              else
                to_apply[attribute_name] = default
              end
            end

            if to_apply.empty?
              attributes_hash
            else
              attributes_hash.merge(to_apply)
            end
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
foobara-0.0.7 projects/builtin_types/src/attributes/supported_transformers/defaults.rb
foobara-0.0.6 projects/builtin_types/src/attributes/supported_transformers/defaults.rb
foobara-0.0.5 projects/builtin_types/src/attributes/supported_transformers/defaults.rb
foobara-0.0.4 projects/builtin_types/src/attributes/supported_transformers/defaults.rb
foobara-0.0.3 projects/builtin_types/src/attributes/supported_transformers/defaults.rb
foobara-0.0.2 projects/builtin_types/src/attributes/supported_transformers/defaults.rb
foobara-0.0.1 projects/builtin_types/src/attributes/supported_transformers/defaults.rb