bitfield_attribute 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/README.md +6 -5
- data/Rakefile +5 -0
- data/lib/bitfield_attribute/base.rb +16 -11
- data/lib/bitfield_attribute/version.rb +1 -1
- data/spec/lib/activerecord_spec.rb +31 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/test_bitfield.rb +10 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22148454d826a5aed539e10565456317fde44ce0
|
4
|
+
data.tar.gz: c058f6722461be8857e46fd06877566b759be544
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 999ab9f3495bf06de562a9d3ecdebcaf4939b2277bf454dc1b8a686b1f9f570cf98d80d72acf7a5d5995236623aa369402bb7eb31c94cdb5a48e9b82f4503271
|
7
|
+
data.tar.gz: cdfd13539d75cd867d8dc9c9148856fb262bf16d8432e362bf4721fd38afc037d3e5aef1aab99f8d7c267203f0c721a54b79e8ea66f7dcf6876a57b4dd1c3f1a
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -80,13 +80,14 @@ end
|
|
80
80
|
|
81
81
|
Add this to your locale file:
|
82
82
|
|
83
|
-
```
|
83
|
+
```yaml
|
84
84
|
en:
|
85
85
|
activemodel:
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
86
|
+
attributes:
|
87
|
+
"user/notification_settings":
|
88
|
+
weekly_digest: 'Weekly top sellers digest'
|
89
|
+
announces: 'Announces'
|
90
|
+
events: 'Invitations'
|
90
91
|
```
|
91
92
|
|
92
93
|
Use forms:
|
data/Rakefile
CHANGED
@@ -79,19 +79,23 @@ module BitfieldAttribute
|
|
79
79
|
@values.freeze
|
80
80
|
end
|
81
81
|
|
82
|
-
def attributes=(
|
82
|
+
def attributes=(value)
|
83
83
|
@values.each { |key, _| @values[key] = false }
|
84
|
-
update(
|
84
|
+
update(value)
|
85
85
|
end
|
86
86
|
|
87
|
-
def update(
|
88
|
-
|
89
|
-
|
90
|
-
|
87
|
+
def update(value)
|
88
|
+
if value.is_a?(Fixnum)
|
89
|
+
write_bits(value)
|
90
|
+
else
|
91
|
+
value.symbolize_keys.each do |key, value|
|
92
|
+
if @values.keys.include?(key)
|
93
|
+
@values[key] = true_value?(value)
|
94
|
+
end
|
91
95
|
end
|
96
|
+
write_bits
|
92
97
|
end
|
93
98
|
|
94
|
-
write_bits
|
95
99
|
end
|
96
100
|
|
97
101
|
private
|
@@ -104,14 +108,15 @@ module BitfieldAttribute
|
|
104
108
|
end
|
105
109
|
end
|
106
110
|
|
107
|
-
def write_bits
|
108
|
-
|
111
|
+
def write_bits(bits = nil)
|
112
|
+
if bits.nil?
|
113
|
+
bits = 0
|
109
114
|
@values.keys.each.with_index do |name, index|
|
110
115
|
bits = bits | (2 ** index) if @values[name]
|
111
116
|
end
|
112
|
-
|
113
|
-
@instance[@attribute] = bits
|
114
117
|
end
|
118
|
+
|
119
|
+
@instance[@attribute] = bits
|
115
120
|
end
|
116
121
|
|
117
122
|
def true_value?(value)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BitField::DatabaseUser do
|
4
|
+
before do
|
5
|
+
ActiveRecord::Schema.define do
|
6
|
+
create_table :database_users, force: true do |t|
|
7
|
+
t.string :name
|
8
|
+
t.integer :bitfield
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "works" do
|
14
|
+
BitField::DatabaseUser.create!(
|
15
|
+
name: "Artem",
|
16
|
+
bitfield: { first: true, second: false, last: true }
|
17
|
+
)
|
18
|
+
|
19
|
+
record = BitField::DatabaseUser.first
|
20
|
+
expect(record.bitfield.first?).to eq true
|
21
|
+
expect(record.bitfield.second?).to eq false
|
22
|
+
expect(record.bitfield.last?).to eq true
|
23
|
+
|
24
|
+
record.update!(record.attributes)
|
25
|
+
|
26
|
+
expect(record.bitfield.first?).to eq true
|
27
|
+
expect(record.bitfield.second?).to eq false
|
28
|
+
expect(record.bitfield.last?).to eq true
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,9 +8,13 @@ SimpleCov.start do
|
|
8
8
|
add_filter 'spec'
|
9
9
|
end
|
10
10
|
|
11
|
+
require 'active_record'
|
12
|
+
ActiveRecord::Base.establish_connection(database: ':memory:', adapter: 'sqlite3')
|
13
|
+
|
11
14
|
require 'bitfield_attribute'
|
12
15
|
require 'support/test_bitfield'
|
13
16
|
|
17
|
+
|
14
18
|
RSpec.configure do |config|
|
15
19
|
config.order = :random
|
16
20
|
config.filter_run(:focus)
|
@@ -17,4 +17,14 @@ module BitField
|
|
17
17
|
define_bits :second
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
class DatabaseUser < ActiveRecord::Base
|
22
|
+
def bitfield
|
23
|
+
@bitfield ||= TestBitfield.new(self, :bitfield)
|
24
|
+
end
|
25
|
+
|
26
|
+
def bitfield=(value)
|
27
|
+
bitfield.attributes = value
|
28
|
+
end
|
29
|
+
end
|
20
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitfield_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viktor Sokolov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/bitfield_attribute.rb
|
112
112
|
- lib/bitfield_attribute/base.rb
|
113
113
|
- lib/bitfield_attribute/version.rb
|
114
|
+
- spec/lib/activerecord_spec.rb
|
114
115
|
- spec/lib/base_spec.rb
|
115
116
|
- spec/spec_helper.rb
|
116
117
|
- spec/support/test_bitfield.rb
|
@@ -134,11 +135,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
135
|
version: '0'
|
135
136
|
requirements: []
|
136
137
|
rubyforge_project:
|
137
|
-
rubygems_version: 2.
|
138
|
+
rubygems_version: 2.4.6
|
138
139
|
signing_key:
|
139
140
|
specification_version: 4
|
140
141
|
summary: Bitfield value object for ActiveModel
|
141
142
|
test_files:
|
143
|
+
- spec/lib/activerecord_spec.rb
|
142
144
|
- spec/lib/base_spec.rb
|
143
145
|
- spec/spec_helper.rb
|
144
146
|
- spec/support/test_bitfield.rb
|