module Rubaidh
  module DefaultValues
    def self.included(base)
      base.extend(ActsMethods)
    end

    module ActsMethods
      def default_values(values = {})
        unless included_modules.include? InstanceMethods
          class_inheritable_accessor :default_value_options
          include InstanceMethods
          alias_method_chain :initialize, :default_values
        end
        self.default_value_options = values
      end
      alias_method :default_value, :default_values
    end

    module InstanceMethods
      def initialize_with_default_values(*args)
        returning initialize_without_default_values(*args) do
          default_value_options.each do |k, v|
            write_attribute(k, v) if read_attribute(k).nil?
          end
        end
      end
    end
  end
end
module Rubaidh
  module DefaultValues
    def self.included(base)
      base.extend(ActsMethods)
    end

    module ActsMethods
      def default_values(values = {})
        unless included_modules.include? InstanceMethods
          class_inheritable_accessor :default_value_options
          include InstanceMethods
          alias_method_chain :initialize, :default_values
        end
        self.default_value_options = values
      end
      alias_method :default_value, :default_values
    end

    module InstanceMethods
      def initialize_with_default_values(*args)
        returning initialize_without_default_values(*args) do
          default_value_options.each do |k, v|
            v = v.call if v.respond_to?(:call)
            write_attribute(k, v) if read_attribute(k).nil?
          end
        end
      end
    end
  end
end
Allow a default value to be a lambda.
Allow a default value to be a lambda.

git-svn-id: e0da6f832d6dbf049285600df800d19ec85b03ad@208 d6511e60-4710-0410-a7ce-ed01584c9eb4
