Class: ParamsReady::Marshaller::InstanceCollection
- Inherits:
-
Object
- Object
- ParamsReady::Marshaller::InstanceCollection
- Defined in:
- lib/params_ready/marshaller/collection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#instances ⇒ Object
readonly
Returns the value of attribute instances.
Instance Method Summary collapse
- #add_instance(value_class, instance) ⇒ Object
- #canonicalize(definition, input, context, validator, **opts) ⇒ Object
- #default!(value_class) ⇒ Object
- #default? ⇒ Boolean
- #freeze ⇒ Object
- #infer_class(value) ⇒ Object
-
#initialize(canonical, default = nil, instances = {}) ⇒ InstanceCollection
constructor
A new instance of InstanceCollection.
- #instance(value_class) ⇒ Object
- #instance?(value_class) ⇒ Boolean
- #marshal(parameter, format, **opts) ⇒ Object
- #marshal_canonical(parameter, format, **opts) ⇒ Object
- #populate_clone(clone, other) ⇒ Object
- #reverse_merge(other) ⇒ Object
Constructor Details
#initialize(canonical, default = nil, instances = {}) ⇒ InstanceCollection
Returns a new instance of InstanceCollection.
9 10 11 12 13 |
# File 'lib/params_ready/marshaller/collection.rb', line 9 def initialize(canonical, default = nil, instances = {}) @canonical = canonical @default = default @instances = instances end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
7 8 9 |
# File 'lib/params_ready/marshaller/collection.rb', line 7 def default @default end |
#instances ⇒ Object (readonly)
Returns the value of attribute instances.
7 8 9 |
# File 'lib/params_ready/marshaller/collection.rb', line 7 def instances @instances end |
Instance Method Details
#add_instance(value_class, instance) ⇒ Object
48 49 50 51 52 |
# File 'lib/params_ready/marshaller/collection.rb', line 48 def add_instance(value_class, instance) raise ParamsReadyError, "Marshaller must be frozen" unless instance.frozen? @instances[value_class] = instance end |
#canonicalize(definition, input, context, validator, **opts) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/params_ready/marshaller/collection.rb', line 15 def canonicalize(definition, input, context, validator, **opts) value_class = infer_class(input) marshaller = instance(value_class) raise ParamsReadyError, "Unexpected type for #{definition.name}: #{value_class.name}" if marshaller.nil? marshaller.canonicalize(definition, input, context, validator, **opts) end |
#default!(value_class) ⇒ Object
69 70 71 72 73 |
# File 'lib/params_ready/marshaller/collection.rb', line 69 def default!(value_class) instance = instance(value_class) raise ParamsReadyError, "No marshaller for class '#{value_class.name}'" if instance.nil? self.default = instance end |
#default? ⇒ Boolean
75 76 77 |
# File 'lib/params_ready/marshaller/collection.rb', line 75 def default? !@default.nil? end |
#freeze ⇒ Object
98 99 100 101 |
# File 'lib/params_ready/marshaller/collection.rb', line 98 def freeze @instance.freeze super end |
#infer_class(value) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/params_ready/marshaller/collection.rb', line 38 def infer_class(value) if instances.key? value.class value.class elsif value.is_a?(Hash) || Extensions::Hash.acts_as_hash?(value) Hash else value.class end end |
#instance(value_class) ⇒ Object
54 55 56 |
# File 'lib/params_ready/marshaller/collection.rb', line 54 def instance(value_class) @instances[value_class] end |
#instance?(value_class) ⇒ Boolean
58 59 60 |
# File 'lib/params_ready/marshaller/collection.rb', line 58 def instance?(value_class) @instances.key?(value_class) end |
#marshal(parameter, format, **opts) ⇒ Object
34 35 36 |
# File 'lib/params_ready/marshaller/collection.rb', line 34 def marshal(parameter, format, **opts) default.marshal(parameter, format, **opts) end |
#marshal_canonical(parameter, format, **opts) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/params_ready/marshaller/collection.rb', line 23 def marshal_canonical(parameter, format, **opts) marshaller = instance @canonical if marshaller.nil? value = parameter.send(:bare_value) raise ParamsReadyError, "Value is not canonical" unless value.is_a? @canonical value else marshaller.marshal(parameter, format, **opts) end end |
#populate_clone(clone, other) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/params_ready/marshaller/collection.rb', line 84 def populate_clone(clone, other) if other.default? && !clone.default? clone.default = other.default end other.instances.each do |value_class, i| next if clone.instance?(value_class) clone.add_instance value_class, i end clone end |
#reverse_merge(other) ⇒ Object
79 80 81 82 |
# File 'lib/params_ready/marshaller/collection.rb', line 79 def reverse_merge(other) clone = self.class.new(@canonical, @default, @instances.dup) populate_clone(clone, other) end |