Module: AnnotatedModule

Defined in:
lib/rbbt/workflow/annotate.rb,
lib/rbbt/util/misc/annotated_module.rb

Class Method Summary collapse

Class Method Details

.add_consummable_annotation(target, *annotations) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rbbt/workflow/annotate.rb', line 3

def self.add_consummable_annotation(target, *annotations)
  if annotations.length == 1 and Hash === annotations.first
    annotations.first.each do |annotation, default|
      target.send(:attr_accessor, annotation)
      target.send(:define_method, "consume_#{annotation}") do
        value = instance_variable_get("@#{annotation}") || default.dup
        instance_variable_set("@#{annotation}", default.dup)
        value
      end
    end
  else
    annotations.each do |annotation|
      target.send(:attr_accessor, annotation)
      target.send(:define_method, "consume_#{annotation}") do
        value = instance_variable_get("@#{annotation}")
        instance_variable_set("@#{annotation}", nil)
      end
    end
  end
end