Class: BitMask

Inherits:
Object
  • Object
show all
Defined in:
lib/bit_mask.rb,
lib/bit_mask/radix.rb,
lib/bit_mask/version.rb

Defined Under Namespace

Modules: Radix Classes: Field

Constant Summary collapse

CHARACTER_SET =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
VERSION =
"0.2.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_attributes = {}) ⇒ BitMask

Returns a new instance of BitMask.



17
18
19
20
21
22
# File 'lib/bit_mask.rb', line 17

def initialize(new_attributes = {})
  @attributes = {}
  self.assign_attributes(
    self.class.defaults.merge(new_attributes)
  )
end

Class Method Details

.check_and_cast_value(key, value) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bit_mask.rb', line 148

def check_and_cast_value(key,value)
  if field = (key.is_a? Field) ? key : self.fields[key]
    return true if field.null && value.nil?
    if values = field.values
      if values.kind_of? Integer
        value = value.to_i
        return nil if value < 0
        return value if (values == -1 || value <= values)
      else
        return value if values.include?(value)
      end
    end
  end
  nil
end

.defaultsObject



141
142
143
144
145
146
# File 'lib/bit_mask.rb', line 141

def defaults
  fields_array = fields.values.map do |field|
    [field.name, field.default]
  end
  Hash[fields_array]
end

.field(name, opts) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bit_mask.rb', line 126

def field(name,opts)
  name = name.to_sym
  self.fields[name] = Field.new(name,opts)

  include(Module.new do
    define_method(name) do
      read_attribute(name)
    end

    define_method("#{name}=") do |*args|
      write_attribute(name,*args)
    end
  end)
end

.from_bin(binary_string) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bit_mask.rb', line 113

def from_bin(binary_string)
  binary_array = binary_string.split('')
  bit_mask = self.new
  self.fields.values.each do |field|
    value = (field.bits == -1) ? binary_array : binary_array.pop(field.bits)
    break if value.nil?
    value = value.join.to_i(2)
    value = field.from_i(value)
    bit_mask.write_attribute(field.name,value)
  end
  bit_mask
end

.from_i(integer) ⇒ Object



109
110
111
# File 'lib/bit_mask.rb', line 109

def from_i(integer)
  self.from_bin(integer.to_s(2))
end

.from_s(string, radix = nil) ⇒ Object Also known as: load



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

def from_s(string,radix = nil)
  characters = self.get_characters(radix)
  self.from_i(Radix.string_to_integer(string, characters))
end

.get_characters(radix = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/bit_mask.rb', line 164

def get_characters(radix = nil)
  radix ||= self.base

  if radix.kind_of?(String)
    radix
  elsif radix.kind_of?(Integer) && radix > 1 && radix <= CHARACTER_SET.length
    CHARACTER_SET[0..radix-1]
  else
    raise "#{radix} is and invali base to convert to. It must be a string or between 2 and #{CHARACTER_SET.length}"
  end
end

.inherited(sub) ⇒ Object



12
13
14
15
# File 'lib/bit_mask.rb', line 12

def self.inherited(sub)
  sub.fields = ActiveSupport::OrderedHash.new
  sub.base = 62
end

.keysObject



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

def keys
  self.fields.keys
end

Instance Method Details

#==(other) ⇒ Object



79
80
81
# File 'lib/bit_mask.rb', line 79

def ==(other)
  self.class == other.class && self.fields == other.fields && self.attributes == other.attributes
end

#[](field) ⇒ Object



32
33
34
# File 'lib/bit_mask.rb', line 32

def [](field)
  self.read_attribute(field)
end

#[]=(field, value) ⇒ Object



36
37
38
# File 'lib/bit_mask.rb', line 36

def []=(field,value)
  self.write_attribute(field,value)
end

#assign_attributes(new_attributes) ⇒ Object Also known as: attributes=



24
25
26
27
28
# File 'lib/bit_mask.rb', line 24

def assign_attributes(new_attributes)
  new_attributes.each do |field, value|
    self.send("#{field}=",value)
  end
end

#attributesObject



83
84
85
86
87
88
# File 'lib/bit_mask.rb', line 83

def attributes
  fields.inject({}) do |attrs, field|
    attrs[field.first] = self.send(field.first)
    attrs
  end
end

#inspectObject



90
91
92
# File 'lib/bit_mask.rb', line 90

def inspect
  self.attributes.inspect
end

#keysObject



73
74
75
# File 'lib/bit_mask.rb', line 73

def keys
  self.class.keys
end

#read_attribute(key) ⇒ Object



40
41
42
# File 'lib/bit_mask.rb', line 40

def read_attribute(key)
  @attributes[key]
end

#to_binObject



56
57
58
59
60
61
# File 'lib/bit_mask.rb', line 56

def to_bin
  self.fields.values.reverse.map do |field|
    val = self.read_attribute(field.name)
    field.to_bin(val)
  end.join('').sub(/\A0+/,'')
end

#to_iObject

converts it to an integer, Good for IDs



64
65
66
# File 'lib/bit_mask.rb', line 64

def to_i
  self.to_bin.to_i(2)
end

#to_s(radix = nil) ⇒ Object Also known as: dump



68
69
70
71
# File 'lib/bit_mask.rb', line 68

def to_s(radix=nil)
  characters = self.class.get_characters(radix)
  Radix.integer_to_string(self.to_i, characters)
end

#write_attribute(key, value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bit_mask.rb', line 44

def write_attribute(key,value)
  if field = self.fields[key]
    if self.class.check_and_cast_value(field,value)
      @attributes[field.name] = value
    else
      raise "Invalid input for #{key}: #{value}  field: #{field}"
    end
  else
    raise "#{key} is an invalid key"
  end
end