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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1da85dce3a7dee9f30ae1f56db09a16e2cf58098
4
- data.tar.gz: 3b30bfaf3943ffe7dff701bdcb931cce8173b8ed
3
+ metadata.gz: 22148454d826a5aed539e10565456317fde44ce0
4
+ data.tar.gz: c058f6722461be8857e46fd06877566b759be544
5
5
  SHA512:
6
- metadata.gz: 6649c773b5257f37c07856865fb1b2e6c970247624839ed418b6091afef7e8ffd9d23984e9e006392add8f3b085796f1985ddb28e06f1fe16fcb912041aa1e81
7
- data.tar.gz: 561f47334f78e9e7b9c04050bf4dea2da9cbae9333c3575f2312e32e6429654ac4b99eefae7689a37a93211d35a0880a9f79b25922e0457cf71633cf4a7b8f7f
6
+ metadata.gz: 999ab9f3495bf06de562a9d3ecdebcaf4939b2277bf454dc1b8a686b1f9f570cf98d80d72acf7a5d5995236623aa369402bb7eb31c94cdb5a48e9b82f4503271
7
+ data.tar.gz: cdfd13539d75cd867d8dc9c9148856fb262bf16d8432e362bf4721fd38afc037d3e5aef1aab99f8d7c267203f0c721a54b79e8ea66f7dcf6876a57b4dd1c3f1a
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in bitfield_attribute.gemspec
4
4
  gemspec
5
+
6
+ gem "activerecord", "~> 4.0"
7
+ gem "sqlite3"
data/README.md CHANGED
@@ -80,13 +80,14 @@ end
80
80
 
81
81
  Add this to your locale file:
82
82
 
83
- ```ruby
83
+ ```yaml
84
84
  en:
85
85
  activemodel:
86
- "user/notification_settings":
87
- weekly_digest: 'Weekly top sellers digest'
88
- announces: 'Announces'
89
- events: 'Invitations'
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
@@ -1,2 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -79,19 +79,23 @@ module BitfieldAttribute
79
79
  @values.freeze
80
80
  end
81
81
 
82
- def attributes=(hash)
82
+ def attributes=(value)
83
83
  @values.each { |key, _| @values[key] = false }
84
- update(hash)
84
+ update(value)
85
85
  end
86
86
 
87
- def update(hash)
88
- hash.symbolize_keys.each do |key, value|
89
- if @values.keys.include?(key)
90
- @values[key] = true_value?(value)
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
- 0.tap do |bits|
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)
@@ -1,3 +1,3 @@
1
1
  module BitfieldAttribute
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -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
@@ -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.1
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: 2014-12-10 00:00:00.000000000 Z
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.2.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