class BitHash

Attributes

default[RW]
options[RW]

Public Class Methods

new(config_map=nil, *options) click to toggle source

look below at load_config_map for config_map schema base is the base value you want to use. Defaults to a URL safe character base which is 63 character set can also be changed but not really necessary. read to_insane doc for more details

# File lib/bit_hash.rb, line 9
def initialize(config_map=nil, *options)
  @options = {
    :base     => :url_safe,
    :char_set => nil,
  }
  @options = load_options(options)
  @config = Hash.new
  @default = Hash.new
  #validate mapping
  load_config_map(config_map) if config_map.kind_of? Array
  @config
end

Public Instance Methods

[](key) click to toggle source

fetches values for given key

# File lib/bit_hash.rb, line 115
def [](key)
  @config[key]
end
[]=(key,value) click to toggle source

see set

# File lib/bit_hash.rb, line 90
def []=(key,value)
  set(key,value)
end
get_options(key) click to toggle source

returns option settings for given key

# File lib/bit_hash.rb, line 132
def get_options(key)
  index = @config_map.index{|x|x[:name]==key}
  (index) ? @config_map[index].merge({:index => index}) : nil
end
keys() click to toggle source

Just get a list of keys

# File lib/bit_hash.rb, line 74
def keys
  @config_map.map{ |c| c[:name]}
end
load_config_map(config_map) click to toggle source
# you can use :default_index option to reference something in the option arrays but it really isn't suggested, :default take precedence
:default_index =>  2
}

]

# File lib/bit_hash.rb, line 58
def load_config_map(config_map)
  raise ArgumentError, "Config Map must be an Array" unless config_map.kind_of? Array
  config_array = []
  new_config = {}
  config_map.each_index do |index|
    conf = config_map[index]
    raise ArgumentError, "#{conf[:name]} is a duplicate" if new_config.keys.index(conf[:name])
    new_config[conf[:name]] = get_check_config(index,conf)
    config_array[index] = conf 
  end
  @config = new_config.dup
  @default = new_config.dup
  @config_map = config_array
end
parse(config_string, *options) click to toggle source

takes a given config string and returns a mapped hash please read to_insane doc for info on base and char_set

# File lib/bit_hash.rb, line 150
def parse(config_string, *options)
  options = load_options(options)
  config_string = config_string.from_insane(options[:base],options[:char_set]).to_s(2)
  config_array = config_string.split('')
  new_config = @default.dup
  @config_map.each do |conf|
    value = config_array.pop(conf[:size])
    break if value.nil?
    value = value.join('').to_i(2)
    new_config[conf[:name]] = (conf[:options].kind_of? Array ) ? conf[:options][value] : value
  end
  new_config
end
replace(hash) click to toggle source

replaces internal settings with any given hash and overwrites values and returns hash. On failure returns nil

# File lib/bit_hash.rb, line 120
def replace(hash)
  cache = @config.dup
  hash.each do |key, value|
    if set(key,value).nil?
      @config = cache
      return nil
    end
  end
  @config
end
replace_options(key, options) click to toggle source

replaces given option with name of key and replaces with options

# File lib/bit_hash.rb, line 138
def replace_options(key, options)
  @config_map.map! do |conf|
    if conf[:name] == key
      options
    else
      conf
    end
  end
end
save(config_string, *options) click to toggle source

pareses and saves string into internal hash

# File lib/bit_hash.rb, line 165
def save(config_string, *options)
  @config = parse(config_string, *options)
end
set(key,value) click to toggle source

sets a key value, returns nil if value is invalid

# File lib/bit_hash.rb, line 95
def set(key,value)
  conf = get_options(key)
  if conf && check_value(conf,value)
    @config[key] = value
  else
    nil
  end
end
set!(key,value) click to toggle source

sets with critical failure

# File lib/bit_hash.rb, line 105
def set!(key,value)
  conf = get_options(key)
  if conf && check_value!(conf,value)
    @config[key] = value
  else
    raise ArgumentError, "Key: '#{key}' not found in config"
  end
end
set_default(key) click to toggle source

Set default key

# File lib/bit_hash.rb, line 79
def set_default(key)
  conf = get_options(key)
  @config[conf[:name]] = 0
  if !conf[:default].nil?
    new_config[conf[:name]] = conf[:default]
  elsif !conf[:default_index].nil?
    new_config[conf[:name]] = conf[:options][conf.default_index]
  end
end
to_bin() click to toggle source

converts it into a binary string

# File lib/bit_hash.rb, line 170
def to_bin
  bin_config = []
  @config_map.each do |conf|
    val = @config[conf[:name]]
    val = conf[:options].index(val) if conf[:options].kind_of? Array
    bin = "%0#{conf[:size]}d" % val.to_s(2).to_i
    bin_config.unshift(bin)
  end
  bin_config.join('')
end
to_hash() click to toggle source

returns as a hash (alias: inspect)

# File lib/bit_hash.rb, line 203
def to_hash
  @config
end
to_i() click to toggle source

converts it to an integer, Good for IDs

# File lib/bit_hash.rb, line 182
def to_i
  to_bin.to_i(2)
end
to_s(*options) click to toggle source

turns hash into a small compact string. see to_insane rdoc for base, and char_set definitions

# File lib/bit_hash.rb, line 188
def to_s(*options)
  options = load_options(options)
  str = ''
  str << to_i.to_insane(options[:base], options[:char_set])
  str
end
valid_value?(key,val) click to toggle source

checks key to see if value given is valid

# File lib/bit_hash.rb, line 196
def valid_value?(key,val)
  check_value(get_options(key),val)
end

Private Instance Methods

check_value(conf,val) click to toggle source

checks value against given config

# File lib/bit_hash.rb, line 210
def check_value(conf,val)
  if conf[:options].kind_of?(Array)
    return false if conf[:options].index(val).nil?
  elsif conf[:options].kind_of?(Integer) && val.kind_of?(Integer)
    return false if val < 0 or val > conf[:options]
  else
    return false
  end
  true
end
check_value!(conf,val) click to toggle source

checks values with a bang

# File lib/bit_hash.rb, line 222
def check_value!(conf,val)
  if conf[:options].kind_of? Array
    raise ArgumentError, "#{conf[:name]} is #{val} and is not a valid value within the stored array. #{conf[:options].to_s}" if conf[:options].index(val).nil?
  elsif conf[:options].kind_of? Integer
    raise ArgumentError, "#{conf[:name]} is #{val} and should not be less than 0" if val < 0
    raise ArgumentError, "#{conf[:name]} is #{val} and value is not less than #{conf[:options]}" if val > conf[:options]
  else
    raise ArgumentError, "#{conf[:name]} is not a valid value please associate it with an object in an array or give it an integer value" 
  end
  true
end
get_check_config(key,conf) click to toggle source
# File lib/bit_hash.rb, line 245
def get_check_config(key,conf)
  raise ArgumentError, "config is not a Hash" unless conf.kind_of? Hash
  raise ArgumentError, ":name cannot be nil for [#{key}]" if conf[:name].nil?
  raise ArgumentError, ":options cannot be nil for [#{key}]" if conf[:options].nil?
  if conf[:options].kind_of? Integer
    conf[:size] = conf[:options]
  elsif conf[:options].kind_of? Array
    conf[:size] = conf[:options].size
  elsif conf[:options].kind_of? Symbol || conf[:options].kind_of?(String)
    raise ArgumentError, ":options Character set cannot have duplicate characters" if conf[:options].kind_of?(String) && (conf[:options].size != conf[:options].split(//).uniq.size)
    raise ArgumentError, ":options"
    raise ArgumentError, ":size needs to be an integer larger than 0 [#{conf[:name]}]" unless conf[:size].kind_of? Integer and conf[:size] > 0
  else
    raise ArgumentError, ":options needs to be an Array or Integer for [#{conf[:name]}]" 
  end
  if !conf[:default].nil?
    raise ArgumentError, "default value of (#{conf[:default]}) is not valid for #{conf[:name]}" unless check_value(conf,conf[:default])
    default = conf[:default]
  elsif !conf[:default_index].nil?
    raise ArgumentError, ":default_index must be an integer #{conf[:name]}" unless conf[:default_index].kind_of? Integer
    unless default = conf[:options][conf[:default_index]]
      raise ArgumentError, ":default_index must be a valid option array index #{conf[:name]}"
    end
  end
  conf[:size] = (conf[:size]-1).to_s(2).size
  default ||= (conf[:options].kind_of? Integer) ? 0 : conf[:options][0]
end
load_options(options) click to toggle source
# File lib/bit_hash.rb, line 234
def load_options(options)
  new_options = @options.dup
  if options[0].kind_of? Hash
    new_options.merge(options[0])
  else
    new_options[:base] = options[0] unless options[0].nil?
    new_options[:char_set] = options[1] unless options[1].nil?
  end
  new_options
end