Skip to content

Instantly share code, notes, and snippets.

@drKreso
Created March 25, 2012 03:53
Show Gist options
  • Save drKreso/2191268 to your computer and use it in GitHub Desktop.
Save drKreso/2191268 to your computer and use it in GitHub Desktop.
This is implementation
module MultidimensionalTable
@context = []
@table_rules = {}
@attributes ||= {}
@index_level = 0
def dimensions=(map)
@dimensions = map
@dimensions.each do |key, value|
value.each do |possible_value|
define_method possible_value do |value = nil, &block|
if value.nil? && !block.nil?
@index_level += 1
@context[@index_level] = "@attributes[:#{key}] == :#{possible_value}"
block.call
@index_level -= 1
elsif !value.nil?
context = (1..@index_level).reduce([]) { |context, level| context << @context[level] }
@table_rules[value] = context << ["@attributes[:#{key}] == :#{possible_value}"]
end
end
end
end
end
def update_attributes(attrs)
attrs.each do |key, value| @attributes[key] = value end
end
def table_result
@table_rules.each { |key, condition| return key if class_eval(condition.join(' && ')) == true }
end
def table_data
yield
end
def dimensions
@dimensions ||= {}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment