Sha256: 565f0e541e20660d2c763656151410380495840aed0de0ff2c8d5af0acccffbd

Contents?: true

Size: 703 Bytes

Versions: 1

Compression:

Stored size: 703 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|
          defaults = defaults.merge(block.call) if block_given?
          defaults = Hash[defaults.each_pair.map { |key, value| [key, value.is_a?(Proc) ? value.call : value] }]

          initialize_without_default_values(*attributes, &inner_block)

          defaults.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.1 lib/default_values/dsl.rb