Class: Tabry::Models::ConfigObject
- Inherits:
-
Object
- Object
- Tabry::Models::ConfigObject
show all
- Defined in:
- lib/tabry/models/config_object.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#as_json ⇒ Object
-
#assert_of_class(key, val, klasses) ⇒ Object
Gets fields, asserts is either nil or of given type.
-
#init_field_boolean(key, val) ⇒ Object
-
#init_field_list_object(key, val, object_class) ⇒ Object
-
#init_field_object(key, val, object_class) ⇒ Object
-
#init_field_string(key, val) ⇒ Object
-
#init_field_string_array(key, vals) ⇒ Object
-
#initialize(raw:, root:) ⇒ ConfigObject
constructor
A new instance of ConfigObject.
-
#inspect ⇒ Object
-
#to_s ⇒ Object
Constructor Details
#initialize(raw:, root:) ⇒ ConfigObject
Returns a new instance of ConfigObject.
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/tabry/models/config_object.rb', line 27
def initialize(raw:, root:)
@_raw = raw
@_root = root or raise "missing root"
unknown_fields = @_raw.keys - self.class::FIELDS.keys.map(&:to_s)
unless unknown_fields.empty?
raise ConfigError, "Unknown field(s) #{unknown_fields.inspect} for #{self.class}"
end
raw.compact.each do |key, val|
type, * = Array(self.class::FIELDS[key.to_sym])
instance_variable_set :"@#{key}", send(:"init_field_#{type}", key, val, *)
end
end
|
Instance Attribute Details
#_raw ⇒ Object
Returns the value of attribute _raw.
25
26
27
|
# File 'lib/tabry/models/config_object.rb', line 25
def _raw
@_raw
end
|
#_root ⇒ Object
Returns the value of attribute _root.
25
26
27
|
# File 'lib/tabry/models/config_object.rb', line 25
def _root
@_root
end
|
Class Method Details
.as_json(val) ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/tabry/models/config_object.rb', line 84
def self.as_json(val)
if [ConfigObject, ConfigList, ConfigStringHash].any? { val.is_a?(_1) }
val.as_json
elsif val.is_a?(Array)
val.map { as_json(_1) }
else
val
end
end
|
Instance Method Details
#as_json ⇒ Object
77
78
79
80
81
82
|
# File 'lib/tabry/models/config_object.rb', line 77
def as_json
self.class::FIELDS.to_h do |k, _|
raw_val = send(k)
[k, ConfigObject.as_json(raw_val)]
end.compact
end
|
#assert_of_class(key, val, klasses) ⇒ Object
Gets fields, asserts is either nil or of given type
42
43
44
45
46
47
|
# File 'lib/tabry/models/config_object.rb', line 42
def assert_of_class(key, val, klasses)
unless Array(klasses).any? { |klass| val.is_a?(klass) }
raise ConfigError,
"Invalid type #{val.class} for #{self.class} field #{key.inspect}, expected #{klasses.inspect}"
end
end
|
#init_field_boolean(key, val) ⇒ Object
72
73
74
75
|
# File 'lib/tabry/models/config_object.rb', line 72
def init_field_boolean(key, val)
assert_of_class(key, val, [TrueClass, FalseClass])
val
end
|
#init_field_list_object(key, val, object_class) ⇒ Object
67
68
69
70
|
# File 'lib/tabry/models/config_object.rb', line 67
def init_field_list_object(key, val, object_class)
assert_of_class(key, val, Array)
Object.const_get("Tabry::Models::#{object_class}").new(raw: val, root: _root)
end
|
#init_field_object(key, val, object_class) ⇒ Object
62
63
64
65
|
# File 'lib/tabry/models/config_object.rb', line 62
def init_field_object(key, val, object_class)
assert_of_class(key, val, Hash)
Object.const_get("Tabry::Models::#{object_class}").new(raw: val, root: _root)
end
|
#init_field_string(key, val) ⇒ Object
49
50
51
52
|
# File 'lib/tabry/models/config_object.rb', line 49
def init_field_string(key, val)
assert_of_class(key, val, String)
val
end
|
#init_field_string_array(key, vals) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/tabry/models/config_object.rb', line 54
def init_field_string_array(key, vals)
assert_of_class(key, vals, Array)
vals.each_with_index do |val, i|
assert_of_class("#{key}[#{i}]", val, String)
end
vals
end
|
#inspect ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/tabry/models/config_object.rb', line 12
def inspect
field_strings = self.class::FIELDS.keys.map do |f|
val = instance_variable_get(:"@#{f}")
if val.nil?
nil
else
"#{f}=#{val.inspect}"
end
end
desc = [self.class.name, *field_strings.compact].join(" ")
"#<#{desc}>"
end
|
#to_s ⇒ Object
8
9
10
|
# File 'lib/tabry/models/config_object.rb', line 8
def to_s
inspect
end
|