acts_as_bytefield 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 848f6a19764646e0ee5abe9fcd0c1e6d46e55e4c
4
+ data.tar.gz: 2bfa63187e24543c38a94cc7028e3cb3e2f63e86
5
+ SHA512:
6
+ metadata.gz: f1d08bebbedb75d03e3b4551cb8f517af1774332f22f97081dbe131c114a44023debf7ffcc1a2f9274d41c4ffb2ab74ebf94d445e988105c31dc0b43cc9c6ea8
7
+ data.tar.gz: d3908f861357f87d5313897b273eaf5ab3fa5008d9479516e2285736c6375c9bf8be55a943a66b009d252f1e935d7d2e2fdc46348e2cbb6ce78f4430c0ddb971
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_bytefield.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Chris Blackburn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # ActsAsBytefield
2
+
3
+ **Version: 0.1.0**
4
+
5
+ Use a string column as a bytefield on an ActiveRecord model.
6
+
7
+ Requires Rails 4.x. Untested with other versions.
8
+
9
+ ## Example
10
+
11
+ ```ruby
12
+ class User < ActiveRecord::Base
13
+ include ActsAsBytefield
14
+ acts_as_bytefield :game_data, keys: [:health, :mana, :ammo]
15
+ end
16
+
17
+ user = User.new(health: 100, mana: 100, ammo: 200)
18
+ user.game_data #=> "dd\xC8"
19
+
20
+ user.health = 50
21
+ user.save
22
+ user.game_data #=> "2d\xC8"
23
+
24
+ user.game_data = 'ABC'
25
+ user.health #=> 65
26
+ user.mana #=> 66
27
+ user.ammo #=> 67
28
+
29
+ user.update_attribute(ammo: 0)
30
+ user.ammo #=> "\x00"
31
+ ```
32
+
33
+ ## Installation
34
+
35
+ Add this line to your application's Gemfile:
36
+
37
+ ```ruby
38
+ gem 'acts_as_bytefield'
39
+ ```
40
+
41
+ And then execute:
42
+
43
+ $ bundle
44
+
45
+ ## Usage
46
+
47
+ ### Model
48
+
49
+ Include the ActsAsBytefield module, and pass the column name to `acts_as_bytefield` along with the keys you want to use.
50
+
51
+ ```ruby
52
+ class User < ActiveRecord::Base
53
+ include ActsAsBytefield
54
+ acts_as_bytefield :game_data, keys: [:health, :mana, :ammo]
55
+ end
56
+ ```
57
+
58
+ The order of the keys matches the byte order of the string column. So, in the above example `game_data` with contain 3 bytes, from left to right representing `health`, `mana` and `ammo`.
59
+
60
+ ### Indexing
61
+
62
+ You don't need to do anything special. Indexing works as expected.
63
+
64
+ ### Database Issues
65
+
66
+ It has been tested with `sqlite3` and `postgres`. Make sure you pay attention to the encoding as things can change depending on how you set that up.
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cblackburn/acts_as_bytefield.
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
6
+
7
+ begin
8
+ require 'midwire_common/rake_tasks'
9
+ rescue StandardError => e
10
+ puts(">>> Can't load midwire_common/rake_tasks.")
11
+ puts(">>> Did you 'bundle exec'?: #{e.message}")
12
+ exit
13
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_bytefield/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'acts_as_bytefield'
8
+ spec.version = ActsAsBytefield::VERSION
9
+ spec.authors = ['Chris Blackburn']
10
+ spec.email = ['87a1779b@opayq.com']
11
+
12
+ spec.summary = 'Change an ActiveRecord string column into a byte field.'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/cblackburn/acts_as_bytefield'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.required_ruby_version = '>= 2.0.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'midwire_common', '~> 1.1.1'
28
+ spec.add_development_dependency 'pry-nav', '~> 0.2.4'
29
+ spec.add_development_dependency 'sqlite3', '~> 1.3.11'
30
+
31
+ spec.add_dependency 'activerecord', '~> 4.0'
32
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acts_as_bytefield"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module ActsAsBytefield
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,85 @@
1
+ require 'acts_as_bytefield/version'
2
+ require 'active_record'
3
+
4
+ module ActsAsBytefield
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def acts_as_bytefield(column, options = {})
11
+ fail(
12
+ ArgumentError,
13
+ "Hash expected, got #{options.class.name}"
14
+ ) unless options.is_a?(Hash) || options.empty?
15
+
16
+ unless respond_to?(:bytefields)
17
+ class_eval { cattr_accessor :bytefields }
18
+ self.bytefields = {}
19
+ end
20
+
21
+ column = column.to_sym
22
+ bytefields[column] = create_field_hash(options[:keys])
23
+ bytefields[column].keys.each do |key|
24
+ define_bytefield_methods(column, key)
25
+ end
26
+
27
+ include ActsAsBytefield::InstanceMethods
28
+ end
29
+
30
+ private
31
+
32
+ def create_field_hash(fields)
33
+ attrs = {}
34
+ fields.inject(0) do |n, f|
35
+ attrs[f] = n
36
+ n += 1
37
+ end
38
+ attrs
39
+ end
40
+
41
+ def define_bytefield_methods(column, field)
42
+ define_method(field) { bytefield_value(column, field) }
43
+ define_method("#{field}=") { |value| set_bytefield_value(column, field, value) }
44
+ end
45
+ end
46
+
47
+ module InstanceMethods
48
+ def bytefield_value(column, field)
49
+ self[column][byte_index(column, field)]
50
+ end
51
+
52
+ def set_bytefield_value(column, field, value)
53
+ self[column] ||= ''
54
+ null_pad(column)
55
+ self[column][byte_index(column, field)] = guarded_value(value)
56
+ end
57
+
58
+ private
59
+
60
+ def null_pad(column)
61
+ self[column] << "\0" * (column_length(column) - self[column].length)
62
+ end
63
+
64
+ def column_length(column)
65
+ bytefields[column].keys.count
66
+ end
67
+
68
+ def byte_index(column, field)
69
+ bytefields[column][field]
70
+ end
71
+
72
+ def guarded_value(value)
73
+ if value.is_a?(String)
74
+ value.first
75
+ elsif value.is_a?(Fixnum)
76
+ value.chr
77
+ else
78
+ fail "#{value} must be a Fixnum or String"
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ # Add class methods
85
+ ActiveRecord::Base.send(:extend, ActsAsBytefield)
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_bytefield
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Blackburn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-08 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: midwire_common
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.1.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-nav
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.2.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.2.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.11
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.11
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.0'
111
+ description: Change an ActiveRecord string column into a byte field.
112
+ email:
113
+ - 87a1779b@opayq.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - acts_as_bytefield.gemspec
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/acts_as_bytefield.rb
129
+ - lib/acts_as_bytefield/version.rb
130
+ homepage: https://github.com/cblackburn/acts_as_bytefield
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 2.0.0
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.4.5.1
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Change an ActiveRecord string column into a byte field.
154
+ test_files: []