jsanders-bitfields 0.2.8 → 0.3.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.
- data/lib/bitfields.rb +59 -28
- metadata +2 -2
data/lib/bitfields.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/bits'
|
2
2
|
|
3
3
|
class BitFields
|
4
|
-
|
4
|
+
|
5
5
|
# Class methods
|
6
|
-
|
6
|
+
|
7
7
|
def self.template(template_name, &block)
|
8
8
|
template_name = symbol_if_string(template_name)
|
9
9
|
templates[template_name] = block
|
10
10
|
nil
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def self.create(template_name, params = {})
|
14
14
|
template_name = symbol_if_string(template_name)
|
15
15
|
if templates[template_name]
|
16
16
|
instance = self.new
|
17
17
|
templates[template_name].call(instance)
|
18
18
|
params.each_pair { | field_name, value | instance[field_name] = value }
|
19
|
+
regexs[ instance.regex ] ||= template_name
|
19
20
|
instance
|
20
21
|
else
|
21
22
|
warn "BitFields: No template named #{template_name}"
|
@@ -23,23 +24,31 @@ class BitFields
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
length = instance.field_lengths[index]
|
35
|
-
instance[field_name] = bit_string[current_index, length].to_i(2)
|
36
|
-
current_index += length
|
27
|
+
# +message+ is assumed to be a string of 1's and 0's representing a binary number
|
28
|
+
# TODO: determine format of message and convert to a consistent representation
|
29
|
+
def self.decode( message )
|
30
|
+
template_name = nil
|
31
|
+
regexs.detect do | key, value |
|
32
|
+
if message[ key ]
|
33
|
+
template_name = value
|
34
|
+
break
|
37
35
|
end
|
38
|
-
instance
|
39
|
-
else
|
40
|
-
warn "BitFields: No template named #{template_name}"
|
41
|
-
nil
|
42
36
|
end
|
37
|
+
|
38
|
+
_, *matches = $~.to_a
|
39
|
+
generate_instance_from_match_data template_name, matches
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.decode_from_template_name(template_name, message)
|
43
|
+
regexs.each do | key, value |
|
44
|
+
if value == template_name
|
45
|
+
message[ key ]
|
46
|
+
break
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
_, *matches = $~.to_a
|
51
|
+
generate_instance_from_match_data template_name, matches
|
43
52
|
end
|
44
53
|
|
45
54
|
def self.compose(&block)
|
@@ -49,7 +58,7 @@ class BitFields
|
|
49
58
|
end
|
50
59
|
|
51
60
|
# Object methods
|
52
|
-
|
61
|
+
|
53
62
|
def integer; get_bits.integer end
|
54
63
|
def bit_string; get_bits.bit_string end
|
55
64
|
def hex_string; get_bits.hex_string end
|
@@ -64,15 +73,7 @@ class BitFields
|
|
64
73
|
if instance
|
65
74
|
field_names << field_name
|
66
75
|
field_values << instance.get_bits
|
67
|
-
|
68
|
-
end
|
69
|
-
|
70
|
-
def bring(template_name, params = {})
|
71
|
-
template_name = BitFields.symbol_if_string(template_name)
|
72
|
-
instance = BitFields.create(template_name, params)
|
73
|
-
if instance
|
74
|
-
field_names << template_name
|
75
|
-
field_values << instance.get_bits
|
76
|
+
field_regexs += instance.field_regexes.dup
|
76
77
|
end
|
77
78
|
end
|
78
79
|
|
@@ -83,6 +84,8 @@ class BitFields
|
|
83
84
|
field_names << field_name
|
84
85
|
field_values << Bits.new(value, length)
|
85
86
|
field_lengths << length
|
87
|
+
l = length != 0 ? "{#{length},#{length}}" : "*?"
|
88
|
+
field_regexs << "([01]#{l})"
|
86
89
|
end
|
87
90
|
|
88
91
|
def get_bits
|
@@ -112,16 +115,44 @@ class BitFields
|
|
112
115
|
@field_lengths ||= Array.new
|
113
116
|
end
|
114
117
|
|
118
|
+
def field_regexs
|
119
|
+
@field_regexs ||= Array.new
|
120
|
+
end
|
121
|
+
|
122
|
+
def regex
|
123
|
+
Regexp.new('^' + field_regexs.join + '$')
|
124
|
+
end
|
125
|
+
|
126
|
+
|
115
127
|
private
|
116
128
|
|
117
129
|
def self.templates
|
118
130
|
@@templates ||= Hash.new
|
119
131
|
end
|
120
132
|
|
133
|
+
def self.regexs
|
134
|
+
@@regexs ||= Hash.new
|
135
|
+
end
|
136
|
+
|
121
137
|
# Helpers
|
122
138
|
|
123
139
|
def self.symbol_if_string(name)
|
124
140
|
name.kind_of?(String) ? name.to_sym : name
|
125
141
|
end
|
142
|
+
|
143
|
+
def self.generate_instance_from_match_data( template_name, matches = [] )
|
144
|
+
return nil if matches.empty?
|
145
|
+
|
146
|
+
instance = self.new
|
147
|
+
templates[ template_name ].call(instance)
|
148
|
+
fields = instance.field_names
|
149
|
+
|
150
|
+
fields.each do | item |
|
151
|
+
instance[ item ] = matches.shift.to_i(2)
|
152
|
+
end
|
153
|
+
|
154
|
+
instance
|
155
|
+
end
|
156
|
+
|
126
157
|
end
|
127
158
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsanders-bitfields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Sanders
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-30 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|