Sha256: e368e595e64245b2f3f39e03936061b6a64440ef2f2659dd8cfade0f77ebf7b7

Contents?: true

Size: 756 Bytes

Versions: 3

Compression:

Stored size: 756 Bytes

Contents

module RiceBubble
  class Attributes
    class Object < Base
      attr_reader :children

      def initialize(children = {}, &)
        super(&)
        @children = Attributes.new(
          children.transform_values(&method(:instantiate))
        )
      end

      def valid?(value)
        children.all? do |name, attr|
          attr.valid?(value[name])
        end
      end

      def validate!(value, path:, **)
        children.each do |name, attr|
          child = attr.fetch(value, name)
          coerced = attr.coerce(child)
          attr.validate!(child, coerced:, path: "#{path}.#{name}")
        end
      end

      def coerce(value)
        children.map { |name, attr| attr.coerce(attr.fetch(value, name)) }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rice_bubble-0.1.2 lib/rice_bubble/attributes/object.rb
rice_bubble-0.1.1 lib/rice_bubble/attributes/object.rb
rice_bubble-0.1.0 lib/rice_bubble/attributes/object.rb