Sha256: 90329839736fcadc8d5e32d48b33a74fce873be2e8f7c4610721c127f0b46b4a

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

module Attestor

  module Validations

    # Bulder for error messages
    #
    # @api private
    class Message < String

      # @!scope class
      # @!method new(value, object, options = {})
      # Builds a string from value
      #
      # @param [#to_s] value
      # @param [Object] object
      # @param [Hash] options
      #   options for translating symbolic value
      #
      # @return [String]
      #   either translation of symbolic value or stringified value argument

      # @private
      def initialize(value, object, options = {})
        @value   = value
        @object  = object
        @options = options
        super(@value.instance_of?(Symbol) ? translation : @value.to_s)
        freeze
      end

      private

      def translation
        I18n.t @value, @options.merge(scope: scope, default: default)
      end

      def scope
        %W(attestor errors #{ class_scope })
      end

      def class_scope
        @object.class.to_s.split("::").map(&:snake_case).join("/")
      end

      def default
        "#{ @object } is invalid (#{ @value })"
      end

    end # class Message

  end # module Validations

end # module Attestor

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
attestor-1.0.0 lib/attestor/validations/message.rb
attestor-0.4.0 lib/attestor/validations/message.rb
attestor-0.3.0 lib/attestor/validations/message.rb
attestor-0.2.0 lib/attestor/validations/message.rb
attestor-0.1.0 lib/attestor/validations/message.rb
attestor-0.0.1 lib/attestor/validations/message.rb