array_bit_mask 0.0.3

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.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .idea
20
+ .rvmrc
21
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alex Leschenko
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,42 @@
1
+ # ArrayBitMask
2
+
3
+ Creates methods that accepts array of values and save them as bit mask to the attribute with an "_mask" suffix using specified source.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'array_bit_mask'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install array_bit_mask
18
+
19
+ ## Usage
20
+
21
+ Call `as_bit_mask` in an ActiveRecord class and pass the name of the attribute you wish to be the accessor for array of values.
22
+ In the option `:source` you can specify array of values or method name which return an array.
23
+ Bit mask would be saved to attribute with an "_mask" suffix, but you can also specify it in `:column` option
24
+
25
+ ```ruby
26
+ class Permission < ActiveRecord::Base
27
+ as_bit_mask :actions, :source => [:create, :show, :update]
28
+ end
29
+
30
+ permission = Permission.new
31
+ permission.actions = [:show, :update]
32
+ permission.actions_mask
33
+ => 6
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'array_bit_mask/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'array_bit_mask'
8
+ gem.version = ArrayBitMask::VERSION
9
+ gem.authors = ['Alex Leschenko']
10
+ gem.email = %w(leschenko.al@gmail.com)
11
+ gem.summary = %q{Generates bit mask for attribute from array of values}
12
+ gem.description = %q{Creates methods that accepts array of values and save them as bit mask extracted using specified source}
13
+ gem.homepage = 'https://github.com/leschenko/array_bit_mask'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = %w(lib)
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_runtime_dependency 'activesupport'
23
+ end
@@ -0,0 +1,9 @@
1
+ module ArrayBitMask
2
+ class Railtie < Rails::Railtie
3
+ initializer 'array_bit_mask.model_additions' do
4
+ ActiveSupport.on_load :active_record do
5
+ include ArrayBitMask
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ArrayBitMask
2
+ VERSION = '0.0.3'
3
+ end
@@ -0,0 +1,47 @@
1
+ require 'array_bit_mask/version'
2
+ require 'array_bit_mask/railtie' if defined? Rails
3
+
4
+ module ArrayBitMask
5
+ def self.included(base)
6
+ base.send(:extend, ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ # Creates methods that accepts array of values and save them as bit mask to the attribute with an "_mask" suffix using specified source
11
+ #
12
+ # === Options
13
+ # [:source]
14
+ # Specify array of values of method name which should return an array.
15
+ # [:column]
16
+ # Specify the column which would be used to store bit mask. By default, this is an attribute with an "_mask" suffix.
17
+ #
18
+ # === Example
19
+ # as_bit_mask :actions, :source => [:create, :show, :update], :column => :my_mask_column
20
+ # as_bit_mask :actions, :source => :subject_actions
21
+ # will save bit mask to :actions_mask attribute
22
+ def as_bit_mask(attr, options={})
23
+ return 0 unless options[:source]
24
+
25
+ options.reverse_merge!(:column => "#{attr}_mask")
26
+
27
+ define_method "#{attr}=" do |val|
28
+ source = bit_mask_source_for(options[:source])
29
+ values = val.map(&:to_sym) & source
30
+ res = self.send("#{options[:column]}=", values.map { |a| 2**source.index(a) }.sum)
31
+ instance_variable_set("@#{attr}", values)
32
+ end
33
+
34
+ define_method attr do
35
+ instance_variable_get("@#{attr}") || begin
36
+ source = bit_mask_source_for(options[:source])
37
+ res = source.reject { |r| ((self.send(options[:column]) || 0) & 2**source.index(r)).zero? }
38
+ instance_variable_set("@#{attr}", res)
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def bit_mask_source_for(source)
45
+ source.is_a?(Array) ? source : send(source)
46
+ end
47
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ class WithBitMask
4
+ attr_accessor :colors_mask, :second_colors_mask
5
+
6
+ include ArrayBitMask
7
+ as_bit_mask :colors, :source => :subject_colors
8
+ as_bit_mask :second_colors, :source => [:red, :green, :black, :white]
9
+
10
+ def subject_colors
11
+ [:red, :green, :black, :white]
12
+ end
13
+ end
14
+
15
+ describe ArrayBitMask do
16
+ subject { WithBitMask.new }
17
+
18
+ it 'should add accessor methods' do
19
+ should respond_to(:colors)
20
+ should respond_to(:colors=)
21
+ end
22
+
23
+ it 'should store bit mask' do
24
+ subject.colors = [:red, :white]
25
+ subject.colors_mask.should eq(9)
26
+ end
27
+
28
+ it 'should restore array from bit mask' do
29
+ subject.colors_mask = 6
30
+ subject.colors.should =~ [:green, :black]
31
+ end
32
+
33
+ it 'should return empty array for zero mask' do
34
+ subject.colors_mask = 0
35
+ subject.colors.should =~ []
36
+ end
37
+
38
+ it 'should store array' do
39
+ subject.colors = []
40
+ subject.colors.should eq([])
41
+ end
42
+
43
+ it 'should return empty array for nil mask' do
44
+ subject.colors_mask = nil
45
+ subject.colors.should =~ []
46
+ end
47
+
48
+ context 'source as array' do
49
+ it 'should store bit mask' do
50
+ subject.second_colors = [:red, :white]
51
+ subject.second_colors_mask.should eq(9)
52
+ end
53
+
54
+ it 'should restore array from bit mask' do
55
+ subject.second_colors_mask = 6
56
+ subject.second_colors.should =~ [:green, :black]
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,2 @@
1
+ require 'active_support/core_ext'
2
+ require 'array_bit_mask'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_bit_mask
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.3
6
+ platform: ruby
7
+ authors:
8
+ - Alex Leschenko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ none: false
21
+ prerelease: false
22
+ name: rake
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ none: false
29
+ type: :development
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ none: false
37
+ prerelease: false
38
+ name: rspec
39
+ requirement: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ none: false
45
+ type: :development
46
+ - !ruby/object:Gem::Dependency
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ none: false
53
+ prerelease: false
54
+ name: activesupport
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ none: false
61
+ type: :runtime
62
+ description: Creates methods that accepts array of values and save them as bit mask
63
+ extracted using specified source
64
+ email:
65
+ - leschenko.al@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - array_bit_mask.gemspec
76
+ - lib/array_bit_mask.rb
77
+ - lib/array_bit_mask/railtie.rb
78
+ - lib/array_bit_mask/version.rb
79
+ - spec/array_bit_mask/bit_mask_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/leschenko/array_bit_mask
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: -2168014349249705125
95
+ none: false
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: -2168014349249705125
104
+ none: false
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.24
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Generates bit mask for attribute from array of values
111
+ test_files:
112
+ - spec/array_bit_mask/bit_mask_spec.rb
113
+ - spec/spec_helper.rb