module AttributeInquiry::ClassMethods

Defines base methods for use in {self.included} for extend

Public Instance Methods

has_attribute_inquiry(*keys, **options) click to toggle source

rubocop:disable Naming/PredicateName rubocop:disable Metrics/MethodLength

# File lib/attribute_inquiry.rb, line 23
    def has_attribute_inquiry(*keys, **options)
      keys.each do |key|
        parent_method = if options[:activerecord_raw]
                          "read_attribute(#{key})"
                        else
                          'super'
                        end

        class_eval(
          <<-DEFINITION, __FILE__, __LINE__ + 1
            def #{key}
              ActiveSupport::StringInquirer.new(
                self.instance_variable_get('@#{key}') ||
                #{parent_method}
              )
            end
          DEFINITION
        )
      end
    end