bit_field_serializer 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: 6dbe1b014b6f209f67b3e54a61e279d7c4ebae3f
4
+ data.tar.gz: ce80d770cc911615518db816bf1fa045035d7905
5
+ SHA512:
6
+ metadata.gz: f17a39697874f392e80ffcb0155ba1537de33d187624a9b303a3fee8f01ef2637e222fd65df3b41bfe579d7e4ae28d32552b06dd2e0fe36bd59423f8aa201930
7
+ data.tar.gz: f0146ea5c1e75a1d2ada1e6d6270d3717cd596927ae6201663860f1b15f371e3bed1bc4be820b3b0752ec5bc62cd8bd13874b6c3c9298f670f805b3689626d6c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bit_field_serializer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Scrimmage
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # BitFieldSerializer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bit_field_serializer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bit_field_serializer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "lib"
6
+ t.test_files = FileList["test/*_test.rb"]
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bit_field_serializer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bit_field_serializer"
8
+ spec.version = BitFieldSerializer::VERSION
9
+ spec.authors = ["Nick DeLuca"]
10
+ spec.email = ["nick@wescrimmage.com"]
11
+ spec.description = %q{Handles conversion of enum arrays to and from a bit field}
12
+ spec.summary = %q{Handles conversion of enum arrays to and from a bit field}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest", "~> 5.0"
23
+ end
@@ -0,0 +1,11 @@
1
+ require "bit_field_serializer/version"
2
+
3
+ module BitFieldSerializer
4
+ def enum_array_to_integer(array)
5
+ array.compact.reduce(0) { |bit_field, t| bit_field |= t.to_i; bit_field }
6
+ end
7
+
8
+ def integer_to_enum_array(collection, integer)
9
+ collection.values.collect { |t| t if (integer & t == t) }.compact
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module BitFieldSerializer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,75 @@
1
+ require_relative './test_helper'
2
+
3
+ class TestBitFieldSerializer < MiniTest::Test
4
+
5
+ def setup
6
+ @serializer = Class.new
7
+ @serializer.extend(BitFieldSerializer)
8
+ end
9
+
10
+ def collection
11
+ { 'enum1' => 0, 'enum2' => 1, 'enum3' => 2, 'enum4' => 4, 'enum5' => 8, 'enum6' => 16 }
12
+ end
13
+
14
+ def test_converts_enum_array_to_integer
15
+ integer = @serializer.enum_array_to_integer([ 0, 1, 2, 4, 8, 16 ])
16
+ assert_equal(31, integer)
17
+ end
18
+
19
+ def test_conversion_ignores_nil_values
20
+ integer = @serializer.enum_array_to_integer([ nil, 1, 2, nil, 4, 8, 16, nil ])
21
+ assert_equal(31, integer)
22
+ end
23
+
24
+ def test_integer_to_enum_array_with_31
25
+ result = @serializer.integer_to_enum_array(collection, 31)
26
+
27
+ assert_equal([0, 1, 2, 4, 8, 16], result)
28
+ end
29
+
30
+ def test_integer_to_enum_array_missing_power_in_enum
31
+ collection = { 'enum1' => 0, 'enum2' => 1, 'enum3' => 2, 'enum5' => 8, 'enum6' => 16 }
32
+
33
+ result = @serializer.integer_to_enum_array(collection, 31)
34
+
35
+ assert_equal([0, 1, 2, 8, 16], result)
36
+ end
37
+
38
+ def test_integer_to_enum_array_with_16
39
+ result = @serializer.integer_to_enum_array(collection, 16)
40
+
41
+ assert_equal([0, 16], result)
42
+ end
43
+
44
+ def test_integer_to_enum_array_with_2
45
+ result = @serializer.integer_to_enum_array(collection, 2)
46
+
47
+ assert_equal([0, 2], result)
48
+ end
49
+
50
+ def test_integer_to_enum_array_with_22
51
+ result = @serializer.integer_to_enum_array(collection, 22)
52
+
53
+ assert_equal([0, 2, 4, 16], result)
54
+ end
55
+
56
+ def test_conversion_and_deconversion_with_2
57
+ result = @serializer.integer_to_enum_array(collection, 2)
58
+
59
+ assert_equal([0, 2], result)
60
+
61
+ integer = @serializer.enum_array_to_integer(result)
62
+
63
+ assert_equal(2, integer)
64
+ end
65
+
66
+ def test_conversion_and_deconversion_with_12
67
+ result = @serializer.integer_to_enum_array(collection, 12)
68
+
69
+ assert_equal([0, 4, 8], result)
70
+
71
+ integer = @serializer.enum_array_to_integer(result)
72
+
73
+ assert_equal(12, integer)
74
+ end
75
+ end
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'bit_field_serializer'
4
+ #require File.expand_path('../lib/bit_field_serializer.rb', __FILE__)
@@ -0,0 +1,8 @@
1
+ require_relative './test_helper'
2
+
3
+ class TestBitFieldSerializer < MiniTest::Test
4
+
5
+ def test_version_is_correct
6
+ assert_equal BitFieldSerializer::VERSION, '0.0.1'
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bit_field_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nick DeLuca
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-05 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: 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
+ description: Handles conversion of enum arrays to and from a bit field
56
+ email:
57
+ - nick@wescrimmage.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - bit_field_serializer.gemspec
68
+ - lib/bit_field_serializer.rb
69
+ - lib/bit_field_serializer/version.rb
70
+ - test/bit_field_serializer_test.rb
71
+ - test/test_helper.rb
72
+ - test/version_test.rb
73
+ homepage:
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.1.11
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Handles conversion of enum arrays to and from a bit field
97
+ test_files:
98
+ - test/bit_field_serializer_test.rb
99
+ - test/test_helper.rb
100
+ - test/version_test.rb