Class: Cacofonix::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/cacofonix/core/code.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list_number, data, options = {}) ⇒ Code

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.



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/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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/cacofonix/core/code.rb', line 8

def key
  @key
end

#listObject (readonly)

Returns the value of attribute list.



8
9
10
# File 'lib/cacofonix/core/code.rb', line 8

def list
  @list
end

#list_numberObject (readonly)

Returns the value of attribute list_number.



8
9
10
# File 'lib/cacofonix/core/code.rb', line 8

def list_number
  @list_number
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/cacofonix/core/code.rb', line 8

def value
  @value
end

Instance Method Details

#to_sObject

Returns the string representation of the key. eg, “BB”.



69
70
71
# File 'lib/cacofonix/core/code.rb', line 69

def to_s
  @real_key
end

#to_strObject

Returns the string representation of the value. eg, “Hardback”.



76
77
78
# File 'lib/cacofonix/core/code.rb', line 76

def to_str
  @value.to_s
end

#valid?Boolean

Returns true if the given key has a value in the codelist.

Returns:

  • (Boolean)


62
63
64
# File 'lib/cacofonix/core/code.rb', line 62

def valid?
  @value ? true : false
end