Sha256: 48ff3d62224348ec41139a521f2157fde456183da661a6d68623918d3054db80

Contents?: true

Size: 699 Bytes

Versions: 142

Compression:

Stored size: 699 Bytes

Contents

# frozen_string_literal: true

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

142 entries across 142 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.1.0 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.167 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.166 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.165 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.164 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.163 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.162 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.161 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.160 app/documents/document/attribute_initializer/active_model.rb
renalware-core-2.0.159 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.158 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.157 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.156 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.155 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.153 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.152 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.151 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.149 lib/document/attribute_initializer/active_model.rb
renalware-core-2.0.148 lib/document/attribute_initializer/active_model.rb