active_redis_db 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e3ba32a859bdf1ace47fb5459e376bd615774825ad973027811594cb70563ef9
4
+ data.tar.gz: 7fd6bf1312659c4101b556e0998d2235e807abbb3bad7d7c3c453074903c5f87
5
+ SHA512:
6
+ metadata.gz: 6f5362925cb17b6660f4b48ee740f9a59381adf71e86e7c56a092faa184fd90345d5409ce1650dc7f2dac504ee5831b05461a0546142e7eda924d8fd9a0c4c15
7
+ data.tar.gz: b4102f7a17155cb8b306ee65ca31efd263fd95e0c58c63df5182d47e66bc85a5e05c073989a5156649fc6033fb28af8ef4ca4ff5d37eeaf6201e9a4d9ba39087
data/.DS_Store ADDED
Binary file
data/.fasterer.yml ADDED
@@ -0,0 +1,19 @@
1
+ speedups:
2
+ rescue_vs_respond_to: true
3
+ module_eval: true
4
+ shuffle_first_vs_sample: true
5
+ for_loop_vs_each: true
6
+ each_with_index_vs_while: false
7
+ map_flatten_vs_flat_map: true
8
+ reverse_each_vs_reverse_each: true
9
+ select_first_vs_detect: true
10
+ sort_vs_sort_by: true
11
+ fetch_with_argument_vs_block: true
12
+ keys_each_vs_each_key: true
13
+ hash_merge_bang_vs_hash_brackets: true
14
+ block_vs_symbol_to_proc: true
15
+ proc_call_vs_yield: true
16
+ gsub_vs_tr: true
17
+ select_last_vs_reverse_detect: true
18
+ getter_vs_attr_reader: true
19
+ setter_vs_attr_writer: true
data/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.reek.yml ADDED
@@ -0,0 +1,29 @@
1
+ ---
2
+ detectors:
3
+ Attribute:
4
+ enabled: false
5
+ BooleanParameter:
6
+ enabled: false
7
+ ClassVariable:
8
+ enabled: false
9
+ ControlParameter:
10
+ enabled: false
11
+ DataClump:
12
+ enabled: false
13
+ DuplicateMethodCall:
14
+ exclude:
15
+ - 'Base#metamorph'
16
+ FeatureEnvy:
17
+ enabled: false
18
+ IrresponsibleModule:
19
+ enabled: false
20
+ LongParameterList:
21
+ enabled: false
22
+ NilCheck:
23
+ enabled: false
24
+ RepeatedConditional:
25
+ enabled: false
26
+ TooManyMethods:
27
+ enabled: false
28
+ TooManyStatements:
29
+ max_statements: 10
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --backtrace
2
+ --color
3
+ --format progress
4
+ --order random
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ TargetRubyVersion: 2.4
5
+ Exclude:
6
+ - 'spec/**/**/*'
7
+ Layout/EmptyLinesAroundBlockBody:
8
+ Enabled: false
9
+ Layout/EmptyLinesAroundClassBody:
10
+ Enabled: false
11
+ Layout/EmptyLinesAroundModuleBody:
12
+ Enabled: false
13
+ LineLength:
14
+ Max: 100
15
+ Lint/ScriptPermission:
16
+ Enabled: false
17
+ Metrics/ClassLength:
18
+ Enabled: false
19
+ Style/ClassAndModuleChildren:
20
+ EnforcedStyle: compact
21
+ Style/ClassVars:
22
+ Enabled: false
23
+ Style/Documentation:
24
+ Enabled: false
25
+ Style/ExpandPathArguments:
26
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ before_install:
2
+ - sudo apt-get install libxml2-dev
3
+ cache: bundler
4
+ language: ruby
5
+ notifications:
6
+ email:
7
+ recipients:
8
+ - j.gomez@drexed.com
9
+ on_failure: change
10
+ on_success: never
11
+ rvm:
12
+ - ruby-head
13
+ script: 'bundle exec rake'
@@ -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, 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,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in active_redis_db.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # ActiveRedisDB
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/active_redis_db.svg)](http://badge.fury.io/rb/active_redis_db)
4
+ [![Build Status](https://travis-ci.org/drexed/active_redis_db.svg?branch=master)](https://travis-ci.org/drexed/active_redis_db)
5
+
6
+ ActiveRedisDB is a library for object ruby mapping of different databases.
7
+
8
+ **Supported:**
9
+ * Redis
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'active_redis_db'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install active_redis_db
26
+
27
+ ## Table of Contents
28
+
29
+ * [Redis](#redis)
30
+
31
+ ## Redis
32
+
33
+ **Configuration:**
34
+
35
+ **Options:**
36
+ * client: add a custom Redis client
37
+
38
+ `rails generate active_redis_db:install` will generate the following file:
39
+ `../config/initalizers/active_redis_db.rb`
40
+
41
+ ```ruby
42
+ if defined?(Redis)
43
+ ActiveRedisDB.configure do |config|
44
+ config.client = Redis.new(host: '10.0.1.1', port: 6380, db: 15)
45
+ end
46
+ end
47
+ ```
48
+
49
+ **Usage:**
50
+
51
+ **Commands:**
52
+ * Geo
53
+ * Hash
54
+ * HyperLogLog
55
+ * Key
56
+ * List
57
+ * PubSub
58
+ * Script
59
+ * Set
60
+ * Sorted Set
61
+ * String
62
+ * Transaction
63
+
64
+ ```ruby
65
+ ActiveRedisDB::String.create(:month, '01')
66
+ ActiveRedisDB::String.find(:month) #=> '01'
67
+ ActiveRedisDB::String.evaluate.find(:month) #=> 1
68
+
69
+ ActiveRedisDB::List.create(:user_1, { id: 32123, name: 'James Dean', username: 'alpha123' })
70
+ ActiveRedisDB::List.find(:user_1) #=> { id: '32123', name: 'James Dean', username: 'alpha123' }
71
+ ActiveRedisDB::List.evaluate.find(:user_1) #=> { id: 32123, name: 'James Dean', username: 'alpha123' }
72
+ ```
73
+
74
+ **Rake:**
75
+
76
+ **Options:**
77
+ * reset: reset current database
78
+ * reset_all: reset all databases
79
+
80
+ `rake db:redis:reset` and `rake db:redis:reset_all`
81
+
82
+ ## Contributing
83
+
84
+ Your contribution is welcome.
85
+
86
+ 1. Fork it
87
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
88
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
89
+ 4. Push to the branch (`git push origin my-new-feature`)
90
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ task default: :spec
6
+
7
+ import 'lib/tasks/redis.rake'
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'active_redis_db/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'active_redis_db'
9
+ spec.version = ActiveRedisDB::VERSION
10
+ spec.authors = ['Juan Gomez']
11
+ spec.email = ['j.gomez@drexed.com']
12
+
13
+ spec.summary = 'Gem for Redis activerecord like interface.'
14
+ spec.description = 'Redis activerecord like interface.'
15
+ spec.homepage = 'http://drexed.github.io/active_redis_db'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = %w[lib]
22
+
23
+ spec.add_runtime_dependency 'rails'
24
+ spec.add_runtime_dependency 'redis'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'coveralls'
28
+ # spec.add_development_dependency 'fakeredis'
29
+ spec.add_development_dependency 'fasterer'
30
+ spec.add_development_dependency 'generator_spec'
31
+ spec.add_development_dependency 'rake'
32
+ spec.add_development_dependency 'reek'
33
+ spec.add_development_dependency 'rspec'
34
+ spec.add_development_dependency 'rubocop'
35
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'active_redis_db'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require 'pry'
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
data/bin/rake ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rake' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath)
11
+
12
+ require 'rubygems'
13
+ require 'bundler/setup'
14
+
15
+ load Gem.bin_path('rake', 'rake')
data/bin/setup ADDED
@@ -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
data/lib/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Base
4
+ class << self
5
+
6
+ @@evaluate = false
7
+
8
+ def client(new_client = nil)
9
+ new_client || ActiveRedisDB.configuration.client
10
+ end
11
+
12
+ def evaluate(value = true)
13
+ @@evaluate = value
14
+ self
15
+ end
16
+
17
+ private
18
+
19
+ def evaluate?
20
+ value = @@evaluate
21
+ @@evaluate = false
22
+ value
23
+ end
24
+
25
+ def append?(order)
26
+ order.to_s == 'append'
27
+ end
28
+
29
+ # rubocop:disable Security/Eval
30
+ def metaform(value)
31
+ eval(value.to_s)
32
+ rescue StandardError
33
+ value
34
+ end
35
+ # rubocop:enable Security/Eval
36
+
37
+ def metaform_array(datum)
38
+ datum.map { |val| metaform(val) }
39
+ end
40
+
41
+ def metaform_hash(datum)
42
+ datum.each { |key, val| datum[key] = metaform(val) }
43
+ end
44
+
45
+ def metamorph_array(datum)
46
+ case datum.first.class.name
47
+ when 'Array' then datum.map { |arr| metaform_array(arr) }
48
+ when 'Hash' then datum.map { |hsh| metaform_hash(hsh) }
49
+ else metaform_array(datum)
50
+ end
51
+ end
52
+
53
+ def metamorph(datum)
54
+ case datum.class.name
55
+ when 'Array' then metamorph_array(datum)
56
+ when 'Hash' then metaform_hash(datum)
57
+ else metaform(datum)
58
+ end
59
+ end
60
+
61
+ def metatransform(datum)
62
+ return if datum.empty?
63
+
64
+ evaluate? ? metamorph(datum) : datum
65
+ end
66
+
67
+ def milliseconds?(format)
68
+ format.to_s == 'milliseconds'
69
+ end
70
+
71
+ def normalize_key(key)
72
+ key.to_s
73
+ end
74
+
75
+ def prepend?(order)
76
+ order.to_s == 'prepend'
77
+ end
78
+
79
+ def seconds?(format)
80
+ format.to_s == 'seconds'
81
+ end
82
+
83
+ def stringify_keys(value)
84
+ value.map { |key, _| key.to_s }
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Style/ClassAndModuleChildren
4
+ begin
5
+ require 'fakeredis'
6
+ rescue LoadError
7
+ require 'redis'
8
+ end
9
+
10
+ module ActiveRedisDB
11
+ class Configuration
12
+
13
+ attr_accessor :client
14
+
15
+ def initialize
16
+ @client = ::Redis.new
17
+ end
18
+
19
+ end
20
+
21
+ def self.configuration
22
+ @configuration ||= Configuration.new
23
+ end
24
+
25
+ def self.configuration=(config)
26
+ @configuration = config
27
+ end
28
+
29
+ def self.configure
30
+ yield(configuration)
31
+ end
32
+
33
+ end
34
+ # rubocop:enable Style/ClassAndModuleChildren
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Connection < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def authenticate(password)
7
+ client.auth(password)
8
+ end
9
+
10
+ def connected?
11
+ client.connected?
12
+ end
13
+
14
+ def database(index)
15
+ client.select(index)
16
+ end
17
+
18
+ def database_id
19
+ client.database_id
20
+ end
21
+
22
+ def database_size
23
+ client.dbsize
24
+ end
25
+
26
+ def debug(*args)
27
+ client.debug(args)
28
+ end
29
+
30
+ def disconnect
31
+ client.disconnect
32
+ end
33
+
34
+ def echo(message)
35
+ client.echo(message)
36
+ end
37
+
38
+ def flush
39
+ client.flushdb
40
+ end
41
+
42
+ def flush_all
43
+ client.flushall
44
+ end
45
+
46
+ def info
47
+ client.info
48
+ end
49
+
50
+ def ping
51
+ client.ping
52
+ end
53
+
54
+ def quit
55
+ client.quit
56
+ end
57
+
58
+ def reconnect
59
+ client.reconnect
60
+ end
61
+
62
+ def rewrite_aof
63
+ client.bgrewriteaof
64
+ end
65
+
66
+ def save
67
+ client.bgsave
68
+ end
69
+
70
+ def saved_at
71
+ client.lastsave
72
+ end
73
+
74
+ def shutdown
75
+ client.shutdown
76
+ end
77
+
78
+ def slave_of(host, port)
79
+ client.slaveof(host, port)
80
+ end
81
+
82
+ def slowlog(command, length = nil)
83
+ client.slowlog(command, length)
84
+ end
85
+
86
+ def synchronize
87
+ client.synchronize
88
+ end
89
+
90
+ def time
91
+ client.time
92
+ end
93
+
94
+ def with_reconnect(&block)
95
+ client.with_reconnect(true, &block)
96
+ end
97
+
98
+ def without_reconnect(&block)
99
+ client.with_reconnect(false, &block)
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Geo < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def create
7
+ # TODO
8
+ end
9
+
10
+ def hash
11
+ # TODO
12
+ end
13
+
14
+ def position
15
+ # TODO
16
+ end
17
+
18
+ def distance
19
+ # TODO
20
+ end
21
+
22
+ def radius
23
+ # TODO
24
+ end
25
+
26
+ def radius_member
27
+ # TODO
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Hash < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def find(key, field)
7
+ value = client.hget(normalize_key(key), field)
8
+ value = metamorph(value) if evaluate?
9
+ value
10
+ end
11
+
12
+ def find_each(key, *args)
13
+ value = client.hmget(normalize_key(key), args)
14
+ value = metamorph(value) if evaluate?
15
+ value
16
+ end
17
+
18
+ def all(key)
19
+ value = client.hgetall(normalize_key(key))
20
+ value = metamorph(value) if evaluate?
21
+ value
22
+ end
23
+
24
+ def keys(key)
25
+ value = client.hkeys(normalize_key(key))
26
+ value = metamorph(value) if evaluate?
27
+ value
28
+ end
29
+
30
+ def values(key)
31
+ value = client.hvals(normalize_key(key))
32
+ value = metamorph(value) if evaluate?
33
+ value
34
+ end
35
+
36
+ def value_length(key, field)
37
+ client.hstrlen(normalize_key(key), field)
38
+ end
39
+
40
+ def count(key)
41
+ client.hlen(normalize_key(key))
42
+ end
43
+
44
+ def exists?(key, field)
45
+ client.hexists(normalize_key(key), field)
46
+ end
47
+
48
+ def create(key, field, value)
49
+ client.hset(normalize_key(key), field, value)
50
+ end
51
+
52
+ def create!(key, field, value)
53
+ client.hsetnx(normalize_key(key), field, value)
54
+ end
55
+
56
+ def create_each(key, *args)
57
+ client.hmset(normalize_key(key), args)
58
+ end
59
+
60
+ def increment(key, field, value)
61
+ normalized_key = normalize_key(key)
62
+
63
+ if value.is_a?(Float)
64
+ client.hincrbyfloat(normalized_key, field, value)
65
+ else
66
+ client.hincrby(normalized_key, field, value)
67
+ end
68
+ end
69
+
70
+ def destroy(key, *args)
71
+ client.hdel(normalize_key(key), args)
72
+ end
73
+
74
+ def scan(key, cursor, opts = {})
75
+ client.hdel(normalize_key(key), cursor, opts)
76
+ end
77
+
78
+ end
79
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::HyperLogLog < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def create(key, member)
7
+ client.pfadd(normalize_key(key), member)
8
+ end
9
+
10
+ def count(*args)
11
+ client.pfcount(args)
12
+ end
13
+
14
+ def merge(key, *keys)
15
+ client.pfmerge(normalize_key(key), keys)
16
+ end
17
+
18
+ end
19
+ end