Sha256: c5756d9829250fa35e6b5fe2bb99051dcc494a3e054b3a8ca21e2c9896052ae4

Contents?: true

Size: 647 Bytes

Versions: 1

Compression:

Stored size: 647 Bytes

Contents

# frozen_string_literal: true

module ModelAuditor
  module AttributeReaders
    class Associated
      attr_reader :model, :key

      def initialize(model, key)
        @model = model
        @key = key
      end

      def value
        return unless method_name

        model.send(method_name)&.title
      end

      protected

      def attr_name(pattern, replacement = '')
        return unless key.to_s =~ pattern
        attribute_name = key.to_s.gsub(pattern, replacement)

        attribute_name if model.respond_to?(attribute_name)
      end

      private

      def method_name
        attr_name(/_id$/)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
model_auditor-0.0.1 lib/model_auditor/attribute_readers/associated.rb