6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/default_values/dsl.rb', line 6
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
|