Class: Plutonium::Wizard::Data::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/plutonium/wizard/data.rb

Overview

The step-keyed data snapshot โ€” a thin dispatcher over the per-step typed sub-objects (ยง2.6). data.identity (via method_missing) or data[:identity] returns the step's typed sub-object, built lazily from its nested data slice and memoized; an unknown step key returns nil. to_h gives the nested {step => {field => value}} view.

A plain object (not a generated class) so it isn't rebuilt every time the runner reassigns data_attributes; the per-step typed classes are built once per wizard class and passed in.

Instance Method Summary collapse

Constructor Details

#initialize(step_classes, attrs = {}) ⇒ Container

Returns a new instance of Container.

Parameters:

  • step_classes (Hash{Symbol=>Class})

    step key => typed sub-object class

  • attrs (Hash) (defaults to: {})

    nested staged data (=> {field => value})



103
104
105
106
107
# File 'lib/plutonium/wizard/data.rb', line 103

def initialize(step_classes, attrs = {})
  @step_classes = step_classes
  @attrs = (attrs || {}).transform_keys(&:to_sym)
  @objects = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

data.identity โ†’ the identity step's typed sub-object.



127
128
129
130
# File 'lib/plutonium/wizard/data.rb', line 127

def method_missing(name, *args)
  return self[name] if args.empty? && @step_classes.key?(name.to_sym)
  super
end

Instance Method Details

#[](key) ⇒ Object

The typed sub-object for a step (lazy + memoized); nil for an unknown step.



110
111
112
113
114
# File 'lib/plutonium/wizard/data.rb', line 110

def [](key)
  key = key.to_sym
  return nil unless @step_classes.key?(key)
  @objects[key] ||= @step_classes[key].new(@attrs[key] || {})
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/plutonium/wizard/data.rb', line 122

def respond_to_missing?(name, include_private = false)
  @step_classes.key?(name.to_sym) || super
end

#step_keysObject

The declared step keys, in order.



117
# File 'lib/plutonium/wizard/data.rb', line 117

def step_keys = @step_classes.keys

#to_hObject

Nested typed hash: => {field => value}.



120
# File 'lib/plutonium/wizard/data.rb', line 120

def to_h = @step_classes.keys.index_with { |key| self[key].to_h }