class Eaco::Adapters::ActiveRecord::Compatibility

Sets up JSONB support for the different AR versions

Public Class Methods

new(model) click to toggle source

Memoizes the given model for later {#check!} calls.

@param model [ActiveRecord::Base] the model to check

# File lib/eaco/adapters/active_record/compatibility.rb, line 24
def initialize(model)
  @model = model
end

Public Instance Methods

check!() click to toggle source

Checks whether ActiveRecord::Base is compatible. Looks up the {#support_module} and includes it.

@see support_module

@return [void]

# File lib/eaco/adapters/active_record/compatibility.rb, line 36
def check!
  layer = support_module
  ::ActiveRecord::Base.instance_eval { include layer }
end

Private Instance Methods

active_record_version() click to toggle source

@return [String] the ActiveRecord major and minor version numbers

Example: “42” for 4.2

# File lib/eaco/adapters/active_record/compatibility.rb, line 48
def active_record_version
  [
    ::ActiveRecord::VERSION::MAJOR,
    ::ActiveRecord::VERSION::MINOR
  ].join
end
support_module() click to toggle source

Tries to look up the support module for the {#active_record_version} in the {Compatibility} namespace.

@return [Module] the support module

@raise [Eaco::Error] if not found.

@see check!

# File lib/eaco/adapters/active_record/compatibility.rb, line 65
        def support_module
          unless self.class.const_defined?(support_module_name)
            raise Eaco::Error, <<-EOF
              Unsupported Active Record version: #{active_record_version}
            EOF
          end

          self.class.const_get support_module_name
        end
support_module_name() click to toggle source

@return [String] “V” with {.active_record_version} appended.

Example: “V32” for Rails 3.2.

# File lib/eaco/adapters/active_record/compatibility.rb, line 80
def support_module_name
  ['V', active_record_version].join
end