Class: Kafo::DataTypes::Struct
Instance Method Summary
collapse
#condition_value, #dump_default, new_from_string, parse_hash, register_type, split_arguments, types, unregister_type
Constructor Details
#initialize(spec) ⇒ Struct
Returns a new instance of Struct.
Instance Method Details
#multivalued? ⇒ Boolean
16
17
18
|
# File 'lib/kafo/data_types/struct.rb', line 16
def multivalued?
true
end
|
#to_s ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/kafo/data_types/struct.rb', line 20
def to_s
"struct containing " + @spec.keys.map do |k|
if k.is_a?(Optional)
[k.inner_value, %{"#{k.inner_value}" (optional #{@spec[k]})}]
elsif k.is_a?(NotUndef)
[k.inner_value, %{"#{k.inner_value}" (required #{@spec[k]})}]
else
[k, %{"#{k}" (#{@spec[k]})}]
end
end.sort_by(&:first).map(&:last).join(', ')
end
|
#typecast(value) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/kafo/data_types/struct.rb', line 32
def typecast(value)
if value.nil?
nil
elsif value.is_a?(::Hash)
value
elsif value == ['EMPTY_HASH']
{}
else
::Hash[[value].flatten.map do |kv|
k, v = kv.split(':', 2)
if (value_type = spec_value(k))
[k, value_type.typecast(v)]
else
[k, v]
end
end]
end
end
|
#valid?(input, errors = []) ⇒ Boolean
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/kafo/data_types/struct.rb', line 51
def valid?(input, errors = [])
unless input.is_a?(::Hash)
errors << "#{input.inspect} is not a valid struct"
return false
end
required_keys = @spec.keys.select { |k| k.is_a?(NotUndef) }.map { |k| k.inner_value }
missing_keys = required_keys - input.keys
errors << "Struct elements are missing: #{missing_keys.join(', ')}" unless missing_keys.empty?
known_keys = @spec.keys.map { |k| spec_key_name(k) }
= input.keys - known_keys
errors << "Struct elements are not permitted: #{.join(', ')}" unless .empty?
value_errors = []
optional_keys = @spec.keys.select { |k| k.is_a?(Optional) }.map { |k| k.inner_value }
(optional_keys & input.keys).each { |k| spec_value(k).valid?(input[k], value_errors) }
regular_keys = @spec.keys.select { |k| !k.is_a?(Optional) }.map { |k| spec_key_name(k) }
regular_keys.each { |k| spec_value(k).valid?(input[k], value_errors) }
errors << "Struct values are invalid: #{value_errors.join(', ')}" unless value_errors.empty?
return errors.empty?
end
|