Sha256: a9756048e026d7261115a6e6b282855c686c144e944cdd032d44a31a0105b9cf

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module AxleAttributes
  class Definition
    module Formatted
      extend ActiveSupport::Concern

      included do
        class_attribute :formatters
        self.formatters = {}

        setup do
          setup_method_override!
          formatter.try(:setup!, self)
        end
      end

      def analyzer_mapping
        if mapping = formatter.try(:analyzer_mapping)
          mapping
        else
          super
        end
      end

      def instructions
        fetch_format_property(:instructions)
      end

      def standardize(value)
        formatter.try(:standardize, self, value) || value
      end

      def validate(value)
        formatter.try :validate, self, value
      end

      def formatter
        formatters[format]
      end

      private

        def fetch_format_property(property)
          read_customization(property) || formatter.try(property, self)
        end

        def setup_method_override!
          if formatter.respond_to?(:standardize)
            definition = self
            model.send :define_method, "#{name}=" do |value|
              super definition.standardize(value)
            end
          end
        end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
axle_attributes-1.13.2 lib/axle_attributes/definition/formatted.rb