bit_mask 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -63,7 +63,7 @@ When writing these config maps. Try to put the mose used options first so the st
63
63
 
64
64
  * Impliment more tests
65
65
  * Storage of strings?
66
- * If binary options and default 1, user inverse
66
+ * Allow for embeded bit_masks
67
67
  * create javascript version for easy communication between ruby and javascript
68
68
 
69
69
  == Contributing to bit_mask
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/bit_mask.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bit_mask}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Ryan Ong}]
12
- s.date = %q{2011-07-18}
12
+ s.date = %q{2011-07-21}
13
13
  s.description = %q{bit_mask creates a simple api to create bit mask models. By bit masking dataing you can compress the amount of data that needs to be sent between servers and clients}
14
14
  s.email = %q{ryanong@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/bit_mask.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'active_support/core_ext/class/attribute'
2
2
 
3
3
  class BitMask
4
- CHARACTER_SET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
4
+ CHARACTER_SET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
5
5
  class_attribute :fields, :defaults, :base
6
6
  def self.inherited(sub)
7
7
  sub.fields = []
@@ -22,12 +22,12 @@ class BitMask
22
22
  alias_method :attributes=, :replace
23
23
 
24
24
  def [](field)
25
- raise "#{field} is an invalid key" unless self.class.keys.include? field.to_sym
25
+ raise "#{field} is an invalid key" unless self.respond_to?(field)
26
26
  self.send(field)
27
27
  end
28
28
 
29
29
  def []=(field,value)
30
- raise "#{field} is an invalid key" unless self.class.keys.include? field.to_sym
30
+ raise "#{field} is an invalid key" unless self.respond_to?("#{field}=")
31
31
  self.send("#{field}=",value)
32
32
  end
33
33
 
@@ -50,8 +50,15 @@ class BitMask
50
50
  def to_bin
51
51
  self.fields.reverse.map do |field,conf|
52
52
  val = self.read_attribute(field)
53
- val = conf[:values].index(val) if conf[:values].respond_to? :index
54
- val.to_i.to_s(2).rjust(conf[:bits],'0')
53
+ if conf[:nil] && val.nil?
54
+ val = 0
55
+ elsif conf[:values].respond_to? :index
56
+ val = conf[:values].index(val)
57
+ val += 1 if conf[:nil]
58
+ end
59
+ val = val.to_i.to_s(2)
60
+ val = val.rjust(conf[:bits],'0') if conf[:bits] > 0
61
+ val
55
62
  end.join('').sub(/\A0+/,'')
56
63
  end
57
64
 
@@ -119,7 +126,7 @@ class BitMask
119
126
  end
120
127
 
121
128
  int_val = 0
122
- string.reverse.split(//).each_with_index do |char,index|
129
+ string.reverse.split('').each_with_index do |char,index|
123
130
  raise ArgumentError, "Character #{char} at index #{index} is not a valid character for to_insane Base #{radix} String." unless char_index = char_ref.index(char)
124
131
  int_val += (char_index)*(radix**(index))
125
132
  end
@@ -139,7 +146,11 @@ class BitMask
139
146
  value = (conf[:bits] == -1) ? binary_array : binary_array.pop(conf[:bits])
140
147
  break if value.nil?
141
148
  value = value.join('').to_i(2)
142
- if conf[:values].respond_to?(:at)
149
+ value -= 1 if conf[:nil]
150
+
151
+ if conf[:nil] && value == -1
152
+ value = nil
153
+ elsif conf[:values].respond_to?(:at)
143
154
  value = conf[:values].at(value)
144
155
  elsif conf[:values].respond_to?(:from_i)
145
156
  value = conf[:values].from_i(value)
@@ -172,9 +183,15 @@ class BitMask
172
183
  elsif opts[:values].respond_to? :bit_length
173
184
  opts[:bits] = opts[:values].bit_length
174
185
  elsif opts[:values].kind_of? Integer
175
- opts[:limit] = opts[:values]
186
+ if opts[:values] == -1
187
+ opts[:bits] = -1
188
+ else
189
+ opts[:limit] = opts[:values]
190
+ opts[:limit] += 1 if opts[:nil]
191
+ end
176
192
  else
177
193
  opts[:limit] = opts[:values].size
194
+ opts[:limit] += 1 if opts[:nil]
178
195
  end
179
196
  end
180
197
  opts[:bits] ||= (opts[:limit].to_i-1).to_s(2).length
@@ -84,4 +84,24 @@ describe BitMask do
84
84
  new = CarSearch.from_s(modified_conf.to_s('qwerty'),'qwerty')
85
85
  new.attributes.should == modified_conf.attributes
86
86
  end
87
+
88
+ it "should have defaul nil values" do
89
+ search = DealershipSearch.new
90
+ search.zip.should == nil
91
+ end
92
+
93
+ it "should have defaul nil values" do
94
+ search = DealershipSearch.new
95
+ search.makes = 'honda'
96
+ search.zip.should == nil
97
+ new = DealershipSearch.from_s(search.to_s)
98
+ new.attributes.should == search.attributes
99
+ end
100
+
101
+ it "should allow for infinite values" do
102
+ search = DealershipSearch.new
103
+ search.distance = 9999999999999
104
+ loaded = DealershipSearch.from_bin(search.to_bin)
105
+ loaded.distance.should == search.distance
106
+ end
87
107
  end
@@ -1,5 +1,5 @@
1
1
  class DealershipSearch < BitMask
2
- field :makes, :values => ['honda','toyota','ford']
3
- field :zip, :values => 99999
2
+ field :makes, :values => ['honda','toyota','ford'], :nil => true
3
+ field :zip, :values => 99999, :nil => true
4
4
  field :distance, :values => -1
5
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: bit_mask
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Ong
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-18 00:00:00 Z
13
+ date: 2011-07-21 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -114,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- hash: -3458846048979533380
117
+ hash: 4147955215867335187
118
118
  segments:
119
119
  - 0
120
120
  version: "0"