Sha256: f7f1739259c1a4465c14c1be7fba3bf29f5835474391e9d0c919415f8400b2e6

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module MdData
  def self.included(base)
    base.extend MdDataClassMethods
  end

  module MdDataClassMethods
    def table_data(&block)
      @table_data_block = block
    end

    def select(attributes)
      @rules = []
      @current_context = nil
      load_rules
      container = self.new
      attributes.each do |key,value|
        self.send(:define_method, key) do
          value
        end
      end
      self.send(:define_method, :select_from_rules) do |rules|
        result = nil
        rules.each do |rule|
          begin 
            rule_is_satisfied = eval(rule[1])
            if rule_is_satisfied
              result = rule[0]
              break
            end
          rescue
            #nasty, allows me not to define dimensions for now TODO: fix
          end
        end
        result
      end
      container.select_from_rules(@rules)
    end

 private
    def load_rules
      @table_data_block.call
    end

    def add(value, condition)
      context_condition = @current_context.nil? ? '' : " && #{@current_context} "
      @rules << ( [value , (condition + context_condition)] )
    end

    def context(condition, &block)
      @current_context = condition
      block.call
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
md_data-1.0.0 lib/md_data/md_data.rb