5
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/virginity/vcard/field_values/structured_text.rb', line 5
def self.define(components)
m = Module.new
m.const_set("COMPONENTS", components)
m.module_eval <<-RUBY
def components
COMPONENTS
end
def values
EncodingDecoding::decode_structured_text(@value, COMPONENTS.size)
end
def empty?
values.all? {|v| v.empty? }
end
def reencode!(options={})
v = values
unless options[:variable_number_of_fields]
v.pop while v.size > components.size
v.push(nil) while v.size < components.size
else
v.pop while v.last.empty?
end
@value = EncodingDecoding::encode_structured_text(v)
end
def [](component)
raise "which component? \#\{component\}?? I only know \#\{components.inspect\}" unless components.include? component.to_sym
send("\#\{component\}".to_sym)
end
def []=(component, new_value)
raise "which component? \#\{component\}?? I only know \#\{components.inspect\}" unless components.include? component.to_sym
send("\#\{component\}=", new_value)
end
RUBY
components.each_with_index do |component, idx|
m.module_eval <<-RUBY
def #{component}
values[#{idx}]
end
def #{component}=(new_value)
structure = values
structure[#{idx}] = new_value.to_s
@value = EncodingDecoding::encode_structured_text(structure)
end
def subset_of?(other_field)
components.all? do |component|
send(component).empty? or send(component) == other_field.send(component)
end
end
def superset_of?(other_field)
other_field.subset_of?(self)
end
RUBY
end
m
end
|