Module: Anise::Annotation::Aid

Defined in:
lib/anise/annotation.rb

Overview

Anise::Annotations Domain Language.

Instance Method Summary collapse

Instance Method Details

#ann(ref, keys_or_class = nil, keys = nil) ⇒ Object

Set or read annotations.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/anise/annotation.rb', line 105

def ann(ref, keys_or_class=nil, keys=nil)
  return annotation(ref) unless keys_or_class or keys

  if Class === keys_or_class
    keys ||= {}
    keys[:class] = keys_or_class
  else
    keys = keys_or_class
  end

  if Hash === keys
    ref  = ref.to_sym
    keys = keys.inject({}){ |h,(k,v)| h[k.to_sym] = v; h} #rekey
    annotations[ref] ||= {}
    annotations[ref].update(keys)
    # callback
    annotation_added(ref) #if method_defined?(:annotation_added)
  else
    key = keys.to_sym
    annotation(ref)[key]
  end
end

#ann!(ref, keys_or_class = nil, keys = nil) ⇒ Object

To change an annotation’s value in place for a given class or module it first must be duplicated, otherwise the change may effect annotations in the class or module’s ancestors.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/anise/annotation.rb', line 132

def ann!(ref, keys_or_class=nil, keys=nil)
  #return annotation(ref) unless keys_or_class or keys
  unless keys_or_class or keys
    return annotations[ref] ||= {}
  end

  if Class === keys_or_class
    keys ||= {}
    keys[:class] = keys_or_class
  else
    keys = keys_or_class
  end

  if Hash === keys
    ref  = ref.to_sym
    keys = keys.inject({}){ |h,(k,v)| h[k.to_sym] = v; h} #rekey
    annotations[ref] ||= {}
    annotations[ref].update(keys)
    # callback
    annotation_added(ref) #if method_defined?(:annotation_added)
  else
    key = keys.to_sym
    annotations[ref] ||= {}
    begin
      annotations[ref][key] = annotation(ref)[key].dup
    rescue TypeError
      annotations[ref][key] = annotation(ref)[key]
    end
  end
end

#annotation(ref = nil) ⇒ Object Also known as: annotations

Lookup an annotation. Unlike annotations[ref] this provides a complete annotation heritage, pulling annotations of the same reference name from ancestor classes and modules.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/anise/annotation.rb', line 85

def annotation(ref=nil)
  return(@annotations ||= {}) if ref.nil?

  ref = ref.to_sym
  ann = {}
  ancestors.reverse_each do |anc|
    next unless anc < Annotation
    if h = anc.annotations[ref]
      ann.merge!(h)
    end
  end
  return ann
end

#annotation_added(name) ⇒ Object

Callback method. This method is called for each new annotation.



165
166
167
# File 'lib/anise/annotation.rb', line 165

def annotation_added(name)
  super if defined?(super)
end