class Cacofonix::Code
Attributes
Public Class Methods
Note re: key type. For backwards compatibility, code keys that are all-digits are passed around in this gem as Integers.
The actual code list hashes have string-based keys for consistency. If you want the integer-or-string key, use Code#key
. If you want the real string key, use Code#to_s
.
If the key is not found in the list, the behaviour depends on the :enforce option. By default, it returns a code with key and value set to nil. If :enforce is true, an exception is raised. If :enforce is false, the key and value is the data given in the tag.
# File lib/cacofonix/core/code.rb, line 22 def initialize(list_number, data, options = {}) @list_number = list_number unless @list = Cacofonix::Lists.list(@list_number) raise Cacofonix::CodeListNotFound.new(@list_number) end @key = @value = nil return if data.nil? || data == "" if data.kind_of?(Integer) @key = data pad_length = options[:length] || ["#{@list.size}".size, 2].max @real_key = pad(data, pad_length) elsif data.match(/^\d+$/) && @list.keys.include?(data) @key = data.to_i @real_key = data elsif @list.keys.include?(data) @key = @real_key = data else @list.each_pair { |k, v| next unless v == data @real_key = k @key = @real_key.match(/^\d+$/) ? @real_key.to_i : @real_key break } end if @real_key @value = @list[@real_key] elsif options[:enforce] == true raise Cacofonix::CodeNotFoundInList.new(@list_number, data) elsif options[:enforce] == false @value = @key = @real_key = data else @value = @key = @real_key = nil end end
Public Instance Methods
Returns the string representation of the key. eg, “BB”.
# File lib/cacofonix/core/code.rb, line 69 def to_s @real_key end
Returns the string representation of the value. eg, “Hardback”.
# File lib/cacofonix/core/code.rb, line 76 def to_str @value.to_s end
Returns true if the given key has a value in the codelist.
# File lib/cacofonix/core/code.rb, line 62 def valid? @value ? true : false end
Private Instance Methods
Converts a Integer key into a String key.
# File lib/cacofonix/core/code.rb, line 85 def pad(key, len) key ? key.to_s.rjust(len, '0') : nil end