redis_eval 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 55a6600dac30e393a8ecd8b4602c8d35eddf7d14
4
+ data.tar.gz: 46365e2a76f227fd680febda183fa88155c6dc96
5
+ SHA512:
6
+ metadata.gz: 4dc87bb459df935910f8380b59d593c22f2a2d9ab593516490607aadb01fb58326aa6b2dd653d274fb6e53bea4ad20cb1f61ef28ff62ff320bdb719b055ff15a
7
+ data.tar.gz: 66ff511f7e349669f0e2d22449f2d797f17e2ac3c40620240c7e4cfed773d92c675bb17cb851b58c4e3ba7a0bec01d57aa508cece9c3b1be24b1dfbb5d1e060f
@@ -0,0 +1 @@
1
+ repo_token: bhxUt56VkshTmwvYPRYtNwhwJhcS5RBCF
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.7
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.10.6
6
+ services:
7
+ - redis-server
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_eval.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 i2bskn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,85 @@
1
+ # RedisEval
2
+
3
+ [![Build Status](https://travis-ci.org/i2bskn/redis_eval.svg)](https://travis-ci.org/i2bskn/redis_eval)
4
+ [![Coverage Status](https://coveralls.io/repos/i2bskn/redis_eval/badge.svg?branch=master&service=github)](https://coveralls.io/github/i2bskn/redis_eval?branch=master)
5
+
6
+ Evaluate Lua scripts with Redis.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'redis_eval'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install redis_eval
23
+
24
+ ## Usage
25
+
26
+ #### Configurations
27
+
28
+ - `redis_options` [Hash] Generation options of Redis instance.
29
+ - `script_paths` [Array] Path to find a script file.
30
+
31
+ Example:
32
+
33
+ ```ruby
34
+ RedisEval.configure do |config|
35
+ config.redis_options = { url: "redis://localhost:6379/1" }
36
+ config.script_paths = ["/path/to/script_dir"]
37
+ end
38
+ ```
39
+
40
+ #### Basic usage
41
+
42
+ Build and execute the script path to argument.
43
+
44
+ ```ruby
45
+ script = RedisEval.build("/path/to/script.lua")
46
+ script.execute
47
+ ```
48
+
49
+ Directly by the code in the argument to build.
50
+
51
+ ```ruby
52
+ script = RedisEval::Script.new("hello", "return 'Hello world!'")
53
+ script.execute
54
+ ```
55
+
56
+ #### Find and Build
57
+
58
+ To build and looking for a script from name.
59
+ This function must be set `script_paths` option.
60
+
61
+ ```ruby
62
+ RedisEval.hello.execute # => Execute "/path/to/script_dir/hello.lua".
63
+ ```
64
+
65
+ #### Script test
66
+
67
+ Test the script by execute the command.
68
+
69
+ ```
70
+ $ redis_eval_test /path/to/script.lua 1 key1 argv1
71
+ ```
72
+
73
+ ## Development
74
+
75
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/i2bskn/redis_eval. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
85
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redis_eval"
5
+ require "pry"
6
+
7
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redis_eval"
5
+
6
+ if url = ENV["REDIS_EVAL_URL"]
7
+ RedisEval.config.redis_options = { url: url }
8
+ end
9
+
10
+ lua_script = ARGV[0] || (raise ArgumentError, "Unknown script path")
11
+ key_count = ARGV[1].to_i
12
+ argv = ARGV[2..-1] || []
13
+
14
+ raise ArgumentError, "Not enough KEYS of argument." if key_count > argv.size
15
+ keys = argv.shift(key_count)
16
+
17
+ script = RedisEval.build(lua_script)
18
+ p script.execute(keys, argv)
@@ -0,0 +1,8 @@
1
+ require "digest"
2
+ require "pathname"
3
+ require "redis"
4
+
5
+ require "redis_eval/version"
6
+ require "redis_eval/configuration"
7
+ require "redis_eval/generator_methods"
8
+ require "redis_eval/script"
@@ -0,0 +1,58 @@
1
+ module RedisEval
2
+ class Configuration
3
+ module Accessible
4
+ def configure(options = {}, &block)
5
+ config.merge!(options) unless options.empty?
6
+ config.configure(&block) if block_given?
7
+ end
8
+
9
+ def config
10
+ @_config ||= Configuration.new
11
+ end
12
+ end
13
+
14
+ VALID_OPTIONS = [
15
+ :redis_options,
16
+ :script_paths,
17
+ ].freeze
18
+
19
+ attr_accessor *VALID_OPTIONS
20
+
21
+ def initialize
22
+ reset
23
+ end
24
+
25
+ def redis(cache_disable: false)
26
+ if cache_disable
27
+ @_redis = generate_redis_connection!
28
+ else
29
+ @_redis ||= generate_redis_connection!
30
+ end
31
+ end
32
+
33
+ def configure
34
+ yield self
35
+ end
36
+
37
+ def merge(params)
38
+ self.dup.merge!(params)
39
+ end
40
+
41
+ def merge!(params)
42
+ params.keys.each {|key| self.send("#{key}=", params[key]) }
43
+ self
44
+ end
45
+
46
+ def reset
47
+ self.redis_options = nil
48
+ self.script_paths = []
49
+ end
50
+
51
+ private
52
+ def generate_redis_connection!
53
+ redis_options ? Redis.new(**redis_options) : Redis.current
54
+ end
55
+ end
56
+
57
+ extend Configuration::Accessible
58
+ end
@@ -0,0 +1,50 @@
1
+ module RedisEval
2
+ module GeneratorMethods
3
+ def build(name)
4
+ path = case name
5
+ when Pathname
6
+ name
7
+ when String, Symbol
8
+ name =~ /\.lua$/ ? pathname(name.to_s) : script_path(name)
9
+ else
10
+ raise ArgumentError
11
+ end
12
+
13
+ RedisEval::Script.build_from_path(path)
14
+ end
15
+
16
+ private
17
+ def pathname(path)
18
+ path.is_a?(String) ? Pathname.new(path) : path
19
+ end
20
+
21
+ def script_cache
22
+ @script_cache ||= {}
23
+ end
24
+
25
+ def script_path(name)
26
+ RedisEval.config.script_paths.each {|path|
27
+ if (script = pathname(path).join("#{name}.lua")).exist?
28
+ return script
29
+ end
30
+ }
31
+ nil
32
+ end
33
+
34
+ def method_missing(name, *args, &block)
35
+ super unless respond_to?(name)
36
+
37
+ define_singleton_method(name) do |*a, &b|
38
+ script_cache[name.to_s] ||= build(name)
39
+ end
40
+
41
+ send(name, *args, &block)
42
+ end
43
+
44
+ def respond_to_missing?(name, include_private = false)
45
+ !!script_path(name)
46
+ end
47
+ end
48
+
49
+ extend GeneratorMethods
50
+ end
@@ -0,0 +1,51 @@
1
+ module RedisEval
2
+ class Script
3
+ attr_reader :name, :script, :sha
4
+
5
+ class << self
6
+ # @param path [Pathname]
7
+ # @return [RedisEval::Script]
8
+ def build_from_path(path)
9
+ name = path.basename(".*").to_s
10
+ script = File.read(path)
11
+ new(name, script)
12
+ end
13
+
14
+ def load(script)
15
+ redis.script(:load, script)
16
+ end
17
+
18
+ def flush
19
+ redis.script(:flush)
20
+ end
21
+
22
+ def redis
23
+ RedisEval.config.redis
24
+ end
25
+ end
26
+
27
+ def initialize(name, script)
28
+ @name = name
29
+ @script = script
30
+ @sha = Digest::SHA1.hexdigest(script)
31
+ end
32
+
33
+ def execute(keys = [], argv = [])
34
+ redis.evalsha(sha, Array(keys), Array(argv))
35
+ rescue Redis::CommandError => e
36
+ if e.message =~ /NOSCRIPT/
37
+ redis.eval(script, Array(keys), Array(argv))
38
+ else
39
+ raise
40
+ end
41
+ end
42
+
43
+ def exist?
44
+ redis.script(:exists, sha)
45
+ end
46
+
47
+ def redis
48
+ self.class.redis
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module RedisEval
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_eval/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "redis_eval"
8
+ spec.version = RedisEval::VERSION
9
+ spec.authors = ["i2bskn"]
10
+ spec.email = ["i2bskn@gmail.com"]
11
+
12
+ spec.summary = %q{Evaluate Lua scripts with Redis.}
13
+ spec.description = %q{Evaluate Lua scripts with Redis.}
14
+ spec.homepage = "https://github.com/i2bskn/redis_eval"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "redis"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "coveralls"
29
+ spec.add_development_dependency "simplecov"
30
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_eval
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - i2bskn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Evaluate Lua scripts with Redis.
112
+ email:
113
+ - i2bskn@gmail.com
114
+ executables:
115
+ - redis_eval_test
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".coveralls.yml"
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - exe/redis_eval_test
131
+ - lib/redis_eval.rb
132
+ - lib/redis_eval/configuration.rb
133
+ - lib/redis_eval/generator_methods.rb
134
+ - lib/redis_eval/script.rb
135
+ - lib/redis_eval/version.rb
136
+ - redis_eval.gemspec
137
+ homepage: https://github.com/i2bskn/redis_eval
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.5
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Evaluate Lua scripts with Redis.
161
+ test_files: []