Class: Eaco::Adapters::ActiveRecord::Compatibility

Inherits:
Object
  • Object
show all
Defined in:
lib/eaco/adapters/active_record/compatibility.rb,
lib/eaco/adapters/active_record/compatibility/v32.rb,
lib/eaco/adapters/active_record/compatibility/v40.rb,
lib/eaco/adapters/active_record/compatibility/v41.rb,
lib/eaco/adapters/active_record/compatibility/v42.rb,
lib/eaco/adapters/active_record/compatibility/scoped.rb

Overview

Sets up JSONB support for the different AR versions

Defined Under Namespace

Modules: Scoped, V32, V40, V41, V42

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Compatibility

Memoizes the given model for later #check! calls.

Parameters:

  • model (ActiveRecord::Base)

    the model to check



21
22
23
# File 'lib/eaco/adapters/active_record/compatibility.rb', line 21

def initialize(model)
  @model = model
end

Instance Method Details

#active_record_versionString (private)

Example: “42” for 4.2

Returns:

  • (String)

    the ActiveRecord major and minor version numbers



52
53
54
55
# File 'lib/eaco/adapters/active_record/compatibility.rb', line 52

def active_record_version
  ver = target.parent.const_get(:VERSION)
  [ver.const_get(:MAJOR), ver.const_get(:MINOR)].join
end

#check!

This method returns an undefined value.

Checks whether the target model is compatible. Looks up the #support_module and includes it.

See Also:



33
34
35
36
# File 'lib/eaco/adapters/active_record/compatibility.rb', line 33

def check!
  layer = support_module
  target.instance_eval { include layer }
end

#support_moduleModule (private)

Tries to look up the support module for the #active_record_version in the Eaco::Adapters::ActiveRecord::Compatibility namespace.

Returns:

  • (Module)

    the support module

Raises:

See Also:



67
68
69
70
71
72
73
74
75
# File 'lib/eaco/adapters/active_record/compatibility.rb', line 67

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_nameString (private)

Example: “V32” for Rails 3.2.

Returns:



82
83
84
# File 'lib/eaco/adapters/active_record/compatibility.rb', line 82

def support_module_name
  ['V', active_record_version].join
end

#targetActiveRecord::Base (private)

Returns associated with the model.

Returns:

  • (ActiveRecord::Base)

    associated with the model



43
44
45
# File 'lib/eaco/adapters/active_record/compatibility.rb', line 43

def target
  @model.base_class.superclass
end