6
7
8
9
10
11
12
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
|
# File 'lib/onix/list_writer.rb', line 6
def list_writer(name, options = {})
options.symbolize_keys!
unless options.is_a?(Hash) && options[:list].is_a?(Integer)
raise ArgumentError, "Must specify code list number as ':list => Integer'"
end
define_method("#{name}=") do |value|
values = case value.class.to_s
when 'Array'
value
when 'String'
value.split(/\s+/)
else
[value]
end
values.each do |value|
unless value.nil? || ONIX::Lists.list(options[:list]).keys.include?(value)
raise ArgumentError, "Invalid #{("_" + name.to_s).downcase.gsub!(/_(.)/) { $1.upcase }} '#{value}' -- Refer to ONIX Code List #{options[:list]}"
end
end
values = if options[:as] == Array || options[:as] == []
values
elsif options[:as] == String
values.join(" ")
else
values.first
end
self.instance_variable_set("@#{name}", values)
end
end
|