Sha256: 100e96fa41d3f0e07a1588fd53607f0888e7153014bab944ac8df0c15092ac37

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Datacaster
  module Mixin
    def &(other)
      AndNode.new(self, other)
    end

    def |(other)
      OrNode.new(self, other)
    end

    def *(other)
      AndWithErrorAggregationNode.new(self, other)
    end

    def cast_errors(error_caster)
      ContextNodes::ErrorsCaster.new(self, error_caster)
    end

    def then(other)
      ThenNode.new(self, DefinitionDSL.expand(other))
    end

    def with_context(context)
      unless context.is_a?(Hash)
        raise "with_context expected Hash as argument, got #{context.inspect} instead"
      end
      ContextNodes::UserContext.new(self, context)
    end

    def call(object)
      call_with_runtime(object, Runtimes::Base.new)
    end

    def call_with_runtime(object, runtime)
      result = cast(object, runtime: runtime)
      unless result.is_a?(Result)
        raise RuntimeError.new("Caster should've returned Datacaster::Result, but returned #{result.inspect} instead")
      end
      result
    end

    def with_runtime(runtime)
      ->(object) do
        call_with_runtime(object, runtime)
      end
    end

    def i18n_key(*keys, **args)
      ContextNodes::I18n.new(self, I18nValues::Key.new(keys, args))
    end

    def i18n_map_keys(mapping)
      ContextNodes::I18nKeysMapper.new(self, mapping)
    end

    def i18n_scope(scope, **args)
      ContextNodes::I18n.new(self, I18nValues::Scope.new(scope, args))
    end

    def i18n_vars(vars)
      ContextNodes::I18n.new(self, I18nValues::Scope.new(nil, vars))
    end

    def inspect
      "#<Datacaster::Base>"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
datacaster-3.1.5 lib/datacaster/mixin.rb
datacaster-3.1.3 lib/datacaster/mixin.rb
datacaster-3.1.2 lib/datacaster/mixin.rb