Class: BitFields

Inherits:
Object
  • Object
show all
Defined in:
lib/bitfields.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compose {|composition| ... } ⇒ Object

Yields:

  • (composition)


54
55
56
57
58
# File 'lib/bitfields.rb', line 54

def self.compose(&block)
  composition = self.new
  yield composition
  composition
end

.create(template_name, params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bitfields.rb', line 13

def self.create(template_name, params = {})
  template_name = symbol_if_string(template_name)
  if templates[template_name]
    instance = self.new
    templates[template_name].call(instance)
    params.each_pair { | field_name, value | instance[field_name] = value }
    regexs[ instance.regex ] ||= template_name
    instance
  else
    warn "BitFields: No template named #{template_name}"
    nil
  end
end

.decode(message) ⇒ Object

message is assumed to be a string of 1’s and 0’s representing a binary number TODO: determine format of message and convert to a consistent representation



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bitfields.rb', line 29

def self.decode( message )
  template_name = nil
  regexs.detect do | key, value |
    if message[ key ]
      template_name = value
      break
    end
  end

  _, *matches = $~.to_a
  generate_instance_from_match_data template_name, matches
end

.decode_from_template_name(template_name, message) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bitfields.rb', line 42

def self.decode_from_template_name(template_name, message)
  regexs.each do | key, value |
    if value == template_name
      message[ key ]
      break
    end
  end

  _, *matches = $~.to_a
  generate_instance_from_match_data template_name, matches
end

.template(template_name, &block) ⇒ Object

Class methods



7
8
9
10
11
# File 'lib/bitfields.rb', line 7

def self.template(template_name, &block)
  template_name = symbol_if_string(template_name)
  templates[template_name] = block
  nil
end

Instance Method Details

#[](field_name) ⇒ Object



97
98
99
100
# File 'lib/bitfields.rb', line 97

def [](field_name)
  field_name = BitFields.symbol_if_string(field_name)
  field_values[field_names.index(field_name)] if field_names.index(field_name)
end

#[]=(field_name, value) ⇒ Object



102
103
104
# File 'lib/bitfields.rb', line 102

def []=(field_name, value)
  self[field_name].set(value) if self[field_name]
end

#add(field_name, instance) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/bitfields.rb', line 71

def add(field_name, instance)
  field_name = BitFields.symbol_if_string(field_name)
  if instance
    field_names << field_name
    field_values << instance.get_bits
    field_regexs += instance.field_regexes.dup
  end
end

#bit_stringObject



63
# File 'lib/bitfields.rb', line 63

def bit_string; get_bits.bit_string end

#bits(field_name, params = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/bitfields.rb', line 80

def bits(field_name, params = {})
  field_name = BitFields.symbol_if_string(field_name)
  value = params.key?(:value) ? params[:value] : 0
  length = params.key?(:length) ? params[:length] : 0
  field_names << field_name
  field_values << Bits.new(value, length)
  field_lengths << length
  l = length != 0 ? "{#{length},#{length}}" : "*?"
  field_regexs << "([01]#{l})"
end

#field_lengthsObject



114
115
116
# File 'lib/bitfields.rb', line 114

def field_lengths
  @field_lengths ||= Array.new
end

#field_namesObject



106
107
108
# File 'lib/bitfields.rb', line 106

def field_names
  @field_names ||= Array.new
end

#field_regexsObject



118
119
120
# File 'lib/bitfields.rb', line 118

def field_regexs
  @field_regexs ||= Array.new
end

#field_valuesObject



110
111
112
# File 'lib/bitfields.rb', line 110

def field_values
  @field_values ||= Array.new
end

#get_bitsObject



91
92
93
94
95
# File 'lib/bitfields.rb', line 91

def get_bits
  field_values.inject(nil) do |final, value|
    final ? final + value : value
  end
end

#hex_stringObject



64
# File 'lib/bitfields.rb', line 64

def hex_string; get_bits.hex_string end

#integerObject

Object methods



62
# File 'lib/bitfields.rb', line 62

def integer; get_bits.integer end

#oct_stringObject



65
# File 'lib/bitfields.rb', line 65

def oct_string; get_bits.oct_string end

#regexObject



122
123
124
# File 'lib/bitfields.rb', line 122

def regex
  Regexp.new('^' + field_regexs.join + '$')
end

#total_lengthObject



67
68
69
# File 'lib/bitfields.rb', line 67

def total_length
  field_lengths.inject { |total, length| total + length }
end