Sha256: 7c1d9d02c1e364c1b6cd107866e687c9fb6e6e3194ebd787915df615dfd47561
Contents?: true
Size: 802 Bytes
Versions: 32
Compression:
Stored size: 802 Bytes
Contents
module MemoAttr private def __memoized @__memoized ||= {} end def self.included(mod) mod.extend(ClassMethods) end module ClassMethods def memo_attr(name, &block) raise LocalJumpError.new('no block given') unless block_given? instance_eval { define_memoized_method(name, &block) } class_eval { define_validation_block(name) } end private def define_memoized_method(name, &block) define_method(name) do __memoized.fetch(name) { |k| __memoized[k] = instance_eval(&block) } end end def define_validation_block(name) validate do send(name).errors.each do |key, value| errors.add("#{send(name).class.name.underscore}_#{key}", value) end unless send(name).valid? end end end end
Version data entries
32 entries across 32 versions & 1 rubygems