simple_flaggable_column 0.0.1

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: d0b33b7ddfccc8f03c443d21df4e69420c4c8139
4
+ data.tar.gz: 58541615a542b6e7a01684f9ad8d8cf4d5929119
5
+ SHA512:
6
+ metadata.gz: 6103bc6649aea9593036de95463703967b3bbd2403da77d585c4868f999515637210e97cd74e6648e92d93218da43da1b136ee24dd2c6ec5fab54aa6f464d0d7
7
+ data.tar.gz: de63367fb7acc0938556b359b63a99c818275a95dc19e12e6262308019955d4b591937f01afc0a20352117c5c578a04b7183b69b2f5ffe46201c029cac0577bf
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ /vendor
16
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_flaggable_column.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Zequez
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # SimpleFlaggableColumn
2
+
3
+ Allows you to add really simple binary flag column to an ActiveRecord model.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple_flaggable_column'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple_flaggable_column
20
+
21
+ ## Usage example
22
+
23
+ Migration:
24
+
25
+ ```ruby
26
+ add_column(:games, :platforms, :integer, default: 0, null: false)
27
+ ```
28
+
29
+ Model:
30
+
31
+ ```ruby
32
+ class Game < ActiveRecord::Base
33
+ include SimpleFlaggableColumn
34
+
35
+ flag_column :platforms, {
36
+ win: 0b001,
37
+ mac: 0b010,
38
+ linux: 0b100
39
+ }
40
+ end
41
+ ```
42
+
43
+ This redefines the `platforms` method in `Game` so you can set it and read it as an array:
44
+
45
+ ```ruby
46
+ game = Game.new
47
+ game.platforms = [:win, :linux]
48
+ # => [:win, :linux]
49
+ game.read_attribute :platforms
50
+ # => 5
51
+ game.read_attribute(:platforms).to_s(2)
52
+ # => "101"
53
+ ```
54
+
55
+ Push/Pop and other arrays operations won't work, just simple writing and reading.
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( https://github.com/Zequez/simple_flaggable_column/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create a new Pull Request
@@ -0,0 +1,3 @@
1
+ module SimpleFlaggableColumn
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support'
2
+ require 'simple_flaggable_column/version'
3
+
4
+ module SimpleFlaggableColumn
5
+ extend ActiveSupport::Concern
6
+
7
+ def self.symbols_to_flags(symbols, symbols_flags)
8
+ symbols.map{|s| symbols_flags[s]}.compact.reduce(:|) || 0
9
+ end
10
+
11
+ def self.flags_to_symbols(flags, symbols_flags)
12
+ symbols_flags.each_pair.inject([]){|all, v| (flags & v[1] != 0) ? (all << v[0]) : all}
13
+ end
14
+
15
+ module ClassMethods
16
+ def flag_column(name, symbols_flags)
17
+ flags_symbols = symbols_flags.invert
18
+
19
+ define_singleton_method :"#{name}_flags" do |*symbols|
20
+ if symbols.empty?
21
+ symbols_flags
22
+ else
23
+ SimpleFlaggableColumn.symbols_to_flags(symbols, symbols_flags)
24
+ end
25
+ end
26
+
27
+ define_singleton_method :"flags_#{name}" do
28
+ flags_symbols
29
+ end
30
+
31
+ define_method "#{name}=" do |symbols|
32
+ write_attribute name, SimpleFlaggableColumn.symbols_to_flags(symbols, symbols_flags)
33
+ end
34
+
35
+ define_method name do
36
+ SimpleFlaggableColumn.flags_to_symbols(read_attribute(name), symbols_flags)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -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 'simple_flaggable_column/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'simple_flaggable_column'
8
+ spec.version = SimpleFlaggableColumn::VERSION
9
+ spec.authors = ['Zequez']
10
+ spec.email = ['zequez@gmail.com']
11
+ spec.summary = 'Simple binary flags columns'
12
+ spec.description = 'This gem adds a concern to allow you to use binary flags columns in ActiveRecord models'
13
+ spec.homepage = 'https://github.com/Zequez/simple_flaggable_column'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'activesupport', '~> 4.2'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.7'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'with_model', '~> 1.2'
25
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
26
+ end
@@ -0,0 +1,55 @@
1
+ describe SimpleFlaggableColumn do
2
+ with_model :GameModel do
3
+ table do |t|
4
+ t.integer :platforms, default: 0, null: false
5
+ end
6
+
7
+ model do
8
+ include SimpleFlaggableColumn
9
+
10
+ flag_column :platforms, {
11
+ win: 0b001,
12
+ mac: 0b010,
13
+ linux: 0b100
14
+ }
15
+ end
16
+ end
17
+
18
+ it 'should be empty by default' do
19
+ expect(GameModel.new.platforms).to eq []
20
+ end
21
+
22
+ it 'should let you save data' do
23
+ game = GameModel.new
24
+ game.platforms = [:mac, :linux]
25
+ expect(game.read_attribute(:platforms)).to eq 0b110
26
+ expect(game.platforms).to match_array [:mac, :linux]
27
+ end
28
+
29
+ it "should ignore items that aren't on the list" do
30
+ game = GameModel.new
31
+ game.platforms = [:mac, :linux, :potato]
32
+ expect(game.read_attribute(:platforms)).to eq 0b110
33
+ expect(game.platforms).to match_array [:mac, :linux]
34
+ end
35
+
36
+ it 'should define the list of flags' do
37
+ expect(GameModel.platforms_flags).to eq({
38
+ win: 0b001,
39
+ mac: 0b010,
40
+ linux: 0b100
41
+ })
42
+ end
43
+
44
+ it 'should let you get a join list of flags' do
45
+ expect(GameModel.platforms_flags(:win, :linux)).to eq 0b101
46
+ end
47
+
48
+ it 'should define the reverse list of flags' do
49
+ expect(GameModel.flags_platforms).to eq({
50
+ 0b001 => :win,
51
+ 0b010 => :mac,
52
+ 0b100 => :linux
53
+ })
54
+ end
55
+ end
@@ -0,0 +1,21 @@
1
+ require 'simple_flaggable_column'
2
+ require 'with_model'
3
+
4
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
5
+ ActiveRecord::Migration.verbose = false
6
+
7
+ RSpec.configure do |config|
8
+ config.extend WithModel
9
+
10
+ config.expect_with :rspec do |expectations|
11
+ # Default in RSpec 4
12
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
13
+ end
14
+
15
+ # rspec-mocks config goes here. You can use an alternate test double
16
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
17
+ config.mock_with :rspec do |mocks|
18
+ # Default in RSpec 4.
19
+ mocks.verify_partial_doubles = true
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_flaggable_column
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zequez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-01 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: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: with_model
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ description: This gem adds a concern to allow you to use binary flags columns in ActiveRecord
84
+ models
85
+ email:
86
+ - zequez@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - lib/simple_flaggable_column.rb
97
+ - lib/simple_flaggable_column/version.rb
98
+ - simple_flaggable_column.gemspec
99
+ - spec/simple_flaggable_column/simple_flaggable_column_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/Zequez/simple_flaggable_column
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.4.5
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Simple binary flags columns
125
+ test_files:
126
+ - spec/simple_flaggable_column/simple_flaggable_column_spec.rb
127
+ - spec/spec_helper.rb