Class: Module

Inherits:
Object show all
Defined in:
lib/omega/common.rb

Instance Method Summary collapse

Instance Method Details

#foreign_reference(*symbols) ⇒ Object

Define a foreign reference, this will:

  • define the normal attribute accessor

  • define an attribute accessor for the id attribute

  • define attribute writer so that id attribute is set

    when setting attribute
    
  • define id attribute writer so that attribute is set

    to nil if changing id
    

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/omega/common.rb', line 108

def foreign_reference(*symbols)
  symbols.each { |symbol|
    id_symbol      = "#{symbol}_id".intern
    symbol_attr    = "@#{symbol}".intern
    id_symbol_attr = "@#{id_symbol}".intern
    writer_symbol = "#{symbol}=".intern
    id_writer_symbol = "#{id_symbol}=".intern
    attr_accessor symbol
    attr_accessor id_symbol
    define_method(writer_symbol) { |val|
      instance_variable_set symbol_attr, val
      instance_variable_set id_symbol_attr, (val.nil? ? nil : val.id)
      nil
    }
    define_method(id_writer_symbol) { |val|
      change = val != instance_variable_get(id_symbol_attr)
      instance_variable_set(id_symbol_attr, val)
      instance_variable_set(symbol_attr, nil) if change
      nil
    }
  }
  nil
end

#module_classesObject


96
97
98
99
# File 'lib/omega/common.rb', line 96

def module_classes
  self.constants.collect { |c| self.const_get(c)}.
                 select  { |c| c.kind_of?(Class) }
end

#parentObject

See comment in parent_name above


141
142
143
# File 'lib/omega/common.rb', line 141

def parent
  parent_name ? parent_name.constantize : Object
end

#parent_nameObject

The following two were taken from ActiveSupport/CoreExtensions/Module


133
134
135
136
137
138
# File 'lib/omega/common.rb', line 133

def parent_name
  unless defined? @parent_name
    @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
  end
  @parent_name
end

#subclassesObject


90
91
92
93
94
# File 'lib/omega/common.rb', line 90

def subclasses
  ObjectSpace.to_enum(:each_object, Module).select do |m|
    m.ancestors.include?(self)
  end
end