Sha256: dc1e0033c069f644fec997e9f681284ec88f6fff27ef62bce872490aacfedef9

Contents?: true

Size: 1002 Bytes

Versions: 1

Compression:

Stored size: 1002 Bytes

Contents

# frozen_string_literal: true

module Schema
  module Normalize
    def self.included(base)
      base.extend InheritanceHelper::Methods
      base.extend ClassMethods
    end

    module ClassMethods
      def schema_normalizations
        [].freeze
      end

      def create_schema_model_normalizer
        normalizer = ::Schema::ModelNormalizer.new
        schema_normalizations.each do |(attribute_name, method_name, options)|
          normalizer.add(attribute_name, method_name, options)
        end
        normalizer
      end

      def schema_model_normalizer
        @schema_model_normalizer ||= create_schema_model_normalizer
      end

      def normalize(attribute_name, method_name = nil, options = {}, &block)
        new_value = schema_normalizations.dup << [attribute_name, method_name || block, options]
        redefine_class_method(:schema_normalizations, new_value)
      end
    end

    def normalize
      self.class.schema_model_normalizer.normalize(self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schema-normalize-0.1.2 lib/schema/normalize.rb