acts_as_bytefield 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 848f6a19764646e0ee5abe9fcd0c1e6d46e55e4c
4
- data.tar.gz: 2bfa63187e24543c38a94cc7028e3cb3e2f63e86
3
+ metadata.gz: 8e48d9cf8999f76fa2798a417ed76fb856875354
4
+ data.tar.gz: 83f53488ab4d6bb57538e957f2d57c1e73cf1ae0
5
5
  SHA512:
6
- metadata.gz: f1d08bebbedb75d03e3b4551cb8f517af1774332f22f97081dbe131c114a44023debf7ffcc1a2f9274d41c4ffb2ab74ebf94d445e988105c31dc0b43cc9c6ea8
7
- data.tar.gz: d3908f861357f87d5313897b273eaf5ab3fa5008d9479516e2285736c6375c9bf8be55a943a66b009d252f1e935d7d2e2fdc46348e2cbb6ce78f4430c0ddb971
6
+ metadata.gz: 2fa05ffdcd6936052c4cbb47229963109dccc9f50b2e543e4c7d4e97058ecf716d44e63869049e593ba069ea4c36224bcd94a1a48488ee288373e6efa2e9acfa
7
+ data.tar.gz: 7b96cb09d299caacf418c7fb16c5cca7fdece528d408dccac6cd839bc31d1863c861279a68fdc10aba920104465f28d9a357ad4b50bb078fb2d071c0aa9724fc
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.0.0
3
4
  - 2.2.4
4
5
  before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,6 @@
1
+ *0.1.1* (April 09, 2016)
2
+
3
+ * Use absolute value of integers passed in.
4
+ * Refactor some code and specs.
5
+ * Update README with more detailed usage info.
6
+ * Add ruby 2.0.0 to the travis-ci build
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # ActsAsBytefield
2
2
 
3
- **Version: 0.1.0**
3
+ [![Build Status](https://travis-ci.org/cblackburn/acts_as_bytefield.svg?branch=master)](https://travis-ci.org/cblackburn/acts_as_bytefield)
4
+
5
+ **Version: 0.1.1**
4
6
 
5
7
  Use a string column as a bytefield on an ActiveRecord model.
6
8
 
@@ -14,19 +16,37 @@ class User < ActiveRecord::Base
14
16
  acts_as_bytefield :game_data, keys: [:health, :mana, :ammo]
15
17
  end
16
18
 
17
- user = User.new(health: 100, mana: 100, ammo: 200)
19
+ user = User.create(health: 100, mana: 100, ammo: 200)
18
20
  user.game_data #=> "dd\xC8"
19
-
21
+ user.health #=> 'd'
22
+ user.health.ord #=> 100
23
+ user.mana #=> 'd'
24
+ user.mana.ord #=> 100
25
+ user.ammo #=> "\xC8"
26
+ user.ammo.ord #=> 200
27
+
28
+ # Set integers
20
29
  user.health = 50
21
30
  user.save
22
31
  user.game_data #=> "2d\xC8"
23
-
32
+ user.health #=> '2'
33
+ user.health.ord #=> 50
34
+ user.mana #=> 'd'
35
+ user.mana.ord #=> 100
36
+ user.ammo #=> "\xC8"
37
+ user.ammo.ord #=> 200
38
+
39
+ # Set the original column directly
24
40
  user.game_data = 'ABC'
25
- user.health #=> 65
26
- user.mana #=> 66
27
- user.ammo #=> 67
28
-
29
- user.update_attribute(ammo: 0)
41
+ user.health #=> 'A'
42
+ user.health.ord #=> 65
43
+ user.mana #=> 'B'
44
+ user.mana.ord #=> 66
45
+ user.ammo #=> 'C'
46
+ user.ammo.ord #=> 67
47
+
48
+ # Store zero/null
49
+ user.update_attributes(ammo: 0)
30
50
  user.ammo #=> "\x00"
31
51
  ```
32
52
 
@@ -57,6 +77,46 @@ end
57
77
 
58
78
  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
79
 
80
+ You can still use the string column as a regular string column.
81
+
82
+ ```ruby
83
+ user.game_data = 'this is a test'
84
+
85
+ user.health #=> 't'
86
+ user.mana #=> 'h'
87
+ user.ammo #=> 'i'
88
+ ```
89
+
90
+ #### Using Integers
91
+
92
+ When setting a field to an integer it will convert that integer to a byte with that ordinal value.
93
+
94
+ If the value is negative it will use the absolute value.
95
+
96
+ ```ruby
97
+ user = User.create(health: -100, mana: -100, ammo: -200)
98
+ user.game_data #=> "dd\xC8"
99
+ user.health #=> 'd'
100
+ user.health.ord #=> 100
101
+ user.mana #=> 'd'
102
+ user.mana.ord #=> 100
103
+ user.ammo #=> "\xC8"
104
+ user.ammo.ord #=> 200
105
+ ```
106
+
107
+ Depending on your database encoding, values greater than 255 may raise an encoding error. Sqlite3 seems to have this restriction regardless of the encoding. Your mileage may vary. However, PostgreSQL with `unicode` encoding will allow values up to 1114111 `"\u{10FFFF}"` for each byte field.
108
+
109
+ ```ruby
110
+ user = User.create(health: 1114111, mana: 229, ammo: 51)
111
+ user.game_data #=> "\u{10FFFF}å3"
112
+ user.health #=> "\u{10FFFF}"
113
+ user.health.ord #=> 1114111
114
+ user.mana #=> "å"
115
+ user.mana.ord #=> 229
116
+ user.ammo #=> "3"
117
+ user.ammo.ord #=> 51
118
+ ```
119
+
60
120
  ### Indexing
61
121
 
62
122
  You don't need to do anything special. Indexing works as expected.
@@ -65,10 +125,12 @@ You don't need to do anything special. Indexing works as expected.
65
125
 
66
126
  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
127
 
68
- ## Contributing
128
+ ## Contributing and Support
69
129
 
70
130
  Bug reports and pull requests are welcome on GitHub at https://github.com/cblackburn/acts_as_bytefield.
71
131
 
132
+ I'm fairly responsive. So don't be shy if you have a problem.
133
+
72
134
  ## License
73
135
 
74
136
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -13,34 +13,36 @@ module ActsAsBytefield
13
13
  "Hash expected, got #{options.class.name}"
14
14
  ) unless options.is_a?(Hash) || options.empty?
15
15
 
16
- unless respond_to?(:bytefields)
17
- class_eval { cattr_accessor :bytefields }
18
- self.bytefields = {}
19
- end
20
-
16
+ declare_class_accessors
21
17
  column = column.to_sym
22
18
  bytefields[column] = create_field_hash(options[:keys])
23
- bytefields[column].keys.each do |key|
24
- define_bytefield_methods(column, key)
25
- end
19
+ define_bytefield_methods(column)
26
20
 
27
21
  include ActsAsBytefield::InstanceMethods
28
22
  end
29
23
 
30
24
  private
31
25
 
26
+ def declare_class_accessors
27
+ return nil if respond_to?(:bytefields)
28
+ class_eval { cattr_accessor :bytefields }
29
+ self.bytefields = {}
30
+ end
31
+
32
32
  def create_field_hash(fields)
33
33
  attrs = {}
34
- fields.inject(0) do |n, f|
35
- attrs[f] = n
36
- n += 1
34
+ fields.inject(0) do |index, key|
35
+ attrs[key] = index
36
+ index += 1
37
37
  end
38
38
  attrs
39
39
  end
40
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) }
41
+ def define_bytefield_methods(column)
42
+ bytefields[column].keys.each do |key|
43
+ define_method(key) { bytefield_value(column, key) }
44
+ define_method("#{key}=") { |value| set_bytefield_value(column, key, value) }
45
+ end
44
46
  end
45
47
  end
46
48
 
@@ -73,7 +75,7 @@ module ActsAsBytefield
73
75
  if value.is_a?(String)
74
76
  value.first
75
77
  elsif value.is_a?(Fixnum)
76
- value.chr
78
+ value.abs.chr
77
79
  else
78
80
  fail "#{value} must be a Fixnum or String"
79
81
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsBytefield
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_bytefield
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Blackburn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-08 00:00:00.000000000 Z
11
+ date: 2016-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -118,6 +118,7 @@ files:
118
118
  - ".gitignore"
119
119
  - ".rspec"
120
120
  - ".travis.yml"
121
+ - CHANGELOG
121
122
  - Gemfile
122
123
  - LICENSE.txt
123
124
  - README.md