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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02de993668b47710ed8674dd89e0f31df73b5fe10a0f54791cfede73927f6801
4
- data.tar.gz: bcc2a3e1916287a32029746e9878a3951adb6ca08a28e76d2eefb2cb713b1080
3
+ metadata.gz: b9fa95607e753fe460e409ec2640d53793fa43e547494a4bae92ea2a78755c68
4
+ data.tar.gz: f681a595089540a7054bf9af62de9b22e20aa4230230c0ee79c3d6cca60bca2d
5
5
  SHA512:
6
- metadata.gz: 35a58ec03084936782bf04ac7e634783d427861ce7a82fb7088ede9f162e8fd7b7d2acb387387454271e194e821709d6e1c6047a630556c2b6eb31fb44d5ee51
7
- data.tar.gz: 5b9a4e19f46ec7082baa56f0a6c48e9b10fd8b82d31bca08f3672463d53b760fc71a4f68d18e37dfd4abab29f397cd37444f8b3b5cce76eff6064228d03bf3ad
6
+ metadata.gz: 7be30fde174978206331f1cf0a783fc25d4efabe6e6e5c68d100239ce415344557c1218a104bb07b25b58cbd3ee31542a99ccb674caf5bf729abf0788b90782b
7
+ data.tar.gz: 89324f8480c0c63bce9be2ac99cf057e1c1db6e2dbc8aadb70ffc281018fa8e523550afbf1c13c38f0b100e836e3cdce25f246b471e61c972ef5148d8dfec1df
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  Metrics/AbcSize:
2
- Max: 20
2
+ Max: 30
3
3
 
4
4
  Style/Documentation:
5
5
  Exclude:
@@ -12,7 +12,7 @@ Metrics/LineLength:
12
12
  Max: 100
13
13
 
14
14
  Metrics/MethodLength:
15
- Max: 20
15
+ Max: 30
16
16
 
17
17
  Metrics/BlockLength:
18
- Max: 45
18
+ Max: 100
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bitwise_attribute (0.1.0)
4
+ bitwise_attribute (0.2.0)
5
5
  activesupport (~> 5.1.5)
6
6
 
7
7
  GEM
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
data/bin/console CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #!/usr/bin/env ruby
2
4
 
3
5
  require 'bundler/setup'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  lib = File.expand_path('lib', __dir__)
2
4
 
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BitwiseAttribute
2
4
  class ValuesList < Array
3
5
  def initialize(field, record, values)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BitwiseAttribute
2
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.2.0'
3
5
  end
@@ -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
- # Sets the constant <name.upcase>_MAPPING
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
- @mapping[name]
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(name, values)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitwise_attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Otero