Class: ParamsReady::Marshaller::ClassCollection

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

Instance Attribute Summary collapse

Attributes inherited from InstanceCollection

#default, #instances

Instance Method Summary collapse

Methods inherited from InstanceCollection

#canonicalize, #default!, #default?, #infer_class, #instance, #instance?, #marshal, #marshal_canonical

Constructor Details

#initialize(canonical, default = nil, instances = {}, factories = {}) ⇒ ClassCollection

Returns a new instance of ClassCollection.



107
108
109
110
# File 'lib/params_ready/marshaller/collection.rb', line 107

def initialize(canonical, default = nil, instances = {}, factories = {})
  @factories = factories
  super canonical, default, instances
end

Instance Attribute Details

#factoriesObject (readonly)

Returns the value of attribute factories.



105
106
107
# File 'lib/params_ready/marshaller/collection.rb', line 105

def factories
  @factories
end

Instance Method Details

#add_factory(name, factory) ⇒ Object

Raises:



116
117
118
119
120
121
122
# File 'lib/params_ready/marshaller/collection.rb', line 116

def add_factory(name, factory)
  name = name.to_sym
  raise ParamsReadyError, "Name '#{name}' already taken" if factory?(name)
  raise ParamsReadyError, "Factory must be frozen" unless factory.frozen?

  @factories[name] = factory
end

#add_instance(value_class, instance) ⇒ Object

Raises:



124
125
126
127
128
# File 'lib/params_ready/marshaller/collection.rb', line 124

def add_instance(value_class, instance)
  raise ParamsReadyError, "Marshaller for '#{value_class.name}' already exists" if instance?(value_class)

  super
end

#build_instance(name, **opts) ⇒ Object



130
131
132
# File 'lib/params_ready/marshaller/collection.rb', line 130

def build_instance(name, **opts)
  factory(name).instance(**opts)
end

#factory(name) ⇒ Object



134
135
136
# File 'lib/params_ready/marshaller/collection.rb', line 134

def factory(name)
  @factories[name]
end

#factory?(name) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/params_ready/marshaller/collection.rb', line 138

def factory?(name)
  @factories.key? name
end

#freezeObject



142
143
144
145
# File 'lib/params_ready/marshaller/collection.rb', line 142

def freeze
  @factories.freeze
  super
end

#instance_collectionObject



112
113
114
# File 'lib/params_ready/marshaller/collection.rb', line 112

def instance_collection
  InstanceCollection.new(@canonical, nil, @instances.dup)
end

#populate_clone(clone, other) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/params_ready/marshaller/collection.rb', line 152

def populate_clone(clone, other)
  merged = super

  other.factories.each do |value_class, f|
    next if merged.factory?(value_class)

    clone.add_factory value_class, f
  end

  merged
end

#reverse_merge(other) ⇒ Object



147
148
149
150
# File 'lib/params_ready/marshaller/collection.rb', line 147

def reverse_merge(other)
  clone = self.class.new(@canonical, @default, @instances.dup, @factories.dup)
  populate_clone(clone, other)
end