Class: ParamsReady::Parameter::EnumSetParameterDefinition

Inherits:
AbstractStructParameterDefinition show all
Defined in:
lib/params_ready/parameter/enum_set_parameter.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractStructParameterDefinition

#key_map, #names

Attributes inherited from Definition

#default

Attributes inherited from AbstractDefinition

#altn, #name

Instance Method Summary collapse

Methods inherited from AbstractStructParameterDefinition

#child_definition, #duplicate_value, #freeze_value, #has_child?, #infer_default, #set_default

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, type: :boolean, **opts) ⇒ EnumSetParameterDefinition

Returns a new instance of EnumSetParameterDefinition.



67
68
69
70
71
# File 'lib/params_ready/parameter/enum_set_parameter.rb', line 67

def initialize(*args, type: :boolean, **opts)
  @type = type
  @values = {}
  super *args, **opts
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



61
62
63
# File 'lib/params_ready/parameter/enum_set_parameter.rb', line 61

def type
  @type
end

#valuesObject (readonly)

Returns the value of attribute values.



61
62
63
# File 'lib/params_ready/parameter/enum_set_parameter.rb', line 61

def values
  @values
end

Instance Method Details

#add_child(child, value:) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/params_ready/parameter/enum_set_parameter.rb', line 90

def add_child(child, value:)
  value = value.nil? ? child.name : value

  if @values.key(value).nil?
    @values[child.name] = value
  else
    raise ParamsReadyError, "Value '#{value}' already taken by '#{@values.key(value)}'"
  end
  super child
end

#ensure_canonical(set) ⇒ Object

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/params_ready/parameter/enum_set_parameter.rb', line 73

def ensure_canonical(set)
  raise ParamsReadyError, "Unexpected default type: #{set.class.name}" unless set.is_a?(Set)

  context = Format.instance(:backend)
  value, _validator = try_canonicalize set, context, nil, freeze: true
  return value if value.length == set.length

  extra_keys = set.reject do |key|
    value.key?(key) || value.key?(key.to_s)
  end.map do |key|
    "'#{key.to_s}'"
  end.join(", ")
  raise ParamsReadyError, "extra elements found -- #{extra_keys}" if extra_keys.length > 0

  value
end