Sha256: 92f431868ba77ae9a9393d9f1381c3109dbf080f8d2816ba41b00efbe2b13a03

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module RedisEval
  class ScriptSet
    attr_reader :path

    SCRIPT_SUFFIX = ".lua".freeze

    def initialize(target_path, conn = nil)
      @path = pathname(target_path)
      @redis = conn
    end

    def load(name)
      source = ERB.new(script_path(name).read).result(__binding__)
      loaded_scripts[name.to_s] ||= RedisEval::Script.build_from_parent(source, self)
    end

    def load_all
      path.children(false).each { |child| self.load(child.basename(SCRIPT_SUFFIX).to_s) }
      true
    end

    def redis
      @redis || Redis.current
    end

    attr_writer :redis

    private

      def pathname(path)
        path.is_a?(Pathname) ? path : Pathname.new(path)
      end

      def loaded_scripts
        @loaded_scripts ||= {}
      end

      def script_path(name)
        path.join(name.to_s).sub_ext(SCRIPT_SUFFIX)
      end

      def method_missing(name, *args, &block)
        super unless respond_to?(name)

        self.load(name)
        define_singleton_method(name) { |*_a| loaded_scripts[name.to_s] }
        send(name, *args, &block)
      end

      def respond_to_missing?(name, include_private = false)
        loaded_scripts.has_key?(name.to_s) || script_path(name).exist? || super
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_eval-0.3.0 lib/redis_eval/script_set.rb