Sha256: ecaf982ca22027e8357d02d45ad37148466513089de5182a6b848f716dbc7e3a

Contents?: true

Size: 721 Bytes

Versions: 1

Compression:

Stored size: 721 Bytes

Contents

module DefaultValues
  module DSL
    extend ActiveSupport::Concern

    module ClassMethods
      def default_values(defaults = {}, &block)
        define_method(:initialize_with_default_values) do |*attributes, &inner_block|
          values = defaults
          values = values.merge(block.call) if block_given?
          values = Hash[values.each_pair.map { |key, value| [key, value.is_a?(Proc) ? value.call : value] }]

          initialize_without_default_values(*attributes, &inner_block)

          values.each_pair do |key, value|
            write_attribute(key, value) unless attribute_present?(key)
          end
        end
        alias_method_chain :initialize, :default_values
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ar_default_values-0.5.2 lib/default_values/dsl.rb