class Tacky

Tacky is a simple decorator of an existing object that makes all of its methods cache all values and calculate them only once.

For more information read README file.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2020 Yegor Bugayenko

License

MIT

Constants

STOP

Deep nesting will stop at these classes.

Public Class Methods

new(origin, deep: true) click to toggle source
# File lib/tacky.rb, line 46
def initialize(origin, deep: true)
  @origin = origin
  @cache = {}
  @deep = deep
  @mutex = Mutex.new
end

Public Instance Methods

method_missing(*args) { |*a| ... } click to toggle source
# File lib/tacky.rb, line 53
def method_missing(*args)
  @mutex.synchronize do
    unless @cache.key?(args)
      @cache[args] = @origin.__send__(*args) do |*a|
        yield(*a) if block_given?
      end
      if @deep && STOP.none? { |t| @cache[args].is_a?(t) }
        @cache[args] = Tacky.new(@cache[args], deep: @deep)
      end
    end
    @cache[args]
  end
end
respond_to?(method, include_private = false) click to toggle source
# File lib/tacky.rb, line 67
def respond_to?(method, include_private = false)
  @origin.respond_to?(method, include_private)
end
respond_to_missing?(_method, _include_private = false) click to toggle source
# File lib/tacky.rb, line 71
def respond_to_missing?(_method, _include_private = false)
  true
end