Class: Abbrv::Abbreviation

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

Instance Method Summary collapse

Constructor Details

#initialize(seed_hash) ⇒ Abbreviation

Returns a new instance of Abbreviation.



5
6
7
8
9
10
11
12
13
# File 'lib/abbrv.rb', line 5

def initialize(seed_hash)
  @seed_hash = seed_hash
  validate_for_value_uniqueness
  all_subkeys.each do |k|
    self.class.send(:define_method, k) do |main_key|
      @seed_hash[main_key][k]
    end
  end
end

Instance Method Details

#all_subkeysObject



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

def all_subkeys
  key_arr = []
  @seed_hash.each_value do |sub_hash|
    sub_hash.each_key do |k|
      key_arr << k
    end
  end
  key_arr.uniq
end

#all_valsObject



33
34
35
36
37
38
39
40
41
# File 'lib/abbrv.rb', line 33

def all_vals
  val_arr = []
  @seed_hash.each_value do |sub_hash|
    sub_hash.each_value do |v|
      val_arr << v
    end
  end
  val_arr
end

#find(val, do_search_keys = false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/abbrv.rb', line 15

def find(val, do_search_keys = false)
  ret_hash = {}
  @seed_hash.each do |k, sub_hash|
    sub_hash.each do |k2, v|
      regex = /\A#{val.strip}\Z/i
      if v =~ regex
        ret_hash[k] = sub_hash
        return ret_hash
      end
      if k.to_s =~ regex && do_search_keys
        ret_hash[k] = sub_hash
        return ret_hash
      end
    end
  end
  return nil
end

#random_main_keyObject



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

def random_main_key
  #no spec. eyeball tested
  keys = @seed_hash.keys
  keys[rand(0..keys.count - 1)]
end

#random_val(subkey) ⇒ Object



61
62
63
64
65
# File 'lib/abbrv.rb', line 61

def random_val(subkey)
  #no spec. eyeball tested
  vals = sub_hash(subkey).values
  vals[rand(0..vals.count - 1)]
end

#sub_hash(subkey) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/abbrv.rb', line 53

def sub_hash(subkey)
  ret_hash = {}
  @seed_hash.each do |k,v|
    ret_hash[k] = @seed_hash[k][subkey] if @seed_hash[k].has_key?(subkey)
  end
  ret_hash
end