aukan-bitmask 0.0.2

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.
File without changes
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2012-11-12
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.md
4
+ README.rdoc
5
+ Rakefile
6
+ lib/aukan-bitmask.rb
7
+ lib/bitmask.rb
8
+ lib/bitmask_attribute.rb
@@ -0,0 +1,79 @@
1
+ aukan-bitmask
2
+ =============
3
+
4
+ Agnostic Bitmask Attribute
5
+
6
+ # Installation
7
+
8
+ ``` sh
9
+ gem install aukan-bitmask
10
+ ```
11
+
12
+ # Bitmask
13
+
14
+ Usage:
15
+
16
+ ```rb
17
+ flags = Bitmask.new({
18
+ :bit_ids => [:flag1, :flag2, :flag3],
19
+ :value => 0
20
+ })
21
+
22
+ # Setting the value of a bit
23
+ flags.set :flag2, true
24
+
25
+ flags.get :flag1 # false
26
+ flags.get :flag2 # true
27
+ flags.get :flag3 # false
28
+
29
+ flags.value # 2
30
+ flags.value.to_s(2) # "10"
31
+ ```
32
+
33
+ Reseting the mask value:
34
+
35
+ ```rb
36
+ flags.value = 5
37
+
38
+ flags.get :flag1 # true
39
+ flags.get :flag2 # false
40
+ flags.get :flag3 # true
41
+ ```
42
+
43
+ Changing the bit ids:
44
+
45
+ ```rb
46
+ flags.value = 5
47
+
48
+ flags.bit_ids = [:flag1, :flag3, :flag2] # Switching flag2 for flag3
49
+
50
+ flags.get :flag1 # true
51
+ flags.get :flag2 # true
52
+ flags.get :flag3 # false
53
+ ```
54
+
55
+ # BitmaskAttribute
56
+
57
+ Uses Bitmask to decorate a class attribute. Can be used on ActiveRecord or any ORM.
58
+
59
+ Usage:
60
+
61
+ ```rb
62
+ class Something
63
+ attr_accessor :flags
64
+
65
+ include BitmaskAttribute
66
+ bitmask_attribute :flags, {
67
+ :bit_ids => [
68
+ :flag1, :flag2, :flag3
69
+ ],
70
+ :default_value => 3
71
+ }
72
+ end
73
+
74
+ algo = Something.new
75
+ algo.flags_bitmask.set(:flag1, false)
76
+
77
+ algo.flags_bitmask.get(:flag1) # false
78
+ algo.flags_bitmask.get(:flag2) # true
79
+ ```
@@ -0,0 +1,48 @@
1
+ = aukan-bitmask
2
+
3
+ * http://github.com/#{github_username}/#{project_name}
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2012 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/bitmask'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'aukan-bitmask' do
14
+ self.developer 'Pablo Antonio Gonzalez Cervantes', 'pbglezc@gmail.com'
15
+ self.rubyforge_name = self.name # TODO this is default value
16
+ self.version = '0.0.2'
17
+ self.summary = 'Agnostic Bitmask and BitmaskAttribute.'
18
+ self.description = 'This gem includes Bitmask for standalone usage, and BitmaskAttribute to decorate an existing attribute on any class.'
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
@@ -0,0 +1,6 @@
1
+ require 'bitmask'
2
+ require 'bitmask_attribute'
3
+
4
+ module AukanBitmask
5
+ VERSION = '0.0.1'
6
+ end
@@ -0,0 +1,53 @@
1
+ class Bitmask
2
+
3
+ attr_accessor :bit_ids, :after_change
4
+ attr_reader :value
5
+
6
+ def initialize ( options = {} )
7
+ default_values = {
8
+ :value => 0,
9
+ :bit_ids => []
10
+ }
11
+ options = default_values.merge(options)
12
+
13
+ # Events
14
+ @after_change = options[:after_change]
15
+
16
+ @value = options[:value].to_i
17
+ @bit_ids = options[:bit_ids]
18
+ end
19
+
20
+ def get ( bit_id )
21
+ position = @bit_ids.index( bit_id )
22
+
23
+ if position == nil
24
+ raise "#{bit_id.inspect} was not included on bit_ids array"
25
+ end
26
+
27
+ return (@value & (2 ** position)) > 0
28
+ end
29
+
30
+ def set ( bit_id, val )
31
+ position = @bit_ids.index( bit_id )
32
+
33
+ if position == nil
34
+ raise "#{bit_id.inspect} was not included on bit_ids array"
35
+ end
36
+
37
+ if val == true
38
+ self.value |= (2 ** position)
39
+ else
40
+ self.value &= ~(2 ** position)
41
+ end
42
+
43
+ @after_change.call(self) if @after_change
44
+
45
+ return self.value
46
+ end
47
+
48
+ def value= ( val )
49
+ @value = val
50
+ @after_change.call(self) if @after_change
51
+ end
52
+
53
+ end
@@ -0,0 +1,33 @@
1
+ module BitmaskAttribute
2
+
3
+ def self.included (base)
4
+ base.extend( ClassMethods )
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def bitmask_attribute ( attribute_name, options = {} )
10
+ default_options = {
11
+ :bitmask_object => attribute_name.to_s + '_bitmask',
12
+ :bit_ids => []
13
+ }
14
+ options = default_options.merge(options)
15
+
16
+ bitmask_obj = options[:bitmask_object]
17
+ class_eval <<-ADD_METHOD
18
+ def #{options[:bitmask_object]}
19
+ @_#{options[:bitmask_object]} ||= Bitmask.new({
20
+ :bit_ids => #{options[:bit_ids].inspect},
21
+ :value => @#{attribute_name},
22
+ :after_change => Proc.new { |bitmask|
23
+ @#{attribute_name} = bitmask.value
24
+ }
25
+ })
26
+ end
27
+ ADD_METHOD
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,73 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestBitmask < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @bitmask = Bitmask.new({
7
+ :bit_ids => [:flag1, :flag2, :flag3]
8
+ })
9
+ end
10
+
11
+ def test_default_value_is_zero
12
+ assert @bitmask.value == 0
13
+ end
14
+
15
+ def test_initializig_with_a_value
16
+ @bitmask = Bitmask.new({
17
+ :value => 3
18
+ })
19
+ assert @bitmask.value == 3
20
+ end
21
+
22
+ def test_initializing_with_bit_ids
23
+ assert @bitmask.bit_ids == [:flag1, :flag2, :flag3]
24
+ end
25
+
26
+ def test_all_flags_are_false
27
+ assert @bitmask.get(:flag1) == false
28
+ assert @bitmask.get(:flag2) == false
29
+ assert @bitmask.get(:flag3) == false
30
+ end
31
+
32
+ def test_raising_a_flag
33
+ @bitmask.set(:flag1, true)
34
+ assert @bitmask.get(:flag1) == true
35
+ end
36
+
37
+ def test_raising_all_flags_by_setting_the_value
38
+ @bitmask.value = 7
39
+ assert @bitmask.get(:flag2) == true
40
+ assert @bitmask.get(:flag1) == true
41
+ assert @bitmask.get(:flag3) == true
42
+ end
43
+
44
+ def test_after_change_event_on_set
45
+ number = 0
46
+ @bitmask.after_change = Proc.new { |bitmask| number = 5 }
47
+
48
+ assert number == 0
49
+ @bitmask.set :flag1, true
50
+ assert number == 5
51
+ end
52
+
53
+ def test_after_change_on_initialization
54
+ number = 0
55
+ @bitmask = Bitmask.new({
56
+ :after_change => Proc.new { |bitmask| number = 5 },
57
+ :bit_ids => [:flag1]
58
+ })
59
+
60
+ assert number == 0
61
+ @bitmask.set :flag1, true
62
+ assert number == 5
63
+ end
64
+
65
+ def test_after_change_event_when_changing_value
66
+ number = 0
67
+ @bitmask.after_change = Proc.new { |bitmask| number = 5 }
68
+
69
+ assert number == 0
70
+ @bitmask.value = 1
71
+ assert number == 5
72
+ end
73
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestBitmaskAttribute < Test::Unit::TestCase
4
+
5
+ class Something
6
+ attr_accessor :flags
7
+
8
+ include BitmaskAttribute
9
+ bitmask_attribute :flags, {
10
+ :bit_ids => [:flag1, :flag2, :flag3]
11
+ }
12
+ end
13
+
14
+ def setup
15
+ @something = Something.new
16
+ @something.flags = nil
17
+ end
18
+
19
+ def test_initialization_respects_the_attribute
20
+ assert @something.flags == nil
21
+ end
22
+
23
+ def test_setting_a_flag_updates_the_attribute
24
+ @something.flags_bitmask.set :flag1, true
25
+ assert @something.flags == 1
26
+ end
27
+
28
+ def test_setting_the_value_updates_the_attribute
29
+ @something.flags_bitmask.value = 3
30
+ assert @something.flags == 3
31
+ end
32
+
33
+ def test_bit_ids
34
+ assert @something.flags_bitmask.bit_ids == [:flag1, :flag2, :flag3]
35
+ end
36
+
37
+ end
@@ -0,0 +1,4 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/bitmask'
4
+ require File.dirname(__FILE__) + '/../lib/bitmask_attribute'
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aukan-bitmask
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Pablo Antonio Gonzalez Cervantes
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-11-13 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 19
28
+ segments:
29
+ - 3
30
+ - 10
31
+ version: "3.10"
32
+ name: rdoc
33
+ prerelease: false
34
+ type: :development
35
+ requirement: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 5
43
+ segments:
44
+ - 1
45
+ - 5
46
+ - 3
47
+ version: 1.5.3
48
+ name: newgem
49
+ prerelease: false
50
+ type: :development
51
+ requirement: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 3
61
+ - 2
62
+ version: "3.2"
63
+ name: hoe
64
+ prerelease: false
65
+ type: :development
66
+ requirement: *id003
67
+ description: This gem includes Bitmask for standalone usage, and BitmaskAttribute to decorate an existing attribute on any class.
68
+ email:
69
+ - pbglezc@gmail.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files:
75
+ - History.txt
76
+ - Manifest.txt
77
+ - README.rdoc
78
+ files:
79
+ - History.txt
80
+ - Manifest.txt
81
+ - README.md
82
+ - README.rdoc
83
+ - Rakefile
84
+ - lib/aukan-bitmask.rb
85
+ - lib/bitmask.rb
86
+ - lib/bitmask_attribute.rb
87
+ - test/test_bitmask_attribute.rb
88
+ - test/test_bitmask.rb
89
+ - test/test_helper.rb
90
+ - .gemtest
91
+ has_rdoc: true
92
+ homepage: http://github.com/#{github_username}/#{project_name}
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options:
97
+ - --main
98
+ - README.rdoc
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ requirements: []
120
+
121
+ rubyforge_project: aukan-bitmask
122
+ rubygems_version: 1.5.2
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Agnostic Bitmask and BitmaskAttribute.
126
+ test_files:
127
+ - test/test_bitmask_attribute.rb
128
+ - test/test_bitmask.rb
129
+ - test/test_helper.rb