Sha256: 4fac8ec375228a0cf3f1293d7ecac04a785cf405df04f0182d361463b3ed53e1

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module Yaks
  class Config
    include Yaks::FP::Callable

    # @!attribute [r] format_options
    #   @return [Hash<Symbol,Hash>]
    attr_reader :format_options

    # @!attribute [rw] default_format
    #   @return [Symbol]
    attr_accessor :default_format

    # @!attribute [rw] policy_class
    #   @return [Class]
    attr_accessor :policy_class

    # @!attribute [r] policy_options
    #   @return [Hash]
    attr_reader :policy_options

    # @!attribute [rw] primitivize
    #   @return [Primitivize]
    attr_accessor :primitivize

    # @!attribute [r] serializers
    #   @return [Hash<Symbol,#call>]
    attr_reader :serializers

    # @!attribute [r] hooks
    #   @return [Array]
    attr_reader :hooks

    # @param blk [Proc] Configuration block
    def initialize(&blk)
      @format_options = Hash.new({})
      @default_format = :hal
      @policy_options = {}
      @primitivize    = Primitivize.create
      @serializers    = Serializer.all.dup
      @hooks          = []

      DSL.new(self, &blk)
    end

    # @return [Yaks::DefaultPolicy]
    def policy
      @policy ||= @policy_class.new(@policy_options)
    end

    def runner(object, options)
      Runner.new(config: self, object: object, options: options)
    end

    # Main entry point into yaks
    #
    # @param object [Object] The object to serialize
    # @param options [Hash<Symbol,Object>] Serialization options
    #
    # @option env [Hash] The rack environment
    # @option format [Symbol] The target format, default :hal
    # @option mapper [Class] Mapper class to use
    # @option item_mapper [Class] Mapper class to use for items in a top-level collection
    #
    def call(object, options = {})
      runner(object, options).call
    end
    alias serialize call

    def map(object, options = {})
      runner(object, options).map(object)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaks-0.7.7 lib/yaks/config.rb