Class: ParamsReady::Marshaller::InstanceCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/params_ready/marshaller/collection.rb

Direct Known Subclasses

ClassCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#defaultObject

Returns the value of attribute default.



7
8
9
# File 'lib/params_ready/marshaller/collection.rb', line 7

def default
  @default
end

#instancesObject (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

Raises:



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

Raises:



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

Raises:



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

Returns:

  • (Boolean)


75
76
77
# File 'lib/params_ready/marshaller/collection.rb', line 75

def default?
  !@default.nil?
end

#freezeObject



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

Returns:

  • (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