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

Version Path
jobshop-0.0.179 lib/jobshop/support/memo_attr.rb
jobshop-0.0.167 lib/jobshop/support/memo_attr.rb
jobshop-0.0.163 lib/jobshop/support/memo_attr.rb
jobshop-0.0.157 lib/jobshop/support/memo_attr.rb
jobshop-0.0.131 lib/jobshop/support/memo_attr.rb
jobshop-0.0.127 lib/jobshop/support/memo_attr.rb
jobshop-0.0.113 lib/jobshop/support/memo_attr.rb
jobshop-0.0.109 lib/jobshop/support/memo_attr.rb
jobshop-0.0.107 lib/jobshop/support/memo_attr.rb
jobshop-0.0.101 lib/jobshop/support/memo_attr.rb
jobshop-0.0.67 lib/jobshop/support/memo_attr.rb
jobshop-0.0.61 lib/jobshop/support/memo_attr.rb
jobshop-0.0.59 lib/jobshop/support/memo_attr.rb
jobshop-0.0.53 lib/jobshop/support/memo_attr.rb
jobshop-0.0.41 lib/jobshop/support/memo_attr.rb
jobshop-0.0.37 lib/jobshop/support/memo_attr.rb
jobshop-0.0.29 lib/jobshop/support/memo_attr.rb
jobshop-0.0.23 lib/jobshop/support/memo_attr.rb
jobshop-0.0.16 lib/jobshop/support/memo_attr.rb
jobshop-0.0.15 lib/jobshop/support/memo_attr.rb