Sha256: 5edc301bb9b669800c0162dfc9d2385a3e8fb3504040cabd85fabf40561e1fdd

Contents?: true

Size: 1.1 KB

Versions: 15

Compression:

Stored size: 1.1 KB

Contents

require 'zeitwerk'
loader = Zeitwerk::Loader.for_gem
loader.inflector.inflect('definition_dsl' => 'DefinitionDSL')
loader.setup

require_relative 'datacaster/result'

module Datacaster
  extend self

  def schema(i18n_scope: nil, &block)
    ContextNodes::StructureCleaner.new(build_schema(i18n_scope: i18n_scope, &block), :fail)
  end

  def choosy_schema(i18n_scope: nil, &block)
    ContextNodes::StructureCleaner.new(build_schema(i18n_scope: i18n_scope, &block), :remove)
  end

  def partial_schema(i18n_scope: nil, &block)
    ContextNodes::StructureCleaner.new(build_schema(i18n_scope: i18n_scope, &block), :pass)
  end

  def absent
    Datacaster::Absent.instance
  end

  def instance?(object)
    object.is_a?(Mixin)
  end

  private

  def build_schema(i18n_scope: nil, &block)
    raise "Expected block" unless block

    datacaster = DefinitionDSL.eval(&block)

    unless Datacaster.instance?(datacaster)
      raise "Datacaster instance should be returned from a block (e.g. result of 'hash_schema(...)' call)"
    end

    datacaster = datacaster.i18n_scope(i18n_scope) if i18n_scope

    datacaster
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
datacaster-4.1.0 lib/datacaster.rb
datacaster-4.0.1 lib/datacaster.rb
datacaster-3.3.1 lib/datacaster.rb
datacaster-3.3.0 lib/datacaster.rb
datacaster-3.2.7 lib/datacaster.rb
datacaster-3.2.6 lib/datacaster.rb
datacaster-3.2.5 lib/datacaster.rb
datacaster-3.2.4 lib/datacaster.rb
datacaster-3.2.3 lib/datacaster.rb
datacaster-3.2.2 lib/datacaster.rb
datacaster-3.2.1 lib/datacaster.rb
datacaster-3.2.0 lib/datacaster.rb
datacaster-3.1.5 lib/datacaster.rb
datacaster-3.1.3 lib/datacaster.rb
datacaster-3.1.2 lib/datacaster.rb