Sha256: 3485b3ae481850417c6c04780b58ac5b24e61e58820989d5b8f63ba5d0f26602

Contents?: true

Size: 668 Bytes

Versions: 35

Compression:

Stored size: 668 Bytes

Contents

module Document
  module AttributeInitializer
    class ActiveModel < Base
      def call
        merge_default_value
        yield name, type, options
        add_validation
      end

      private

      def merge_default_value
        options.reverse_merge!(default: type.public_send(:new))
      end

      def add_validation
        attribute_name = name

        # Add validation callback
        klass.validate("#{name}_valid".to_sym)

        # Validation method
        klass.send(:define_method, "#{name}_valid".to_sym) do
          errors.add(attribute_name.to_sym, :invalid) if public_send(attribute_name).invalid?
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
renalware-core-2.0.16 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.15 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.14 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.13 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.12 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.11 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.9 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.8 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.7 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.5 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.4 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.3 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.2 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.1 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.0 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.0.pre.rc13 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.0.pre.rc11 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.0.pre.rc10 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.0.pre.rc9 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.0.pre.rc8 lib/document/attribute_initializer/active_model.rb