Method: Attributor::Hash.describe

Defined in:
lib/attributor/types/hash.rb

.describe(shallow = false, example: nil) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/attributor/types/hash.rb', line 448

def self.describe(shallow = false, example: nil)
  hash = super(shallow)

  hash[:key] = { type: key_type.describe(true) } if key_type

  if keys.any?
    # Spit keys if it's the root or if it's an anonymous structures
    if ( !shallow || self.name == nil)
      required_names_from_attr = []
      # FIXME: change to :keys when the praxis doc browser supports displaying those
      hash[:attributes] = self.keys.each_with_object({}) do |(sub_name, sub_attribute), sub_attributes|
        required_names_from_attr << sub_name if sub_attribute.options[:required] == true
        sub_example = example.get(sub_name) if example
        sub_attributes[sub_name] = sub_attribute.describe(true, example: sub_example)
      end
      hash[:requirements] = requirements.each_with_object([]) do |req, list|
        described_req = req.describe(shallow)
        if described_req[:type] == :all
          # Add the names of the attributes that have the required flag too
          described_req[:attributes] |= required_names_from_attr
          required_names_from_attr = []
        end
        list << described_req
      end
      # Make sure we create an :all requirement, if there wasn't one so we can add the required: true attributes
      unless required_names_from_attr.empty?
        hash[:requirements] << {type: :all, attributes: required_names_from_attr }
      end
    end
  else
    hash[:value] = { type: value_type.describe(true) }
    hash[:example] = example if example
    hash[:attributes] = {}
  end

  hash
end