Sha256: 0776874864f7f6f0cbd100adce6903dbc1df04ca31aa5650e94f578c70556fc1

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module RedisEval
  module GeneratorMethods
    def build(name)
      path = case name
        when Pathname
          name
        when String, Symbol
          name =~ /\.lua$/ ? pathname(name.to_s) : script_path(name)
        else
          raise ArgumentError
        end

      RedisEval::Script.build_from_path(path)
    end

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

      def script_cache
        @script_cache ||= {}
      end

      def script_path(name)
        RedisEval.config.script_paths.each {|path|
          if (script = pathname(path).join("#{name}.lua")).exist?
            return script
          end
        }
        nil
      end

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

        define_singleton_method(name) do |*a, &b|
          script_cache[name.to_s] ||= build(name)
        end

        send(name, *args, &block)
      end

      def respond_to_missing?(name, include_private = false)
        !!script_path(name)
      end
  end

  extend GeneratorMethods
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_eval-0.1.0 lib/redis_eval/generator_methods.rb