Sha256: 8be6f7b68a1f61b0972997bc10837c2ae945126219c71e253e418cfc5d6a9f21

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

require "dry-container"
require "kanji/types"
require "kanji/type/class_interface"
require "kanji/errors"

module Kanji
  class Type
    extend Dry::Container::Mixin
    extend ClassInterface

    include Dry::Container::Mixin

    def initialize(params)
      result = self.class.resolve(:schema).call(params)

      if result.success?
        register :value, -> { self.class.resolve(:value_object).new(params) }
      else
        errors = parse_error_messages(result)
        raise ValidationError, "Schema validation failed - #{errors}"
      end

      self.class.instance_variable_get(:@_values).each do |key, value|
        register key.to_sym, value
      end

      if repo = self.class.resolve(:repo)
        register :repo, repo
      end
    end

    def to_h
      resolve(:value).to_h
    end

    def to_json
      to_h.to_json
    end

    def method_missing(method_name, *args, &block)
      if resolve(:value).respond_to?(method_name)
        resolve(:value).send(method_name, *args)
      else
        super(method_name, *args, &block)
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      if resolve(:value).respond_to?(method_name)
        true
      else
        super(method_name, include_private)
      end
    end

    private

    def parse_error_messages(result)
      result.messages(full: true).values.flatten.join(",")
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kanji-web-0.3.1 lib/kanji/type.rb
kanji-web-0.3.0 lib/kanji/type.rb
kanji-web-0.2.2 lib/kanji/type.rb
kanji-web-0.2.1 lib/kanji/type.rb
kanji-web-0.2.0 lib/kanji/type.rb
kanji-web-0.1.0 lib/kanji/type.rb