kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.1 KiB
62 lines
2.1 KiB
15 years ago
|
require 'set'
|
||
|
|
||
14 years ago
|
module Report::InheritedAttribute
|
||
14 years ago
|
def inherited_attribute(*attributes)
|
||
15 years ago
|
options = attributes.extract_options!
|
||
|
list = options[:list]
|
||
14 years ago
|
merge = options.include?(:merge) ? options[:merge] : options[:list]
|
||
15 years ago
|
default = options[:default]
|
||
|
uniq = options[:uniq]
|
||
|
map = options[:map] || proc { |e| e }
|
||
|
default ||= [] if list
|
||
|
attributes.each do |name|
|
||
|
define_singleton_method(name) do |*values|
|
||
14 years ago
|
# FIXME: I'm ugly
|
||
15 years ago
|
return get_inherited_attribute(name, default, list, uniq) if values.empty?
|
||
14 years ago
|
if list
|
||
|
old = instance_variable_get("@#{name}") if merge
|
||
|
old ||= []
|
||
|
return set_inherited_attribute(name, values.map(&map) + old)
|
||
|
end
|
||
15 years ago
|
raise ArgumentError, "wrong number of arguments (#{values.size} for 1)" if values.size > 1
|
||
|
set_inherited_attribute name, map.call(values.first)
|
||
|
end
|
||
|
define_method(name) { |*values| self.class.send(name, *values) }
|
||
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
alias singleton_class metaclass unless respond_to? :singleton_class
|
||
|
|
||
15 years ago
|
def define_singleton_method(name, &block)
|
||
14 years ago
|
singleton_class.send :attr_writer, name
|
||
14 years ago
|
singleton_class.class_eval { define_method(name, &block) }
|
||
|
define_method(name) { instance_variable_get("@#{name}") or singleton_class.send(name) }
|
||
15 years ago
|
end
|
||
|
|
||
|
def get_inherited_attribute(name, default = nil, list = false, uniq = false)
|
||
|
return get_inherited_attribute(name, default, list, false).uniq if list and uniq
|
||
15 years ago
|
result = instance_variable_get("@#{name}")
|
||
|
super_result = superclass.get_inherited_attribute(name, default, list) if inherit? name
|
||
15 years ago
|
if result.nil?
|
||
|
super_result || default
|
||
|
else
|
||
|
list && super_result ? result + super_result : result
|
||
|
end
|
||
|
end
|
||
|
|
||
15 years ago
|
def inherit?(name)
|
||
|
superclass.respond_to? :get_inherited_attribute and not not_inherited.include? name
|
||
|
end
|
||
|
|
||
|
def not_inherited
|
||
|
@not_inherited ||= Set.new
|
||
|
end
|
||
|
|
||
|
def dont_inherit(*attributes)
|
||
|
not_inherited.merge attributes
|
||
|
end
|
||
|
|
||
15 years ago
|
def set_inherited_attribute(name, value)
|
||
|
instance_variable_set "@#{name}", value
|
||
|
end
|
||
|
end
|