Module: Annotatable::ClassMethods

Defined in:
lib/annotatable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



11
12
13
14
15
16
# File 'lib/annotatable.rb', line 11

def self.extended(base)
  base.instance_eval do
    alias :inherited_without_annotatable :inherited
    alias :inherited :inherited_with_annotatable
  end
end

Instance Method Details

#annotate(*attrs) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/annotatable.rb', line 18

def annotate(*attrs)
  options = {}
  options = attrs.pop if attrs.last.kind_of?(Hash)
  options.symbolize_keys!
  inherit = options[:inherit]
  if inherit
    @inherited_annotations ||= []
    @inherited_annotations += attrs
  end
  attrs.each do |attr|
    class_eval %{
      def self.#{attr}(string = nil)
        @#{attr} = string || @#{attr}
      end
      def #{attr}(string = nil)
        self.class.#{attr}(string)
      end
      def self.#{attr}=(string = nil)
        #{attr}(string)
      end
      def #{attr}=(string = nil)
        self.class.#{attr}=(string)
      end
    }
  end
  attrs
end

#inherited_with_annotatable(subclass) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/annotatable.rb', line 46

def inherited_with_annotatable(subclass)
  inherited_without_annotatable(subclass)
  (["inherited_annotations"] + (@inherited_annotations || [])).each do |t|
    ivar = "@#{t}" 
    subclass.instance_variable_set(ivar, instance_variable_get(ivar))
  end
end