Class: Rubius::Dictionary

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

Constant Summary collapse

VENDOR =
'VENDOR'
ATTRIBUTE =
'ATTRIBUTE'
VALUE =
'VALUE'
DEFAULT_VENDOR =
0

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Returns a new instance of Dictionary.



8
9
10
11
# File 'lib/rubius/dictionary.rb', line 8

def initialize
  @dictionary = Hash.new
  @dictionary[DEFAULT_VENDOR] = {:name => ''}
end

Instance Method Details

#attribute_id(attr_name, vendor_id = DEFAULT_VENDOR) ⇒ Object



75
76
77
78
79
# File 'lib/rubius/dictionary.rb', line 75

def attribute_id(attr_name, vendor_id = DEFAULT_VENDOR)
  vendor_object = @dictionary[vendor_id].reject{|k,v| !v.is_a?(Hash) || v[:name]!=attr_name}
  vendor_object = vendor_object.to_a if RUBY_VERSION < "1.9.2"
  vendor_object.flatten.first
end

#attribute_name(attr_id, vendor_id = DEFAULT_VENDOR) ⇒ Object



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

def attribute_name(attr_id, vendor_id = DEFAULT_VENDOR)
  attribute(attr_id, vendor_id)[:name] rescue nil
end

#attribute_type(attr_id, vendor_id = DEFAULT_VENDOR) ⇒ Object



71
72
73
# File 'lib/rubius/dictionary.rb', line 71

def attribute_type(attr_id, vendor_id = DEFAULT_VENDOR)
  attribute(attr_id, vendor_id)[:type] rescue nil
end

#load(dictionary_file) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubius/dictionary.rb', line 13

def load(dictionary_file)
  dict_lines = IO.readlines(dictionary_file)
  
  vendor_id = DEFAULT_VENDOR
  skip_until_next_vendor = false
  
  dict_lines.each do |line|
    next if line =~ /^\#/
    next if (tokens = line.split(/\s+/)).empty?
    
    entry_type = tokens[0].upcase
    case entry_type
    when VENDOR
      skip_until_next_vendor = false
      
      # If the vendor_id string is nil or empty, we should skip this entire block
      # until we find another VENDOR definition, also ignore all VALUEs and ATTRIBUTEs
      # until the next VENDOR because otherwise, they will be included in the wrong VENDOR
      vendor_id_str = tokens[2]
      if vendor_id_str.nil? || vendor_id_str.empty?
        skip_until_next_vendor = true
        next
      end
      
      # VENDOR id should be higher than 0, skip everything if it isn't
      vendor_id = vendor_id_str.to_i
      if vendor_id <= 0
        skip_until_next_vendor = true
        next
      end
      
      vendor_name = tokens[1].strip
      @dictionary[vendor_id] ||= {:name => vendor_name}
    when ATTRIBUTE
      next if skip_until_next_vendor
      next if tokens[1].nil? || tokens[2].to_i <= 0 || tokens[3].nil?
      @dictionary[vendor_id][tokens[2].to_i] = {:name => tokens[1].strip, :type => tokens[3].strip}
    when VALUE
      next if skip_until_next_vendor
      @dictionary[vendor_id][tokens[1]] = {tokens[2].strip => tokens[3].to_i}
    end
  end
rescue Errno::ENOENT
  raise Rubius::InvalidDictionaryError
end

#vendor_name(vendor_id = DEFAULT_VENDOR) ⇒ Object



63
64
65
# File 'lib/rubius/dictionary.rb', line 63

def vendor_name(vendor_id = DEFAULT_VENDOR)
  @dictionary[vendor_id][:name]
end

#vendorsObject



59
60
61
# File 'lib/rubius/dictionary.rb', line 59

def vendors
  @dictionary.collect{|k,v| v[:name]}.reject{|n| n.empty?}
end