Module: Locomotive::AstHelpers::Annotations

Included in:
RelationalAlgebra::RelAlgAstNode
Defined in:
lib/locomotive/tree_helpers/annotations.rb

Overview

The purpose of this module is to enhance an instance with accessors on special labeled methods.

Each instance of a class (with this module included) has methods

*. o.ann_\w+
*. o.ann_\w+=

to set and read the annotations.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args) ⇒ Object

set method_missing to overwrite the behaviour of ann_-prefixed methods



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/locomotive/tree_helpers/annotations.rb', line 34

def method_missing(key, *args)
  key_str = key.to_s

  # labeled ANN_PATTERN is not applicable in
  # this case due to ?<data>
  if /^ann_(?<data>\w+)=?/ =~ key_str
    @annotations ||= {}

    data_sym = "ann_#{data}".to_sym
    if key_str[-1,1] == "=" then
      @annotations[data_sym] = args[0]
    else
      @annotations[data_sym]
    end

  else 
    super.method_missing(key, *args)
  end
end

Instance Method Details

#respond_to?(sym) ⇒ Boolean

overwrite the respond_to method to pretend there are ann_-methods

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
# File 'lib/locomotive/tree_helpers/annotations.rb', line 20

def respond_to? sym
  @annotations ||= {}
  if @annotations.keys.member?( sym.to_s[-1,1] != "=" ? 
                                  sym :
                                  sym.to_s[0..-2].to_sym )
    true
  else
    super.respond_to? sym
  end
end