tablesalt 0.8.3 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83ba239c7512c2a7cb481687f4441004fc6dda64e3a4108b5fd571b06f5df35a
4
- data.tar.gz: 533ed0e4c8f18a620f8f30cf2e76dc41bf58999db6a58a3b736d38441971a19a
3
+ metadata.gz: d4f0e11454d95394d0070ffa386056836628db24f0070be4524ea5cf70a17912
4
+ data.tar.gz: 1b27619517487c59c84324b3c8313755ad2274a15eecba5b8ff1e17355cae3e7
5
5
  SHA512:
6
- metadata.gz: 599e8a40a39446cd314f63481776a4b5fbc38ab804013839ca082ddc182a1dcdd463e68f7b9c5bbae2673c98770e43ba4b5ba84a987db4720fc15711c9cdb315
7
- data.tar.gz: 64b3b201336719da82a739d327d64e7e4b77f09bfd8594f6a6bf021a914d4ff034d545331a46f0e1a7d373bb95550d5220f8661d28c2cbe6f3b13a2fb618d016
6
+ metadata.gz: 92d11b14fe9b71135e9b75e23bc885070854fac486982be65802aec6b2462b9a724fc75d5e5345985b0cd7e973cd213ecfa285c5089827632759501b3b3773c2
7
+ data.tar.gz: 11a193ee36b492d3c3167cb9ad6668de036b99dd8341924c05b47ec7e63d847b53855d83e416c8ee9cb146cd8350b015ef9a612afef780621ef9e240d05a442e
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Accessors allow for the retrieval of data from the Hash.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Accessors
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ delegate :assoc, :compact, :dig, :fetch_values, :flatten, :key, :rassoc, :rehash, to: :to_h
11
+ end
12
+
13
+ def [](field)
14
+ hget(redis_key, field) || default(field)
15
+ end
16
+
17
+ def fetch(field, default = nil)
18
+ value = self[field]
19
+ return value if value.present?
20
+ return yield(field) if block_given?
21
+ return default unless default.nil?
22
+
23
+ raise KeyError, "key not found: \"#{field}\""
24
+ end
25
+
26
+ def keys
27
+ hkeys(redis_key)
28
+ end
29
+
30
+ def length
31
+ hlen(redis_key)
32
+ end
33
+ alias_method :size, :length
34
+
35
+ def values
36
+ hvals(redis_key)
37
+ end
38
+
39
+ def values_at(*fields)
40
+ hmget(*fields.flatten.unshift(redis_key))
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Callbacks provide an extensible mechanism for hooking into a RedisHash.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Callbacks
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ include ActiveSupport::Callbacks
11
+ define_callbacks :initialize, :reload
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Comparisons encompasses equality and set inclusion.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Comparisons
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ delegate :<, :<=, :>, :>=, :compare_by_identity, :compare_by_identity?, to: :to_h
11
+ end
12
+
13
+ def eql?(other)
14
+ other.hash == hash
15
+ end
16
+ alias_method :==, :eql?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Converters allow for other objects to be coerced into a RedisHash.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Converters
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ def [](*arguments)
11
+ options = block_given? ? yield({}) : {}
12
+ new(**options).merge!(Hash[*arguments])
13
+ end
14
+
15
+ def try_convert(object, &block)
16
+ return object if object.is_a?(RedisHash)
17
+
18
+ self[object, &block] if object.respond_to?(:to_hash)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A RedisHash is a wrapper around a h<key> stored in Redis.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Core
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ attr_reader :redis_key, :redis
11
+ delegate :del, :hdel, :hexists, :hget, :hgetall, :hkeys, :hlen, :hmget, :hmset, :hset, :hvals, to: :redis
12
+
13
+ private
14
+
15
+ def set_default(default, &block)
16
+ self.default = default if default.present?
17
+ self.default_proc = block if block_given?
18
+ end
19
+ end
20
+
21
+ def initialize(default = nil, redis_key: SecureRandom.hex, redis: Redis.new, &block)
22
+ raise ArgumentError, "cannot specify both block and static default" if block_given? && default.present?
23
+
24
+ run_callbacks(:initialize) do
25
+ set_default(default, &block)
26
+ @redis_key = redis_key
27
+ @redis = redis
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A Default allows a static or procedurally generated value to be returned on failed lookups.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Default
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ attr_reader :default_proc
11
+
12
+ private
13
+
14
+ def to_default(field = nil, allow_nil_field: true)
15
+ @default.presence || (default_proc&.call(self, field) if !field.nil? || allow_nil_field)
16
+ end
17
+
18
+ def validate_proc(proc)
19
+ raise TypeError, "wrong default_proc type #{proc.class.name} (expected Proc)" unless proc.is_a? Proc
20
+
21
+ validate_lambda_arity(proc.arity) if proc.lambda?
22
+ end
23
+
24
+ def validate_lambda_arity(arity)
25
+ raise TypeError, "default_proc takes two arguments (2 for #{arity})" if arity >= 0 && arity != 2
26
+ end
27
+ end
28
+
29
+ def default=(value)
30
+ @default = value
31
+ @default_proc = nil
32
+ end
33
+
34
+ def default_proc=(value)
35
+ validate_proc(value) unless value.nil?
36
+
37
+ @default = nil
38
+ @default_proc = value
39
+ end
40
+
41
+ def default(field = nil)
42
+ to_default(field, allow_nil_field: false)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Deletions allow for the removal of data from the Hash.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Deletions
7
+ extend ActiveSupport::Concern
8
+
9
+ def clear
10
+ del(redis_key) and {}
11
+ end
12
+
13
+ def delete(field)
14
+ value = self[field]
15
+ result = hdel(redis_key, field)
16
+ (result == 0 && block_given?) ? yield(field) : value
17
+ end
18
+
19
+ def shift
20
+ return to_default if empty?
21
+
22
+ field = keys.first
23
+ [ field, delete(field) ]
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Enumerators allow for the traversal and manipulation of data in the Hash.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Enumerators
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ delegate :each, :each_pair, :each_key, :each_value, :reject, :select, :transform_values, to: :to_h
11
+ end
12
+
13
+ def delete_if
14
+ return enum_for(__method__) unless block_given?
15
+
16
+ each { |field, value| delete(field) if yield(field, value) }
17
+
18
+ to_h
19
+ end
20
+
21
+ def keep_if
22
+ return enum_for(__method__) unless block_given?
23
+
24
+ delete_if { |field, value| !yield(field, value) }
25
+ end
26
+
27
+ def reject!(&block)
28
+ return enum_for(__method__) unless block_given?
29
+
30
+ original = to_h
31
+ delete_if(&block)
32
+ current = to_h
33
+
34
+ (original == current) ? nil : current
35
+ end
36
+
37
+ def select!
38
+ return enum_for(__method__) unless block_given?
39
+
40
+ reject! { |*arguments| !yield(*arguments) }
41
+ end
42
+
43
+ def transform_values!
44
+ return enum_for(__method__) unless block_given?
45
+
46
+ each { |field, value| store(field, yield(value)) }
47
+
48
+ to_h
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Identity relates to the specific object instance.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Identity
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ delegate :inspect, :to_proc, :to_s, to: :to_h
11
+ end
12
+
13
+ def hash
14
+ { redis_id: redis.id, redis_key: redis_key }.hash
15
+ end
16
+
17
+ def to_hash
18
+ to_h
19
+ end
20
+
21
+ def to_h
22
+ hgetall(redis_key)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Insertions allow for the addition of data into the RedisHash.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Insertions
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ delegate :merge, to: :to_h
11
+ end
12
+
13
+ def merge!(other_hash)
14
+ hmset(*other_hash.to_a.unshift(redis_key))
15
+ self
16
+ end
17
+ alias_method :update, :merge!
18
+
19
+ def store(field, value)
20
+ hset(redis_key, field, value)
21
+ end
22
+ alias_method :[]=, :store
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Mutations allow for the manipulation of data in the Hash.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Mutations
7
+ extend ActiveSupport::Concern
8
+
9
+ def compact!
10
+ delete_if { |_, value| value.blank? }
11
+ end
12
+
13
+ def invert
14
+ inversion = to_h.invert
15
+ clear
16
+ inversion.each { |key, value| self[key] = value }
17
+ end
18
+
19
+ def replace(other_hash)
20
+ clear and merge!(other_hash)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Predicates enable querying the Hash for data.
4
+ module Tablesalt
5
+ module RedisHash
6
+ module Predicates
7
+ extend ActiveSupport::Concern
8
+
9
+ def any?(&block)
10
+ return length > 0 unless block_given?
11
+
12
+ to_h.any?(&block)
13
+ end
14
+
15
+ def empty?
16
+ length == 0
17
+ end
18
+
19
+ def include?(field)
20
+ hexists(redis_key, field)
21
+ end
22
+ alias_method :has_key?, :include?
23
+ alias_method :key?, :include?
24
+ alias_method :member?, :include?
25
+
26
+ def value?(value)
27
+ values.include? value
28
+ end
29
+ alias_method :has_value?, :value?
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "redis_hash/default"
4
+ require_relative "redis_hash/callbacks"
5
+ require_relative "redis_hash/core"
6
+ require_relative "redis_hash/identity"
7
+ require_relative "redis_hash/accessors"
8
+ require_relative "redis_hash/comparisons"
9
+ require_relative "redis_hash/predicates"
10
+ require_relative "redis_hash/insertions"
11
+ require_relative "redis_hash/deletions"
12
+ require_relative "redis_hash/enumerators"
13
+ require_relative "redis_hash/mutations"
14
+ require_relative "redis_hash/converters"
15
+
16
+ module Tablesalt
17
+ class RedisHashBase
18
+ include Technologic
19
+ include Tablesalt::RedisHash::Default
20
+ include Tablesalt::RedisHash::Callbacks
21
+ include Tablesalt::RedisHash::Core
22
+ include Tablesalt::RedisHash::Identity
23
+ include Tablesalt::RedisHash::Accessors
24
+ include Tablesalt::RedisHash::Comparisons
25
+ include Tablesalt::RedisHash::Predicates
26
+ include Tablesalt::RedisHash::Insertions
27
+ include Tablesalt::RedisHash::Deletions
28
+ include Tablesalt::RedisHash::Enumerators
29
+ include Tablesalt::RedisHash::Mutations
30
+ include Tablesalt::RedisHash::Converters
31
+ end
32
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Tablesalt
4
4
  # This constant is managed by spicerack
5
- VERSION = "0.8.3"
5
+ VERSION = "0.8.4"
6
6
  end
data/lib/tablesalt.rb CHANGED
@@ -1,6 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support"
4
+
5
+ require "redis"
6
+
7
+ require "technologic"
8
+
3
9
  require "tablesalt/version"
4
10
 
5
- module Tablesalt
6
- end
11
+ require "tablesalt/redis_hash_base"
12
+
13
+ module Tablesalt; end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablesalt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Minneti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2019-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: technologic
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
13
55
  description: A package of helpers that introduce some conventions and convenience
14
56
  for common behaviors
15
57
  email:
@@ -21,6 +63,19 @@ files:
21
63
  - LICENSE.txt
22
64
  - README.md
23
65
  - lib/tablesalt.rb
66
+ - lib/tablesalt/redis_hash/accessors.rb
67
+ - lib/tablesalt/redis_hash/callbacks.rb
68
+ - lib/tablesalt/redis_hash/comparisons.rb
69
+ - lib/tablesalt/redis_hash/converters.rb
70
+ - lib/tablesalt/redis_hash/core.rb
71
+ - lib/tablesalt/redis_hash/default.rb
72
+ - lib/tablesalt/redis_hash/deletions.rb
73
+ - lib/tablesalt/redis_hash/enumerators.rb
74
+ - lib/tablesalt/redis_hash/identity.rb
75
+ - lib/tablesalt/redis_hash/insertions.rb
76
+ - lib/tablesalt/redis_hash/mutations.rb
77
+ - lib/tablesalt/redis_hash/predicates.rb
78
+ - lib/tablesalt/redis_hash_base.rb
24
79
  - lib/tablesalt/version.rb
25
80
  homepage: https://www.freshly.com
26
81
  licenses: