Class: ContractedValue::Value
- Inherits:
-
Object
- Object
- ContractedValue::Value
- Defined in:
- lib/contracted_value/core.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(input_attr_values = {}) ⇒ Value
constructor
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize.
-
#to_h ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity.
Constructor Details
#initialize(input_attr_values = {}) ⇒ Value
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/contracted_value/core.rb', line 240 def initialize(input_attr_values = {}) input_attr_values_hash = case input_attr_values when ::Hash input_attr_values when Value input_attr_values.to_h else raise( Errors::InvalidInputType.new( input_attr_values, ), ) end attribute_set = self.class.send(:attribute_set) attribute_set.each_attribute do |attribute| attr_value = attribute.extract_value(input_attr_values_hash) sometimes_frozen_attr_value = case attribute.refrigeration_mode when RefrigerationMode::Enum::DEEP # Use ice_nine for deep freezing ::IceNine.deep_freeze(attr_value) when RefrigerationMode::Enum::SHALLOW # No need to re-freeze attr_value.frozen? ? attr_value : attr_value.freeze when RefrigerationMode::Enum::NONE # No freezing attr_value else raise Errors::InvalidRefrigerationMode.new( refrigeration_mode, ) end # Using symbol since attribute names are limited in number # An alternative would be using frozen string instance_variable_set( :"@#{attribute.name}", sometimes_frozen_attr_value, ) end if self.class.send(:should_detect_unexpected_keys) unexpected_keys = attribute_set.detect_unexpected_keys(input_attr_values_hash) if unexpected_keys.any? raise Errors::UnexpectedInputKeys.new( unexpected_keys, ) end end freeze end |
Class Method Details
.inherited(child_klass) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/contracted_value/core.rb', line 307 def inherited(child_klass) super child_klass.instance_variable_set(:@attribute_set, AttributeSet.new) child_klass.instance_variable_set( :@should_detect_unexpected_keys, if child_klass.superclass.respond_to?(:should_detect_unexpected_keys, true) child_klass.superclass.send(:should_detect_unexpected_keys) else false end ) end |
Instance Method Details
#to_h ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity
298 299 300 301 302 303 |
# File 'lib/contracted_value/core.rb', line 298 def to_h self.class.send(:attribute_set). each_attribute.each_with_object({}) do |attribute, hash| hash[attribute.name] = instance_variable_get(:"@#{attribute.name}") end end |