Class: ParamsReady::Parameter::PolymorphParameterDefinition

Inherits:
Definition show all
Includes:
ValueParameterDefinition::ValueLike
Defined in:
lib/params_ready/parameter/polymorph_parameter.rb

Instance Attribute Summary collapse

Attributes inherited from Definition

#default

Attributes inherited from AbstractDefinition

#altn, #name

Instance Method Summary collapse

Methods included from ValueParameterDefinition::ValueLike

#duplicate_value, #freeze_value

Methods inherited from Definition

#canonical_default, #default_defined?, #fetch_callable_default, #fetch_default, #memoize?, #name_for_formatter, #no_input?, #no_output?, #postprocess, #preprocess, #restricted_for_format?, #set_local, #set_no_input, #set_postprocessor, #set_preprocessor

Methods inherited from AbstractDefinition

#create, #from_hash, #from_input, #normalize_alternative_name, #parameter_class

Methods included from Extensions::Freezer

#freeze_variable, #freeze_variables, #variables_to_freeze

Methods included from Extensions::Finalizer

#obligatory, #obligatory!

Methods included from Extensions::ClassReaderWriter

#class_reader_writer

Methods included from Extensions::LateInit

#late_init

Methods included from Extensions::Collection

#collection

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

#initialize(*args, identifier: nil, types: [], default_name: nil, **options) ⇒ PolymorphParameterDefinition

Returns a new instance of PolymorphParameterDefinition.



105
106
107
108
109
110
111
112
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 105

def initialize(*args, identifier: nil, types: [], default_name: nil, **options)
  @types = {}
  @alt_names = {}
  add_types types
  set_default(default_name)
  super *args, **options
  set_identifier identifier unless identifier.nil?
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



99
100
101
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 99

def types
  @types
end

Instance Method Details

#add_type(definition) ⇒ Object

Raises:



114
115
116
117
118
119
120
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 114

def add_type(definition)
  check_type_names(definition)
  raise ParamsReadyError, "Reused name: #{definition.name}" if @types.key? definition.name
  raise ParamsReadyError, "Reused alternative: #{definition.altn}" if @alt_names.key? definition.altn
  @types[definition.name] = definition
  @alt_names[definition.altn] = definition
end

#add_types(types) ⇒ Object



132
133
134
135
136
137
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 132

def add_types(types)
  return if types.nil?
  types.each do |definition|
    add_type definition
  end
end

#check_type_names(definition) ⇒ Object

Raises:



122
123
124
125
126
127
128
129
130
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 122

def check_type_names(definition)
  return if @marshallers.nil?
  return unless @marshallers.default?
  default = @marshallers.default
  return unless default.respond_to? :reserved?

  raise ParamsReadyError, "Reserved name: #{definition.name}" if default.reserved?(definition.name)
  raise ParamsReadyError, "Reserved alternative: #{definition.altn}" if default.reserved?(definition.altn)
end

#finishObject



164
165
166
167
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 164

def finish
  set_identifier :ppt unless marshallers.default?
  super
end

#set_default(default_name) ⇒ Object

Raises:



154
155
156
157
158
159
160
161
162
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 154

def set_default(default_name)
  return if default_name.nil?

  type_def = types[default_name]
  raise ParamsReadyError, "Unknown type '#{default_name}'" if type_def.nil?
  raise ParamsReadyError, "Default type must have default" unless type_def.default_defined?
  frozen = freeze_value(type_def.create)
  @default = frozen
end

#set_identifier(type_identifier) ⇒ Object

Raises:



139
140
141
142
143
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 139

def set_identifier(type_identifier)
  raise ParamsReadyError, "Identifier already taken: #{type_identifier}" if @alt_names&.key?(type_identifier)
  raise ParamsReadyError, "Identifier already taken: #{type_identifier}" if @types&.key?(type_identifier)
  set_marshaller using: :hash, type_identifier: type_identifier
end

#type(name, context) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/params_ready/parameter/polymorph_parameter.rb', line 145

def type(name, context)
  name = if context.alternative?
    @alt_names[name.to_sym].name
  else
    name
  end
  @types[name.to_sym]
end