Sha256: 3dca5509eef41d7f33a1853a4ca0c358c615e8a3d8181576509eaa9eae8f214b
Contents?: true
Size: 940 Bytes
Versions: 1
Compression:
Stored size: 940 Bytes
Contents
require "active_record" require "active_record/safe_initialize/version" require 'active_support/core_ext/array/extract_options' module ActiveRecord module SafeInitialize def safe_initialize(*attributes, &block) options = attributes.extract_options! default = options.fetch(:with, block) raise ArgumentError, "Missing initialization value" unless default warn "Both :with and block are present; using :with value" if options[:with] && block_given? after_initialize(options) do attributes.each do |attribute| if has_attribute?(attribute) && read_attribute(attribute).nil? value = default value = instance_exec(&value) if value.respond_to?(:call) value = self.send(value) if value.is_a?(Symbol) self.send "#{attribute}=", value end end end end end end ActiveRecord::Base.extend ActiveRecord::SafeInitialize
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
activerecord-safe_initialize-0.2.0 | lib/active_record/safe_initialize.rb |