attr_bitwise 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +36 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +45 -0
- data/LICENSE +21 -0
- data/README.md +49 -0
- data/Rakefile +7 -0
- data/attr_bitwise.gemspec +26 -0
- data/lib/attr_bitwise/version.rb +3 -0
- data/lib/attr_bitwise.rb +230 -0
- data/spec/attr_bitwise_spec.rb +163 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c7bc7f15d2c51cdaa8e8fc01987c4b3e500d89a
|
4
|
+
data.tar.gz: fe4fbf205f6c998ea684a45f56e61a833623e1fc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5aaa7af4fc47354187a8c654f296642d7a0699ebac3676d5ef6891895b730da09b674f8dba5f639d9b7e003605d85f5a87fb780da880f29e8531a562dc809866
|
7
|
+
data.tar.gz: b70fcaf5b88f2eaea060277a4860c1e018a714002ddcf7fb76f5d33fb4a5a99e5c30a08f42acad45ce4b2c44cb6c7ca50a613c61b7a1d07a9f207e2da5460f83
|
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
## Specific to RubyMotion:
|
14
|
+
.dat*
|
15
|
+
.repl_history
|
16
|
+
build/
|
17
|
+
|
18
|
+
## Documentation cache and generated files:
|
19
|
+
/.yardoc/
|
20
|
+
/_yardoc/
|
21
|
+
/doc/
|
22
|
+
/rdoc/
|
23
|
+
|
24
|
+
## Environment normalization:
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
# Gemfile.lock
|
32
|
+
# .ruby-version
|
33
|
+
# .ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
attr_bitwise (0.0.1)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (4.2.5.1)
|
11
|
+
i18n (~> 0.7)
|
12
|
+
json (~> 1.7, >= 1.7.7)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
diff-lcs (1.2.5)
|
17
|
+
i18n (0.7.0)
|
18
|
+
json (1.8.3)
|
19
|
+
minitest (5.8.4)
|
20
|
+
rake (10.5.0)
|
21
|
+
rspec (3.4.0)
|
22
|
+
rspec-core (~> 3.4.0)
|
23
|
+
rspec-expectations (~> 3.4.0)
|
24
|
+
rspec-mocks (~> 3.4.0)
|
25
|
+
rspec-core (3.4.3)
|
26
|
+
rspec-support (~> 3.4.0)
|
27
|
+
rspec-expectations (3.4.0)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.4.0)
|
30
|
+
rspec-mocks (3.4.1)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.4.0)
|
33
|
+
rspec-support (3.4.1)
|
34
|
+
thread_safe (0.3.5)
|
35
|
+
tzinfo (1.2.2)
|
36
|
+
thread_safe (~> 0.1)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
attr_bitwise!
|
43
|
+
bundler (~> 1.6)
|
44
|
+
rake
|
45
|
+
rspec
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Charly POLY
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# `attr_bitwise`
|
2
|
+
Bitwise attribute for ruby class and Rails model
|
3
|
+
|
4
|
+
```ruby
|
5
|
+
# Helper to define a bits based value on a Rails model attribute
|
6
|
+
# this helper expose a set of methods to make bitwise operations
|
7
|
+
#
|
8
|
+
#
|
9
|
+
# Usage :
|
10
|
+
# attr_bitwise :<name>, [column_name: <column_name>,] mapping: <values_sym>
|
11
|
+
#
|
12
|
+
# Example
|
13
|
+
# class MyModel < ActiveRecord::Base
|
14
|
+
# include BitwiseUtils
|
15
|
+
#
|
16
|
+
# attr_bitwise :payment_types, mapping: [:slots, :credits]
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Will define the following high-level methods :
|
20
|
+
# - Class#payment_types => [<Symbol>, ...]
|
21
|
+
# - Class#payment_type?(value_or_sym) => Boolean
|
22
|
+
# - Class#add_payment_type(value_or_sym) => Fixnum
|
23
|
+
# - Class#remove_payment_type(value_or_sym) => Fixnum
|
24
|
+
#
|
25
|
+
# Will define the following low-level methods :
|
26
|
+
# - Class.to_bitwise_values(object, name) => [<Fixnum>, ...]
|
27
|
+
# - Class#payment_types_union([Fixnum, ..]) => [Fixnum, ..]
|
28
|
+
# - Class.bitwise_union([Fixnum, ..], name) => [Fixnum, ..]
|
29
|
+
# - Class#payment_types_intersection([Fixnum, ..]) => [Fixnum, ..]
|
30
|
+
# - Class.bitwise_intersection([Fixnum, ..], name) => [Fixnum, ..]
|
31
|
+
# - Class#payment_types_mapping => Hash
|
32
|
+
#
|
33
|
+
```
|
34
|
+
|
35
|
+
|
36
|
+
## Install
|
37
|
+
|
38
|
+
|
39
|
+
### Inline
|
40
|
+
|
41
|
+
- `gem install bitwise_attr`
|
42
|
+
|
43
|
+
### Gemfile
|
44
|
+
|
45
|
+
- `gem 'bitwise_attr'`
|
46
|
+
|
47
|
+
|
48
|
+
----------------------------------------
|
49
|
+
Maintainers : @wittydeveloper and @FSevaistre
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "attr_bitwise/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "attr_bitwise"
|
8
|
+
spec.version = AttrBitwise::VERSION
|
9
|
+
spec.authors = ["Charly POLY"]
|
10
|
+
spec.email = ["cpoly55@gmail.com"]
|
11
|
+
spec.summary = %q{Bitwise attribute for ruby class and Rails model}
|
12
|
+
spec.description = %q{Bitwise attribute for ruby class and Rails model}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "activesupport"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
data/lib/attr_bitwise.rb
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "active_support/concern"
|
3
|
+
require "active_support/core_ext"
|
4
|
+
require "attr_bitwise/version"
|
5
|
+
|
6
|
+
# Helper to define a bits based value on a Rails model attribute
|
7
|
+
# this helper expose a set of methods to make bitwise operations
|
8
|
+
#
|
9
|
+
#
|
10
|
+
# Usage :
|
11
|
+
# attr_bitwise :<name>, [column_name: <column_name>,] mapping: <values_sym>
|
12
|
+
#
|
13
|
+
# Example
|
14
|
+
# class MyModel < ActiveRecord::Base
|
15
|
+
# include BitwiseUtils
|
16
|
+
#
|
17
|
+
# attr_bitwise :payment_types, mapping: [:slots, :credits]
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# Will define the following high-level methods :
|
21
|
+
# - Class#payment_types => [<Symbol>, ...]
|
22
|
+
# - Class#payment_type?(value_or_sym) => Boolean
|
23
|
+
# - Class#add_payment_type(value_or_sym) => Fixnum
|
24
|
+
# - Class#remove_payment_type(value_or_sym) => Fixnum
|
25
|
+
#
|
26
|
+
# Will define the following low-level methods :
|
27
|
+
# - Class.to_bitwise_values(object, name) => [<Fixnum>, ...]
|
28
|
+
# - Class#payment_types_union([Fixnum, ..]) => [Fixnum, ..]
|
29
|
+
# - Class.bitwise_union([Fixnum, ..], name) => [Fixnum, ..]
|
30
|
+
# - Class#payment_types_intersection([Fixnum, ..]) => [Fixnum, ..]
|
31
|
+
# - Class.bitwise_intersection([Fixnum, ..], name) => [Fixnum, ..]
|
32
|
+
# - Class#payment_types_mapping => Hash
|
33
|
+
#
|
34
|
+
# More details in methods definition
|
35
|
+
module AttrBitwise
|
36
|
+
extend ActiveSupport::Concern
|
37
|
+
|
38
|
+
# ClassMethods
|
39
|
+
module ClassMethods
|
40
|
+
######################
|
41
|
+
# public class methods
|
42
|
+
######################
|
43
|
+
|
44
|
+
# Usage :
|
45
|
+
# attr_bitwise :payment_types, mapping: [:slots, :credits],
|
46
|
+
# column_name: 'payment_types_value'
|
47
|
+
#
|
48
|
+
def attr_bitwise(name, column_name: nil, mapping:)
|
49
|
+
column_name = "#{name}_value" unless column_name.present?
|
50
|
+
|
51
|
+
# mask attribute_accessor (internals)
|
52
|
+
define_method('value') { send(column_name) }
|
53
|
+
define_method('value=') { |value| send("#{column_name}=", value) }
|
54
|
+
|
55
|
+
# build mapping
|
56
|
+
bitwise_mapping = build_mapping(mapping, name)
|
57
|
+
|
58
|
+
# mask to symbols helper
|
59
|
+
define_method("#{name}") { send(:value_getter, bitwise_mapping) }
|
60
|
+
|
61
|
+
define_method("#{name}=") do |values_or_symbols_array|
|
62
|
+
unless values_or_symbols_array.is_a?(Array)
|
63
|
+
values_or_symbols_array = [values_or_symbols_array]
|
64
|
+
end
|
65
|
+
send(:value_setter, values_or_symbols_array, bitwise_mapping)
|
66
|
+
end
|
67
|
+
|
68
|
+
# masks symbol presence
|
69
|
+
define_method("#{name.to_s.singularize}?") do |value_or_symbol|
|
70
|
+
send(:value?, force_to_bitwise_value(value_or_symbol, bitwise_mapping))
|
71
|
+
end
|
72
|
+
|
73
|
+
# add value to mask
|
74
|
+
define_method("add_#{name.to_s.singularize}") do |value_or_symbol|
|
75
|
+
send(:add_value, force_to_bitwise_value(value_or_symbol, bitwise_mapping))
|
76
|
+
end
|
77
|
+
|
78
|
+
# remove value from mask
|
79
|
+
define_method("remove_#{name.to_s.singularize}") do |value_or_symbol|
|
80
|
+
send(:remove_value, force_to_bitwise_value(value_or_symbol, bitwise_mapping))
|
81
|
+
end
|
82
|
+
|
83
|
+
# compute values union against mask
|
84
|
+
define_method("#{name}_union") do |*mixed_array|
|
85
|
+
self.class.bitwise_union(*mixed_array, name)
|
86
|
+
end
|
87
|
+
|
88
|
+
# compute values intersection against mask
|
89
|
+
define_method("#{name}_intersection") do |*mixed_array|
|
90
|
+
self.class.bitwise_intersection(*mixed_array, name)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# given a payment_values array, return a possible matches
|
95
|
+
# for a union
|
96
|
+
#
|
97
|
+
# with PAYMENT_TYPES_MAPPING = { credits: 0b001, slots: 0b010, paypal: 0b100 }
|
98
|
+
# see http://www.calleerlandsson.com/2015/02/16/flags-bitmasks-and-unix-file-system-permissions-in-ruby/
|
99
|
+
#
|
100
|
+
# bitwise_union(:slots, :credits, 'payment_types') => [0b011, 0b111]
|
101
|
+
def bitwise_union(*mixed_array, name)
|
102
|
+
values_array = mixed_array.map { |v| to_bitwise_values(v, name) }
|
103
|
+
mapping = mapping_from_name(name)
|
104
|
+
mask = []
|
105
|
+
|
106
|
+
values_array.each do |pv|
|
107
|
+
mapping.values.each do |pvv|
|
108
|
+
mask << (pv | pvv)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
mask.uniq
|
113
|
+
end
|
114
|
+
|
115
|
+
# given a values_arr ay array, return a possible matches
|
116
|
+
# for a intersection
|
117
|
+
#
|
118
|
+
# with PAYMENT_TYPES_MAPPING = { credits: 0b001, slots: 0b010, paypal: 0b100 }
|
119
|
+
# see http://www.calleerlandsson.com/2015/02/16/flags-bitmasks-and-unix-file-system-permissions-in-ruby/
|
120
|
+
#
|
121
|
+
# bitwise_intersection(:slots, :credits, 'payment_types') => [0b101, 0b100, 0b011, 0b111]
|
122
|
+
def bitwise_intersection(*mixed_array, name)
|
123
|
+
values_array = mixed_array.map { |v| to_bitwise_values(v, name) }
|
124
|
+
mapping = mapping_from_name(name)
|
125
|
+
mask = []
|
126
|
+
val = values_array.reduce(&:|)
|
127
|
+
|
128
|
+
mapping.values.each do |pv|
|
129
|
+
mask << (pv | val)
|
130
|
+
end
|
131
|
+
|
132
|
+
mask.uniq
|
133
|
+
end
|
134
|
+
|
135
|
+
# given an Object, return proper Fixnum value, depending of mapping
|
136
|
+
def to_bitwise_values(object, name)
|
137
|
+
mapping = mapping_from_name(name)
|
138
|
+
if object.is_a?(Array)
|
139
|
+
object.map { |v| force_to_bitwise_value(v, mapping) }
|
140
|
+
elsif object.is_a?(Hash)
|
141
|
+
object.values.map { |v| force_to_bitwise_value(v, mapping) }
|
142
|
+
else
|
143
|
+
force_to_bitwise_value(object, mapping)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# Given a raw value (int) or a symbol, return proper raw value (int)
|
148
|
+
def force_to_bitwise_value(value_or_symbol, mapping)
|
149
|
+
if value_or_symbol.is_a?(Symbol)
|
150
|
+
mapping[value_or_symbol]
|
151
|
+
else
|
152
|
+
value_or_symbol.to_i
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
#######################
|
157
|
+
# Private class methods
|
158
|
+
#######################
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
# return mapping given a bitwise name
|
163
|
+
def mapping_from_name(name)
|
164
|
+
const_get("#{name}_mapping".upcase)
|
165
|
+
end
|
166
|
+
|
167
|
+
# build internal bitwise key-value mapping
|
168
|
+
# it add a zero value, needed for bits operations
|
169
|
+
#
|
170
|
+
# each sym get a power of 2 value
|
171
|
+
def build_mapping(symbols, name)
|
172
|
+
mapping = {}.tap do |hash|
|
173
|
+
symbols.each_with_index do |key, i|
|
174
|
+
hash[key] = 2**i
|
175
|
+
end
|
176
|
+
hash[:empty] = 0
|
177
|
+
end
|
178
|
+
# put mapping in unique constant
|
179
|
+
const_mapping_name = "#{name}_mapping".upcase
|
180
|
+
const_set(const_mapping_name, mapping)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
##########################
|
185
|
+
# Private instance methods
|
186
|
+
##########################
|
187
|
+
|
188
|
+
private
|
189
|
+
|
190
|
+
def force_to_bitwise_value(value_or_symbol, mapping)
|
191
|
+
self.class.force_to_bitwise_value(value_or_symbol, mapping)
|
192
|
+
end
|
193
|
+
|
194
|
+
# Given a raw value (int) return proper raw value (int)
|
195
|
+
def value_to_sym(value, mapping)
|
196
|
+
mapping.invert[value]
|
197
|
+
end
|
198
|
+
|
199
|
+
# Return current value to symbols array
|
200
|
+
# Ex : 011 => :slots, :credits
|
201
|
+
def value_getter(mapping)
|
202
|
+
mapping.values.select { |pv| (value & pv) != 0 }.
|
203
|
+
map { |v| value_to_sym(v, mapping) }
|
204
|
+
end
|
205
|
+
|
206
|
+
# Set current values from values array
|
207
|
+
def value_setter(values_or_symbols_array, mapping)
|
208
|
+
self.value = 0
|
209
|
+
values_or_symbols_array.each { |val| add_value(force_to_bitwise_value(val, mapping)) }
|
210
|
+
end
|
211
|
+
|
212
|
+
# Return if value presents in mask (raw value)
|
213
|
+
def value?(val)
|
214
|
+
value & val != 0
|
215
|
+
end
|
216
|
+
|
217
|
+
# add `value_or_symbol` to mask
|
218
|
+
# Ex, with values = `10`
|
219
|
+
# add_value(1) => 11
|
220
|
+
def add_value(val)
|
221
|
+
self.value |= val
|
222
|
+
end
|
223
|
+
|
224
|
+
# remove `value_or_symbol` to mask
|
225
|
+
# Ex, with values = `11`
|
226
|
+
# remove_value(1) => 10
|
227
|
+
def remove_value(val)
|
228
|
+
self.value &= ~val
|
229
|
+
end
|
230
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require_relative '../lib/attr_bitwise.rb'
|
2
|
+
|
3
|
+
describe AttrBitwise do
|
4
|
+
|
5
|
+
# test class in order to test concern
|
6
|
+
class TestClass
|
7
|
+
|
8
|
+
include AttrBitwise
|
9
|
+
|
10
|
+
attr_accessor :payment_types_value
|
11
|
+
|
12
|
+
attr_bitwise :payment_types, mapping: [:slots, :credits]
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@payment_types_value = 0
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
subject { TestClass.new }
|
21
|
+
|
22
|
+
context '.to_bitwise_values' do
|
23
|
+
|
24
|
+
context 'with Hash argument' do
|
25
|
+
it do
|
26
|
+
expect(
|
27
|
+
TestClass.to_bitwise_values(
|
28
|
+
{
|
29
|
+
a: :credits,
|
30
|
+
b: :slots
|
31
|
+
},
|
32
|
+
'payment_types'
|
33
|
+
)
|
34
|
+
).to eq [2, 1]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with Array argument' do
|
39
|
+
it do
|
40
|
+
expect(TestClass.to_bitwise_values(
|
41
|
+
[:credits, :slots],
|
42
|
+
'payment_types'
|
43
|
+
)).to eq [2, 1]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'with Fixnum argument' do
|
48
|
+
it do
|
49
|
+
expect(TestClass.to_bitwise_values(1, 'payment_types')).to eq 1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'with `payment_types` attribute_name' do
|
56
|
+
|
57
|
+
context '#payment_types=' do
|
58
|
+
|
59
|
+
before { subject.payment_types = [:slots, :credits] }
|
60
|
+
|
61
|
+
it 'should set proper value' do
|
62
|
+
expect(subject.payment_types_value).to eq 3
|
63
|
+
expect(subject.payment_types).to eq [:slots, :credits]
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'with `payment_types_value` = 0' do
|
69
|
+
context '#payment_types' do
|
70
|
+
|
71
|
+
it do
|
72
|
+
expect(subject.payment_types).to eq []
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
context '#add_payment_type' do
|
78
|
+
|
79
|
+
it do
|
80
|
+
subject.add_payment_type(:slots)
|
81
|
+
expect(subject.payment_types).to eq [:slots]
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'when called twice, each type remains unique' do
|
85
|
+
it do
|
86
|
+
subject.add_payment_type(:slots)
|
87
|
+
subject.add_payment_type(:slots)
|
88
|
+
expect(subject.payment_types).to eq [:slots]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
context '#payment_type?(:slots)' do
|
95
|
+
|
96
|
+
it do
|
97
|
+
expect(subject.payment_type?(:slots)).to eq false
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context 'with `payment_types_value` = 3' do
|
104
|
+
|
105
|
+
before { subject.payment_types_value = 3 }
|
106
|
+
|
107
|
+
context '#payment_types' do
|
108
|
+
|
109
|
+
it do
|
110
|
+
expect(subject.payment_types).to eq [:slots, :credits]
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
context '#payment_type?(:slots)' do
|
116
|
+
|
117
|
+
it do
|
118
|
+
expect(subject.payment_type?(:slots)).to eq true
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
context '#remove_payment_type' do
|
124
|
+
|
125
|
+
it do
|
126
|
+
subject.remove_payment_type(:slots)
|
127
|
+
expect(subject.payment_types).to eq [:credits]
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
context '#payment_types_union' do
|
133
|
+
|
134
|
+
it do
|
135
|
+
expect(subject.payment_types_union(1, 2)).to eq [1, 3, 2]
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
context '.bitwise_union' do
|
141
|
+
|
142
|
+
it do
|
143
|
+
expect(
|
144
|
+
TestClass.bitwise_union(1, 2, 'payment_types')
|
145
|
+
).to eq [1, 3, 2]
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
context '#bitwise_intersection' do
|
151
|
+
|
152
|
+
it do
|
153
|
+
expect(
|
154
|
+
TestClass.bitwise_intersection(1, 2, 'payment_types')
|
155
|
+
).to eq [3]
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_bitwise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Charly POLY
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-24 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: '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.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '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
|
+
description: Bitwise attribute for ruby class and Rails model
|
70
|
+
email:
|
71
|
+
- cpoly55@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- LICENSE
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- attr_bitwise.gemspec
|
83
|
+
- lib/attr_bitwise.rb
|
84
|
+
- lib/attr_bitwise/version.rb
|
85
|
+
- spec/attr_bitwise_spec.rb
|
86
|
+
homepage: ''
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Bitwise attribute for ruby class and Rails model
|
110
|
+
test_files:
|
111
|
+
- spec/attr_bitwise_spec.rb
|