Sha256: c07657bc0ad7eda1a510e2a1e5bda7a65a4648af77c73daaec5fa3243422892a

Contents?: true

Size: 1.8 KB

Versions: 11

Compression:

Stored size: 1.8 KB

Contents

module RulesEngine
  class Discovery
    
    @rules_path = nil
    @rule_classes = nil
    @rule_groups = nil
        
    class << self
      def rules_path= path
        @rules_path = path
      end  

      def rules_path
        return @rules_path if @rules_path
        return File.expand_path(File.join(Rails.root, '/app/rules')) if defined?(Rails) && Rails.root
        throw Exception.new("Rails.root or rules_path not defined") 
      end  
      
      def discover!
        @rule_classes = []
        @rule_groups = {}
        
        Dir.glob("#{rules_path}/**/*.rb").each do |rule| 
          Kernel.load rule

          rule_class = Rule.const_get(File.basename(rule, '.rb').classify)
          @rule_classes << rule_class
          
          group = rule_class.options[:group]
          @rule_groups[group] = [] unless @rule_groups.include?(group)
          @rule_groups[group] << rule_class
        end  
        
        @rule_classes.sort! { |left, right| return left.to_s < right.to_s}
        @rule_groups.each { |group, rule_classes| rule_classes.sort!  { |left, right| return left.to_s < right.to_s} }
        @rule_groups.each { |key, value| value.sort }
      end
    
      def rule_classes
        throw Exception.new("RulesEngine::Discovery.discover! required in environment.rb") if @rule_classes.nil?
        @rule_classes
      end

      def rule_groups
        throw Exception.new("RulesEngine::Discovery.discover! required in environment.rb") if @rule_classes.nil?
        @rule_groups
      end
    
      def rule_class(rule_class_name)
        throw Exception.new("RulesEngine::Discovery.discover! required in environment.rb") if @rule_classes.nil?
        @rule_classes.each do |rule|
          return rule if rule.rule_class_name == rule_class_name
        end
        nil
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rules_engine-0.3.2 lib/rules_engine/discovery.rb
rules_engine-0.3.1 lib/rules_engine/discovery.rb
rules_engine-0.3.0 lib/rules_engine/discovery.rb
rules_engine-0.2.0 lib/rules_engine/discovery.rb
rules_engine-0.1.10 lib/rules_engine/discovery.rb
rules_engine-0.1.9 lib/rules_engine/discovery.rb
rules_engine-0.1.8 lib/rules_engine/discovery.rb
rules_engine-0.1.7 lib/rules_engine/discovery.rb
rules_engine-0.1.6 lib/rules_engine/discovery.rb
rules_engine-0.1.5 lib/rules_engine/discovery.rb
rules_engine-0.1.4 lib/rules_engine/discovery.rb