module Integrative::Integrator

Public Instance Methods

define_integration(name, options) click to toggle source
# File lib/integrative/integrator.rb, line 48
def define_integration(name, options)
  integration = Integration.new(name, self, options)
  self.integrations_defined ||= []
  self.integrations_defined << integration
  self.class_eval do
    define_integration_method(name, integration)
  end
end
define_integration_method(name, integration) click to toggle source
# File lib/integrative/integrator.rb, line 57
def define_integration_method(name, integration)
  attr_accessor name

  define_method name do
    integrative_dynamic_method_call(name, integration)
  end
end
initialize_integrative(name) click to toggle source
# File lib/integrative/integrator.rb, line 38
def initialize_integrative(name)
  if self.instance_methods.include? name
    raise Errors::MethodAlreadyExistsError.new(self, name)
  end
  if !defined?(integrations_defined)
    patch_activerecord_relation_for_integrative
    class_attribute :integrations_defined
  end
end
integrate(*name_or_names, **options) click to toggle source
# File lib/integrative/integrator.rb, line 22
def integrate(*name_or_names, **options)
  if all.public_methods.include? :integrate
    all.integrate(*name_or_names, **options)
  else
    raise Errors::IntegrationDefinitionMissingError.new(self, name_or_names)
  end
end
integrates(name, options = {}) click to toggle source
# File lib/integrative/integrator.rb, line 17
def integrates(name, options = {})
  initialize_integrative(name)
  define_integration(name, options)
end
integrative_dynamic_method_call(name, integration) click to toggle source
# File lib/integrative/integrator.rb, line 5
def integrative_dynamic_method_call(name, integration)
  ivar = "@#{name}"
  if instance_variable_defined? ivar
    instance_variable_get ivar
  else
    Rails.logger.info "Integrations fetched for a single #{self.class.name} record."
    integration.integrated_class.integrative_find_and_assign([self], integration)
    instance_variable_get ivar
  end
end
patch_activerecord_relation_for_integrative() click to toggle source
# File lib/integrative/integrator.rb, line 30
def patch_activerecord_relation_for_integrative
  self::ActiveRecord_Relation.class_eval do
    include Integrative::Extensions::RelationExtension
  end
end