Sha256: 5a9409a6ffcc2319733ac5f2f36dc34a489ce219649d9775d25b403b15347072

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module Hatio
  module ExtensionLogic
    
    def self.included(base)
      super
      base.extend(ClassMethods)
      base.class_eval do
        include InstanceMethods
      end
    end

    module ClassMethods
      def run_class_logic(logic_name, params)
        entity = Entity.find_by_name(self.name)
        raise Hatio::Exception::MisConfigured, (I18n.translate 'errors.messages.x_not_found', :x => "Entity (#{self.name})") unless entity
        logic = entity.entity_logics.where("level = ? and name = ?", :class.to_s, logic_name).first
        raise Hatio::Exception::MisConfigured, (I18n.translate 'errors.messages.x_not_found', :x => "EntityLogic (#{logic_name}) of Entity (#{self.name})") unless logic
        raise Hatio::Exception::MisConfigured, (I18n.translate 'errors.messages.empty_x_not_allowed', :x => "EntityLogic (#{logic_name}) of Entity (#{self.name})") if (logic.logic.nil? || logic.logic.empty?)
        return self.instance_eval logic.logic
      end
    end

    module InstanceMethods
      public
        def run_instance_logic(logic_name, params)
          entity = Entity.find_by_name(self.class.name)
          raise Hatio::Exception::MisConfigured, (I18n.translate 'errors.messages.x_not_found', :x => "Entity (#{self.class.name})") unless entity
          logic = entity.entity_logics.where("level = ? and name = ?", :instance.to_s, logic_name).first
          raise Hatio::Exception::MisConfigured, (I18n.translate 'errors.messages.x_not_found', :x => "EntityLogic (#{logic_name}) of Entity (#{self.class.name})") unless logic
          raise Hatio::Exception::MisConfigured, (I18n.translate 'errors.messages.empty_x_not_allowed', :x => "EntityLogic (#{logic_name}) of Entity (#{self.class.name})") if (logic.logic.nil? || logic.logic.empty?)
          return self.instance_eval logic.logic
        end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hatio-core-0.0.6 lib/hatio-core/active_record/extension_logic.rb