bitwise_attribute 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -3
- data/Gemfile +2 -0
- data/Gemfile.lock +1 -1
- data/Rakefile +2 -0
- data/bin/console +2 -0
- data/bitwise_attribute.gemspec +2 -0
- data/lib/bitwise_attribute/values_list.rb +2 -0
- data/lib/bitwise_attribute/version.rb +3 -1
- data/lib/bitwise_attribute.rb +55 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9fa95607e753fe460e409ec2640d53793fa43e547494a4bae92ea2a78755c68
|
4
|
+
data.tar.gz: f681a595089540a7054bf9af62de9b22e20aa4230230c0ee79c3d6cca60bca2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7be30fde174978206331f1cf0a783fc25d4efabe6e6e5c68d100239ce415344557c1218a104bb07b25b58cbd3ee31542a99ccb674caf5bf729abf0788b90782b
|
7
|
+
data.tar.gz: 89324f8480c0c63bce9be2ac99cf057e1c1db6e2dbc8aadb70ffc281018fa8e523550afbf1c13c38f0b100e836e3cdce25f246b471e61c972ef5148d8dfec1df
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/bitwise_attribute.gemspec
CHANGED
data/lib/bitwise_attribute.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support'
|
2
4
|
require 'active_support/concern'
|
3
5
|
require 'active_support/core_ext'
|
@@ -14,14 +16,22 @@ module BitwiseAttribute
|
|
14
16
|
# Check if the values is an array or hash with valid values. Raise ArgumentError otherwise.
|
15
17
|
validate_values!(values)
|
16
18
|
|
17
|
-
|
18
|
-
mapping = build_mapping(name, values)
|
19
|
+
mapping = build_mapping(values)
|
19
20
|
|
20
21
|
@mapping ||= {}
|
21
22
|
@mapping[name] = mapping
|
22
23
|
|
24
|
+
# Class methods
|
23
25
|
define_singleton_method(name) do
|
24
|
-
|
26
|
+
mapping
|
27
|
+
end
|
28
|
+
|
29
|
+
define_singleton_method("with_#{name}") do |*vals|
|
30
|
+
where(column_name => bitwise_union(vals, name))
|
31
|
+
end
|
32
|
+
|
33
|
+
define_singleton_method("with_all_#{name}") do |*vals|
|
34
|
+
where(column_name => bitwise_intersection(vals, name))
|
25
35
|
end
|
26
36
|
|
27
37
|
define_method(name) do
|
@@ -45,7 +55,7 @@ module BitwiseAttribute
|
|
45
55
|
|
46
56
|
# Builds internal bitwise key-value mapping it add a zero value, needed for bits operations.
|
47
57
|
# Each sym get a power of 2 value
|
48
|
-
def build_mapping(
|
58
|
+
def build_mapping(values)
|
49
59
|
{}.tap do |hash|
|
50
60
|
if values.is_a?(Hash)
|
51
61
|
hash.merge!(values.sort_by { |_, value| value }.to_h)
|
@@ -67,6 +77,47 @@ module BitwiseAttribute
|
|
67
77
|
end
|
68
78
|
end
|
69
79
|
end
|
80
|
+
|
81
|
+
def mapping_for_name(name)
|
82
|
+
@mapping[name.to_sym]
|
83
|
+
end
|
84
|
+
|
85
|
+
def values_for_column(name)
|
86
|
+
mapping_for_name(name).values
|
87
|
+
end
|
88
|
+
|
89
|
+
def map_to_values(name, keys)
|
90
|
+
mapping = mapping_for_name(name)
|
91
|
+
|
92
|
+
keys.map { |key| mapping[key.to_sym] }.compact
|
93
|
+
end
|
94
|
+
|
95
|
+
def bitwise_union(keys, name)
|
96
|
+
mask = []
|
97
|
+
|
98
|
+
mapped = map_to_values(name, keys)
|
99
|
+
|
100
|
+
mapped.each do |mv|
|
101
|
+
values_for_column(name).each do |value|
|
102
|
+
mask << (mv | value)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
mask.uniq
|
107
|
+
end
|
108
|
+
|
109
|
+
def bitwise_intersection(keys, name)
|
110
|
+
mask = []
|
111
|
+
|
112
|
+
mapped = map_to_values(name, keys)
|
113
|
+
mapped = mapped.reduce(&:|)
|
114
|
+
|
115
|
+
values_for_column(name).each do |value|
|
116
|
+
mask << (value | mapped)
|
117
|
+
end
|
118
|
+
|
119
|
+
mask.uniq
|
120
|
+
end
|
70
121
|
end
|
71
122
|
|
72
123
|
private
|