bit_attrs 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7d85843c13d88c15526fb49ef6d0f0d307c68a9
4
+ data.tar.gz: ed93ad2f246627878e1fc67203008018ef080570
5
+ SHA512:
6
+ metadata.gz: 56aba610ed322a64ba3c69d50460109f9db12e9b4d2fd63b00200c3c124274f69d66cbee7a7580c1b62775f25bd8904c1b411ce1f2d4f94fdd7f34b478b6394b
7
+ data.tar.gz: e8455c613270708003cccbe13e6a2a48e1ed07b3d53589ade12a6f7cd3bebd44bcd20ec95e2e0f50b74dae49b6ce490454239edb2a936b5b02a613efa632d083
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.sublime-*
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at rsamoilov@yandex.ua. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bit_attrs.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Roman Samoilov
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # BitAttrs
2
+
3
+ BitAttrs allows to store a set of boolean values in one field.
4
+
5
+ * Simple and easy to use
6
+ * Has no additional dependencies, like ActiveRecord
7
+ * Works well with ActiveRecord, DataMapper, Virtus or POROs
8
+ * Has convenient search scopes for ActiveRecord and DataMapper
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'bit_attrs'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install bit_attrs
25
+
26
+ ## Usage
27
+
28
+ BitAttrs has similar to Rails 4 enums syntax:
29
+
30
+ ````ruby
31
+ class User < ActiveRecord::Base
32
+ include BitAttrs
33
+ bitset roles: [:admin, :user, :guest]
34
+ end
35
+ ```
36
+
37
+ In this case, BitAttrs will expect ```roles_mask``` field to actually save roles in:
38
+
39
+ ```ruby
40
+ #<User id: 1, name: 'test user', email: "test@gmail.com", roles_mask: 2>
41
+ ```
42
+
43
+ You can freely add new attributes to the end of the list without changing already existing records:
44
+
45
+ ````ruby
46
+ class User < ActiveRecord::Base
47
+ include BitAttrs
48
+ bitset roles: [:admin, :user, :guest, :developer]
49
+ end
50
+ ```
51
+
52
+ Similarly to Rails enums, if you want to delete some of the attributes, you might want to use hash syntax, where keys are attribute names and values are indexes:
53
+
54
+ ````ruby
55
+ class User < ActiveRecord::Base
56
+ include BitAttrs
57
+ bitset roles: { admin: 0, user: 1, developer: 3 }
58
+ end
59
+ ```
60
+
61
+ Attributes can be accessed on the instance level or through the ```roles``` method:
62
+
63
+ ```ruby
64
+ > user
65
+ => #<User id: 1, name: 'test user', email: "test@gmail.com", roles_mask: 2>
66
+
67
+ > user.user?
68
+ => true
69
+
70
+ > user.roles
71
+ => { admin: false, user: true, guest: false }
72
+ ```
73
+
74
+ Update all attributes (with overwriting):
75
+
76
+ ```ruby
77
+ > user.roles
78
+ => { admin: false, user: true, guest: false }
79
+
80
+ > user.update(roles: { guest: true }) && user.roles
81
+ => { admin: false, user: false, guest: true }
82
+ ```
83
+
84
+ Update single attribute:
85
+
86
+ ```ruby
87
+ > user.roles
88
+ => { admin: false, user: true, guest: false }
89
+
90
+ > user.admin = true
91
+ => { admin: true, user: true, guest: false }
92
+ ```
93
+
94
+ ### Scopes
95
+
96
+ Considering the example above, BitAttrs will create following search scopes:
97
+
98
+ ```
99
+ * User.with_roles
100
+ * User.without_roles
101
+ ```
102
+
103
+ Example:
104
+
105
+ ```ruby
106
+ > User.with_roles(:user)
107
+ => all users which have role :user set to true
108
+ > User.with_roles(:admin, :user).without_roles(:developer)
109
+ => all users which have following roles set: { admin: true, user: true, developer: false }
110
+ ```
111
+
112
+ ## Contributing
113
+
114
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rsamoilov/bit_attrs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
115
+
116
+
117
+ ## License
118
+
119
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bit_attrs"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bit_attrs.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bit_attrs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bit_attrs"
8
+ spec.version = BitAttrs::VERSION
9
+ spec.authors = ["Roman Samoilov"]
10
+ spec.email = ["rsamoilov@yandex.ua"]
11
+
12
+ spec.summary = %q{Store a set of boolean values in one field. Works well with ActiveRecord, DataMapper, Virtus or POROs.}
13
+ spec.homepage = "https://github.com/rsamoilov/bit_attrs"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ spec.add_development_dependency "mocha", "~> 1.1"
25
+ end
data/lib/bit_attrs.rb ADDED
@@ -0,0 +1,62 @@
1
+ require "bit_attrs/bitset"
2
+ require "bit_attrs/version"
3
+ require "bit_attrs/bindings"
4
+
5
+ module BitAttrs
6
+
7
+ def self.included(klass)
8
+ klass.class_eval do
9
+ extend ClassMethods
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ attr_reader :bit_attrs
15
+
16
+ # bitset roles: [:admin, :user, :guest]
17
+ # bitset access_levels: [read: 0, write: 1, delete: 2, api: 3]
18
+ def bitset(flags_map)
19
+ @bit_attrs ||= {}
20
+
21
+ flags_map.each do |attr_name, flags_list|
22
+ @bit_attrs[attr_name] = check_flags_list(flags_list)
23
+ define_alias_methods(attr_name, @bit_attrs[attr_name])
24
+ Bindings.create(self, attr_name)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def check_flags_list(flags_list)
31
+ return flags_list if flags_list.is_a?(Hash)
32
+
33
+ flags_list.each_with_index.inject({}) do |memo, (flag, i)|
34
+ memo.merge!(flag => i)
35
+ end
36
+ end
37
+
38
+ def define_alias_methods(attr_name, flags_map)
39
+ define_method attr_name do
40
+ Bitset.new(flags_map, self.send("#{attr_name}_mask"))
41
+ end
42
+
43
+ define_method "#{attr_name}=" do |flags_map|
44
+ bitset = self.class.new.send(attr_name)
45
+ flags_map.each { |flag, bool| bitset[flag] = bool }
46
+ self.send("#{attr_name}_mask=", bitset.to_i)
47
+ end
48
+
49
+ flags_map.each do |flag, _i|
50
+ define_method(flag) { self.send(attr_name)[flag] }
51
+ define_method("#{flag}?") { self.send(flag) }
52
+
53
+ define_method("#{flag}=") do |bool|
54
+ bitset = self.send(attr_name)
55
+ bitset[flag] = bool
56
+ self.send("#{attr_name}_mask=", bitset.to_i)
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,30 @@
1
+ require "bit_attrs/bindings/active_record_binding"
2
+ require "bit_attrs/bindings/data_mapper_binding"
3
+
4
+ module BitAttrs
5
+
6
+ class Bindings
7
+ ORM_BINDINGS = [ActiveRecordBinding, DataMapperBinding]
8
+
9
+ def self.create(klass, attr_name)
10
+ ORM_BINDINGS.each do |orm_binding|
11
+ next unless orm_binding.should_be_created?(klass)
12
+
13
+ klass.define_singleton_method "with_#{attr_name}" do |*flags_list|
14
+ bitset = self.new.send(attr_name)
15
+ flags_list.each { |flag| bitset[flag] = true }
16
+
17
+ orm_binding.with(klass, attr_name, bitset)
18
+ end
19
+
20
+ klass.define_singleton_method "without_#{attr_name}" do |*flags_list|
21
+ bitset = self.new.send(attr_name)
22
+ flags_list.each { |flag| bitset[flag] = true }
23
+
24
+ orm_binding.without(klass, attr_name, bitset)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,20 @@
1
+ module BitAttrs
2
+
3
+ class ActiveRecordBinding
4
+ def self.should_be_created?(klass)
5
+ return false unless defined?(ActiveRecord)
6
+ return false unless klass.ancestors.include?(ActiveRecord::Base)
7
+
8
+ true
9
+ end
10
+
11
+ def self.with(klass, attr_name, bitset)
12
+ klass.where("#{attr_name}_mask & ? = ?", bitset.to_i, bitset.to_i)
13
+ end
14
+
15
+ def self.without(klass, attr_name, bitset)
16
+ klass.where("#{attr_name}_mask IS NULL OR #{attr_name}_mask & ? = ?", bitset.to_i, 0)
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,20 @@
1
+ module BitAttrs
2
+
3
+ class DataMapperBinding
4
+ def self.should_be_created?(klass)
5
+ return false unless defined?(DataMapper::Resource)
6
+ return false unless klass.included_modules.include?(DataMapper::Resource)
7
+
8
+ true
9
+ end
10
+
11
+ def self.with(klass, attr_name, bitset)
12
+ klass.all(conditions: ["#{attr_name}_mask & ? = ?", bitset.to_i, bitset.to_i])
13
+ end
14
+
15
+ def self.without(klass, attr_name, bitset)
16
+ klass.all(conditions: ["#{attr_name}_mask IS NULL OR #{attr_name}_mask & ? = ?", bitset.to_i, 0])
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,51 @@
1
+ module BitAttrs
2
+
3
+ class Bitset
4
+ def initialize(flags_map, mask = 0)
5
+ @flags_map = flags_map
6
+ @mask = mask.to_i
7
+ end
8
+
9
+ def to_h
10
+ @flags_map.inject({}) do |memo, (flag, _i)|
11
+ memo.merge! flag => self[flag]
12
+ end
13
+ end
14
+
15
+ def [](flag)
16
+ i = @flags_map[flag.to_sym]
17
+ bit = (@mask >> i) & 1
18
+
19
+ bit == 1
20
+ end
21
+
22
+ def []=(flag, value)
23
+ i = @flags_map[flag.to_sym]
24
+
25
+ if truthy?(value)
26
+ @mask |= 1 << i
27
+ else
28
+ @mask &= ~(1 << i)
29
+ end
30
+ end
31
+
32
+ def each(&block)
33
+ self.to_h.each &block
34
+ end
35
+
36
+ def inspect
37
+ to_h.inspect
38
+ end
39
+
40
+ def to_i
41
+ @mask
42
+ end
43
+
44
+ private
45
+
46
+ def truthy?(value)
47
+ value == true || value == 'true' || value == '1'
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,3 @@
1
+ module BitAttrs
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bit_attrs
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Samoilov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ description:
70
+ email:
71
+ - rsamoilov@yandex.ua
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - bit_attrs.gemspec
86
+ - lib/bit_attrs.rb
87
+ - lib/bit_attrs/bindings.rb
88
+ - lib/bit_attrs/bindings/active_record_binding.rb
89
+ - lib/bit_attrs/bindings/data_mapper_binding.rb
90
+ - lib/bit_attrs/bitset.rb
91
+ - lib/bit_attrs/version.rb
92
+ homepage: https://github.com/rsamoilov/bit_attrs
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Store a set of boolean values in one field. Works well with ActiveRecord,
116
+ DataMapper, Virtus or POROs.
117
+ test_files: []