bitten 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 049eab1bdcc6e4d2f84daaa744708f2db48ab702
4
+ data.tar.gz: 5f349da0d0d4d5b3aa9a33bb88ffaca824b8e194
5
+ SHA512:
6
+ metadata.gz: 035373de93a1063cf9ef6478d5c32315f32dee039aab2bd3054f637347d148226ee467cb1ec0e71cd5d8bdc9c76fc2bd1bccb0535accea7c93948250fc07db23
7
+ data.tar.gz: 9cc59ca19e8c214312ceec74096f88b9e89983a4dc39be9e047321f48c6e439732b5b93fab53dbeb41bdfe92e7cb0a52f2b12a0a19bec9d9ae34cfa10558ec96
@@ -0,0 +1,4 @@
1
+ pkg
2
+ .bundle
3
+ .yardoc
4
+ doc
data/.rspec ADDED
@@ -0,0 +1,6 @@
1
+ --format progress
2
+ --color
3
+ --order rand
4
+ -Ilib
5
+ -Ispec
6
+ -r spec_helper
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - rbx-19mode
@@ -0,0 +1,8 @@
1
+ --no-private
2
+ --title "Bitten"
3
+ --readme README.md
4
+ --markup markdown
5
+ --markup-provider kramdown
6
+ -
7
+ HISTORY.md
8
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bitten (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (4.0.0)
10
+ activesupport (= 4.0.0)
11
+ builder (~> 3.1.0)
12
+ activerecord (4.0.0)
13
+ activemodel (= 4.0.0)
14
+ activerecord-deprecated_finders (~> 1.0.2)
15
+ activesupport (= 4.0.0)
16
+ arel (~> 4.0.0)
17
+ activerecord-deprecated_finders (1.0.3)
18
+ activesupport (4.0.0)
19
+ i18n (~> 0.6, >= 0.6.4)
20
+ minitest (~> 4.2)
21
+ multi_json (~> 1.3)
22
+ thread_safe (~> 0.1)
23
+ tzinfo (~> 0.3.37)
24
+ arel (4.0.0)
25
+ atomic (1.1.14)
26
+ builder (3.1.4)
27
+ diff-lcs (1.2.4)
28
+ i18n (0.6.5)
29
+ kramdown (1.2.0)
30
+ minitest (4.7.5)
31
+ multi_json (1.8.0)
32
+ rake (10.1.0)
33
+ rspec (2.14.1)
34
+ rspec-core (~> 2.14.0)
35
+ rspec-expectations (~> 2.14.0)
36
+ rspec-mocks (~> 2.14.0)
37
+ rspec-core (2.14.5)
38
+ rspec-expectations (2.14.3)
39
+ diff-lcs (>= 1.1.3, < 2.0)
40
+ rspec-mocks (2.14.3)
41
+ sqlite3 (1.3.8)
42
+ thread_safe (0.1.3)
43
+ atomic
44
+ tzinfo (0.3.37)
45
+ yard (0.8.7.2)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ activerecord
52
+ bitten!
53
+ kramdown
54
+ rake
55
+ rspec
56
+ sqlite3
57
+ yard
@@ -0,0 +1,6 @@
1
+ # HISTORY
2
+
3
+ ## 0.0.1
4
+
5
+ * Initial release
6
+
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2013 Arjan van der Gaag
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,89 @@
1
+ # Bitten [![Build Status](https://secure.travis-ci.org/avdgaag/bitten.png?branch=master)](http://travis-ci.org/avdgaag/bitten)
2
+
3
+ ## Introduction
4
+
5
+ Bitten is a simple module for adding multiple boolean flags to any Ruby object,
6
+ that internally get stored as a single integer attribute. If your Ruby object
7
+ is an ActiveRecord object, it also provides some convenience scopes for you.
8
+
9
+ The advantage is a simple data model, with a single attribute containing many
10
+ bits of information. You can keep adding more flags to the list of tracked
11
+ flags, as long as the order of existing flags is kept intact.
12
+
13
+ ## Installation
14
+
15
+ Bitten is distributed as a Ruby gem, which should be installed on most Macs and
16
+ Linux systems. Once you have ensured you have a working installation of Ruby
17
+ and Ruby gems, install the gem as follows from the command line:
18
+
19
+ gem install bitten
20
+
21
+ TO use it in projects using Bundler, add the gem to your `Gemfile`:
22
+
23
+ gem 'bitten'
24
+
25
+ Then, install your gems:
26
+
27
+ bundle install
28
+
29
+ ## Usage
30
+
31
+ Here's an example of how to use Bitten:
32
+
33
+ ActiveRecord::Migration.create_table do |t|
34
+ t.integer :bits, null: false, default: 0
35
+ end
36
+
37
+ class User < ActiveRecord::Base
38
+ include Bitten
39
+ has_bits :editor, :author, :reviewer
40
+ end
41
+
42
+ user = User.new editor: true
43
+ user.editor? # => true
44
+ user.author? # => false
45
+ user.reviewer? # => false
46
+ user.author = true
47
+ user.author? # => true
48
+ user.save
49
+ User.author # => [user]
50
+ User.editor # => [user]
51
+ User.reviewer # => []
52
+ User.not_reviewer # => [user]
53
+
54
+ ### Documentation
55
+
56
+ See the inline [API
57
+ docs](http://rubydoc.info/github/avdgaag/bitten/master/frames) for more
58
+ information.
59
+
60
+ ## Other
61
+
62
+ ### Note on Patches/Pull Requests
63
+
64
+ 1. Fork the project.
65
+ 2. Make your feature addition or bug fix.
66
+ 3. Add tests for it. This is important so I don't break it in a future version
67
+ unintentionally.
68
+ 4. Commit, do not mess with rakefile, version, or history. (if you want to have
69
+ your own version, that is fine but bump version in a commit by itself I can
70
+ ignore when I pull)
71
+ 5. Send me a pull request. Bonus points for topic branches.
72
+
73
+ ### Issues
74
+
75
+ Please report any issues, defects or suggestions in the [Github issue
76
+ tracker](https://github.com/avdgaag/bitten/issues).
77
+
78
+ ### What has changed?
79
+
80
+ See the [HISTORY](https://github.com/avdgaag/bitten/blob/master/HISTORY.md) file
81
+ for a detailed changelog.
82
+
83
+ ### Credits
84
+
85
+ Created by: Arjan van der Gaag
86
+ URL: [http://arjanvandergaag.nl](http://arjanvandergaag.nl)
87
+ Project homepage: [http://avdgaag.github.com/bitten](http://avdgaag.github.com/bitten)
88
+ Date: september 2013
89
+ License: [MIT-license](https://github.com/avdgaag/bitten/blob/master/LICENSE) (same as Ruby)
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+ Bundler.setup
5
+
6
+ desc 'Default: run specs.'
7
+ task :default => :spec
8
+
9
+ require 'rspec/core/rake_task'
10
+ desc 'Run specs'
11
+ RSpec::Core::RakeTask.new
12
+
13
+ require 'yard'
14
+ desc 'Generate API docs'
15
+ YARD::Rake::YardocTask.new
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/bitten/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ # Metadata
6
+ s.name = 'bitten'
7
+ s.version = Bitten::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Arjan van der Gaag']
10
+ s.email = %q{arjan@arjanvandergaag.nl}
11
+ s.description = %q{A simple implementation of a bit fields attribute for Ruby objects.}
12
+ s.homepage = %q{http://avdgaag.github.com/bitten}
13
+ s.summary = <<-EOS
14
+ Bitten is a simple module for adding multiple boolean flags to any Ruby object,
15
+ that internally get stored as a single integer attribute. If your Ruby object
16
+ is an ActiveRecord object, it also provides some convenience scopes for you.
17
+
18
+ The advantage is a simple data model, with a single attribute containing many
19
+ bits of information. You can keep adding more flags to the list of tracked
20
+ flags, as long as the order of existing flags is kept intact.
21
+ EOS
22
+
23
+ # Files
24
+ s.files = `git ls-files`.split("
25
+ ")
26
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("
27
+ ")
28
+ s.executables = `git ls-files -- bin/*`.split("
29
+ ").map{ |f| File.basename(f) }
30
+ s.require_paths = ["lib"]
31
+
32
+ # Rdoc
33
+ s.rdoc_options = ['--charset=UTF-8']
34
+ s.extra_rdoc_files = [
35
+ 'LICENSE',
36
+ 'README.md',
37
+ 'HISTORY.md'
38
+ ]
39
+
40
+ # Dependencies
41
+ s.add_development_dependency 'kramdown'
42
+ s.add_development_dependency 'yard'
43
+ s.add_development_dependency 'rspec'
44
+ s.add_development_dependency 'rake'
45
+ s.add_development_dependency 'activerecord'
46
+ s.add_development_dependency 'sqlite3'
47
+ end
@@ -0,0 +1,98 @@
1
+ require 'bitten/version'
2
+
3
+ module Bitten
4
+ # Lower-case string values that should be considered truthy. This allows us
5
+ # to handle both actual `true` and `false` values as well as truthy string
6
+ # values coming from HTML forms.
7
+ TRUE_VALUES = %w[true 1 t].freeze
8
+
9
+ # Define the different flags to be stored in the host model's `bits` column.
10
+ # Every flag given gets its own query method (e.g. `published?`) and setter
11
+ # method (e.g. `published=`). All flags are stored in a single integer column
12
+ # in the host object.
13
+ #
14
+ # Note that you can add more flags to the model if you want. If you do, it
15
+ # will default to false unless explicitly set otherwise. Do make sure to only
16
+ # add new flags to the end of the list, as order is vitally important.
17
+ #
18
+ # Note: this assumes the host object defines `bits` and `bits=`, with a
19
+ # default value of `0`. This does not have to be an ActiveRecord attribute,
20
+ # any `attr_accessor` will do, as longs as the default value is set.
21
+ #
22
+ # To use the convenience scopes, the host object has to be an ActiveRecord
23
+ # object in order to define the class-level `scope` method. To disable these,
24
+ # pass the `scopes: false` option.
25
+ #
26
+ # @example Basic usage
27
+ # ActiveRecord::Migration.create_table do |t|
28
+ # t.integer :bits, null: false, default: 0
29
+ # end
30
+ #
31
+ # class User < ActiveRecord::Base
32
+ # include Bitten
33
+ # has_bits :editor, :author, :reviewer
34
+ # end
35
+ #
36
+ # user = User.new editor: true
37
+ # user.editor? # => true
38
+ # user.author? # => false
39
+ # user.reviewer? # => false
40
+ # user.author = true
41
+ # user.author? # => true
42
+ # user.save
43
+ # User.author # => [user]
44
+ # User.editor # => [user]
45
+ # User.reviewer # => []
46
+ # User.not_reviewer # => [user]
47
+ #
48
+ # @example Provide options
49
+ # class User < ActiveRecord::Base
50
+ # include Bitten
51
+ # has_bits :admin, :manager, scopes: false
52
+ # end
53
+ def has_bits(*flags)
54
+ options = flags.last.is_a?(Hash) ? flags.pop : {}
55
+ options = { scopes: true, column: 'bits' }.merge(options)
56
+
57
+ if options[:scopes]
58
+ flags.each_with_index do |flag, index|
59
+ define_scopes flag, options[:column], 1 << index
60
+ end
61
+ end
62
+
63
+ mod = Module.new do
64
+ define_method :bitten_bits do
65
+ send options[:column]
66
+ end
67
+
68
+ define_method :bitten_bits= do |val|
69
+ send :"#{options[:column]}=", val
70
+ end
71
+
72
+ flags.each_with_index do |flag, index|
73
+ bit = 1 << index
74
+
75
+ define_method :"#{flag}?" do
76
+ bitten_bits & bit != 0
77
+ end
78
+
79
+ define_method :"#{flag}=" do |value|
80
+ if TRUE_VALUES.include? value.to_s.downcase
81
+ self.bitten_bits |= bit
82
+ else
83
+ self.bitten_bits &= ~bit
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ include mod
90
+ end
91
+
92
+ private
93
+
94
+ def define_scopes(flag, column, bit)
95
+ scope flag, -> { where(['(?.? & ?) != 0', table_name, column, bit]) }
96
+ scope :"not_#{flag}", -> { where(['(?.? & ?) = 0', table_name, column, bit]) }
97
+ end
98
+ end
@@ -0,0 +1,3 @@
1
+ module Bitten
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,52 @@
1
+ describe Bitten do
2
+
3
+ before do
4
+ Post.has_bits :published, :featured, :sponsored
5
+ end
6
+
7
+ subject(:post) { Post.new }
8
+
9
+ it { should_not be_published }
10
+ it { should_not be_featured }
11
+ it { should_not be_sponsored }
12
+ it { should respond_to(:published=) }
13
+ it { should respond_to(:featured=) }
14
+ it { should respond_to(:sponsored=) }
15
+
16
+ its(:class) { should respond_to(:published) }
17
+ its(:class) { should respond_to(:featured) }
18
+ its(:class) { should respond_to(:sponsored) }
19
+ its(:class) { should respond_to(:not_published) }
20
+ its(:class) { should respond_to(:not_featured) }
21
+ its(:class) { should respond_to(:not_sponsored) }
22
+
23
+ it 'can toggle a flag' do
24
+ subject.published = true
25
+ expect(post).to be_published
26
+ subject.published = false
27
+ expect(post).not_to be_published
28
+ end
29
+
30
+ describe 'scopes' do
31
+ let!(:post1) { Post.create! published: true }
32
+ let!(:post2) { Post.create! featured: true }
33
+
34
+ it 'finds a record by flag' do
35
+ expect(Post.published).to include(post1)
36
+ expect(Post.published).not_to include(post2)
37
+ expect(Post.featured).not_to include(post1)
38
+ expect(Post.featured).to include(post2)
39
+ expect(Post.sponsored).not_to include(post1)
40
+ expect(Post.sponsored).not_to include(post2)
41
+ end
42
+
43
+ it 'finds a record by not having a flag' do
44
+ expect(Post.not_published).not_to include(post1)
45
+ expect(Post.not_published).to include(post2)
46
+ expect(Post.not_featured).to include(post1)
47
+ expect(Post.not_featured).not_to include(post2)
48
+ expect(Post.not_sponsored).to include(post1)
49
+ expect(Post.not_sponsored).to include(post2)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ require 'bitten'
3
+
4
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
@@ -0,0 +1,13 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
4
+ ActiveRecord::Migration.verbose = false
5
+
6
+ ActiveRecord::Migration.create_table :posts do |t|
7
+ t.integer :bits, null: false, default: 0
8
+ t.timestamps
9
+ end
10
+
11
+ class Post < ActiveRecord::Base
12
+ extend Bitten
13
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitten
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arjan van der Gaag
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: rake
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
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A simple implementation of a bit fields attribute for Ruby objects.
98
+ email: arjan@arjanvandergaag.nl
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files:
102
+ - LICENSE
103
+ - README.md
104
+ - HISTORY.md
105
+ files:
106
+ - .gitignore
107
+ - .rspec
108
+ - .travis.yml
109
+ - .yardopts
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - HISTORY.md
113
+ - LICENSE
114
+ - README.md
115
+ - Rakefile
116
+ - bitten.gemspec
117
+ - lib/bitten.rb
118
+ - lib/bitten/version.rb
119
+ - spec/bitten_spec.rb
120
+ - spec/spec_helper.rb
121
+ - spec/support/activerecord.rb
122
+ homepage: http://avdgaag.github.com/bitten
123
+ licenses: []
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options:
127
+ - --charset=UTF-8
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.1.5
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Bitten is a simple module for adding multiple boolean flags to any Ruby object,
146
+ that internally get stored as a single integer attribute. If your Ruby object is
147
+ an ActiveRecord object, it also provides some convenience scopes for you. The advantage
148
+ is a simple data model, with a single attribute containing many bits of information.
149
+ You can keep adding more flags to the list of tracked flags, as long as the order
150
+ of existing flags is kept intact.
151
+ test_files:
152
+ - spec/bitten_spec.rb
153
+ - spec/spec_helper.rb
154
+ - spec/support/activerecord.rb
155
+ has_rdoc: