Module: Ruckus::StructureRelateDeclaration

Defined in:
lib/ruckus/structure/relate.rb

Instance Method Summary collapse

Instance Method Details

#class_method_missing_hook(meth, *args) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/ruckus/structure/relate.rb', line 3

def class_method_missing_hook(meth, *args)
    if meth.to_s =~ /^relate_(.*)/
        relate($1.intern, *args)
        return false
    end
    true
end

#relate(attr, field, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruckus/structure/relate.rb', line 11

def relate(attr, field, opts={})
    opts[:through] ||= :value
    raise "need a valid field to relate" if not field
    raise "need :to argument" if not opts[:to]

    self.initializers << lambda do
        f = send(field)

        case attr
        when :value
            f.value = opts[:through]
            f.instance_eval { @from_field = opts[:to] }
        when :size
            f.instance_eval {
                @in_size = {
                    :meth => opts[:through],
                    :from_field => opts[:to]
                }
            }
        end
    end
end