Class: ParamsReady::Parameter::AbstractStructParameterDefinition

Inherits:
Definition show all
Defined in:
lib/params_ready/parameter/abstract_struct_parameter.rb

Instance Attribute Summary collapse

Attributes inherited from Definition

#default

Attributes inherited from AbstractDefinition

#altn, #name

Instance Method Summary collapse

Methods inherited from Definition

#canonical_default, #default_defined?, #fetch_callable_default, #fetch_default, #finish, #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, #finish, #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::Finalizer::InstanceMethods

#finish

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

#initialize(*args, **options) ⇒ AbstractStructParameterDefinition

Returns a new instance of AbstractStructParameterDefinition.



162
163
164
165
166
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 162

def initialize(*args, **options)
  super *args, **options
  @key_map = nil
  @names = {}
end

Instance Attribute Details

#key_mapObject (readonly)

Returns the value of attribute key_map.



147
148
149
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 147

def key_map
  @key_map
end

#namesObject (readonly)

Returns the value of attribute names.



160
161
162
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 160

def names
  @names
end

Instance Method Details

#add_child(child) ⇒ Object

Raises:



194
195
196
197
198
199
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 194

def add_child(child)
  raise ParamsReadyError, "Not a parameter definition: '#{child.class.name}'" unless child.is_a?(AbstractDefinition)
  raise ParamsReadyError, "Name already there: '#{child.name}'" if @names.key?(child.name)
  raise ParamsReadyError, "Child can't be added after default has been set" if default_defined?
  @names[child.name] = child
end

#child_definition(name) ⇒ Object

Raises:



168
169
170
171
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 168

def child_definition(name)
  raise ParamsReadyError, "No such name: #{name}" unless names.key? name
  names[name]
end

#duplicate_value(value) ⇒ Object



149
150
151
152
153
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 149

def duplicate_value(value)
  value.values.map do |param|
    [param.name, param.dup]
  end.to_h
end

#freeze_value(value) ⇒ Object



155
156
157
158
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 155

def freeze_value(value)
  value.values.each(&:freeze)
  value.freeze
end

#has_child?(name) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 173

def has_child?(name)
  names.key? name
end

#infer_defaultObject



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 182

def infer_default
  names.reduce({}) do |result, pair|
    child_def = pair[1]
    unless child_def.default_defined?
      raise ParamsReadyError, "Can't infer default, child '#{definition.name}' is not optional and has no default" unless child_def.optional?
    else
      result[child_def.name] = child_def.default
    end
    result
  end
end

#set_default(value) ⇒ Object



177
178
179
180
# File 'lib/params_ready/parameter/abstract_struct_parameter.rb', line 177

def set_default(value)
  value = infer_default if value == :inferred
  super(value)
end