Class: BitHash
- Inherits:
-
Object
- Object
- BitHash
- Defined in:
- lib/bit_hash.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#[](key) ⇒ Object
fetches values for given key.
-
#[]=(key, value) ⇒ Object
see set.
-
#get_options(key) ⇒ Object
returns option settings for given key.
-
#initialize(config_map = nil, *options) ⇒ BitHash
constructor
look below at load_config_map for config_map schema base is the base value you want to use.
-
#keys ⇒ Object
Just get a list of keys.
-
#load_config_map(config_map) ⇒ Object
# 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 } ].
-
#parse(config_string, *options) ⇒ Object
takes a given config string and returns a mapped hash please read to_insane doc for info on base and char_set.
-
#replace(hash) ⇒ Object
replaces internal settings with any given hash and overwrites values and returns hash.
-
#replace_options(key, options) ⇒ Object
replaces given option with name of key and replaces with options.
-
#save(config_string, *options) ⇒ Object
pareses and saves string into internal hash.
-
#set(key, value) ⇒ Object
sets a key value, returns nil if value is invalid.
-
#set!(key, value) ⇒ Object
sets with critical failure.
-
#set_default(key) ⇒ Object
Set default key.
-
#to_bin ⇒ Object
converts it into a binary string.
-
#to_i ⇒ Object
converts it to an integer, Good for IDs.
-
#to_s(*options) ⇒ Object
turns hash into a small compact string.
-
#valid_value?(key, val) ⇒ Boolean
checks key to see if value given is valid.
Constructor Details
#initialize(config_map = nil, *options) ⇒ BitHash
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
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/bit_hash.rb', line 9 def initialize(config_map=nil, *) @options = { :base => :url_safe, :char_set => nil, } @options = () @config = Hash.new @default = Hash.new #validate mapping load_config_map(config_map) if config_map.kind_of? Array @config end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
5 6 7 |
# File 'lib/bit_hash.rb', line 5 def default @default end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/bit_hash.rb', line 5 def @options end |
Instance Method Details
#[](key) ⇒ Object
fetches values for given key
115 116 117 |
# File 'lib/bit_hash.rb', line 115 def [](key) @config[key] end |
#[]=(key, value) ⇒ Object
see set
90 91 92 |
# File 'lib/bit_hash.rb', line 90 def []=(key,value) set(key,value) end |
#get_options(key) ⇒ Object
returns option settings for given key
132 133 134 135 |
# File 'lib/bit_hash.rb', line 132 def (key) index = @config_map.index{|x|x[:name]==key} (index) ? @config_map[index].merge({:index => index}) : nil end |
#keys ⇒ Object
Just get a list of keys
74 75 76 |
# File 'lib/bit_hash.rb', line 74 def keys @config_map.map{ |c| c[:name]} end |
#load_config_map(config_map) ⇒ Object
# 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
}
]
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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) ⇒ Object
takes a given config string and returns a mapped hash please read to_insane doc for info on base and char_set
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/bit_hash.rb', line 150 def parse(config_string, *) = () config_string = config_string.from_insane([:base],[: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) ⇒ Object
replaces internal settings with any given hash and overwrites values and returns hash. On failure returns nil
120 121 122 123 124 125 126 127 128 129 |
# 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) ⇒ Object
replaces given option with name of key and replaces with options
138 139 140 141 142 143 144 145 146 |
# File 'lib/bit_hash.rb', line 138 def (key, ) @config_map.map! do |conf| if conf[:name] == key else conf end end end |
#save(config_string, *options) ⇒ Object
pareses and saves string into internal hash
165 166 167 |
# File 'lib/bit_hash.rb', line 165 def save(config_string, *) @config = parse(config_string, *) end |
#set(key, value) ⇒ Object
sets a key value, returns nil if value is invalid
95 96 97 98 99 100 101 102 |
# File 'lib/bit_hash.rb', line 95 def set(key,value) conf = (key) if conf && check_value(conf,value) @config[key] = value else nil end end |
#set!(key, value) ⇒ Object
sets with critical failure
105 106 107 108 109 110 111 112 |
# File 'lib/bit_hash.rb', line 105 def set!(key,value) conf = (key) if conf && check_value!(conf,value) @config[key] = value else raise ArgumentError, "Key: '#{key}' not found in config" end end |
#set_default(key) ⇒ Object
Set default key
79 80 81 82 83 84 85 86 87 |
# File 'lib/bit_hash.rb', line 79 def set_default(key) conf = (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 ⇒ Object
converts it into a binary string
170 171 172 173 174 175 176 177 178 179 |
# 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_i ⇒ Object
converts it to an integer, Good for IDs
182 183 184 |
# File 'lib/bit_hash.rb', line 182 def to_i to_bin.to_i(2) end |
#to_s(*options) ⇒ Object
turns hash into a small compact string. see to_insane rdoc for base, and char_set definitions
188 189 190 191 192 193 |
# File 'lib/bit_hash.rb', line 188 def to_s(*) = () str = '' str << to_i.to_insane([:base], [:char_set]) str end |
#valid_value?(key, val) ⇒ Boolean
checks key to see if value given is valid
196 197 198 |
# File 'lib/bit_hash.rb', line 196 def valid_value?(key,val) check_value((key),val) end |