Module: Typekit::Element::Association::ClassMethods

Defined in:
lib/typekit/element/association.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/typekit/element/association.rb', line 51

def belongs_to(name)
  owners << name

  define_method(name) do
    value = attributes[name]
    return value if value.nil? || value.is_a?(Element::Base)
    attributes[name] = Element.build(name, self, attributes[name])
  end
end

#has_many(name) ⇒ Object



17
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
45
46
47
48
49
# File 'lib/typekit/element/association.rb', line 17

def has_many(name)
  possessions << name

  define_method(name) do
    value = attributes[name]
    return value if value.is_a?(Collection::Base)

    unless value.nil?
      attributes[name] = Collection.build(name, self, value)
      return attributes[name]
    end

    if feature?(:persistence) && new?
      attributes[name] = Collection.build(name, self, [])
      return attributes[name]
    end

    load!

    value = attributes[name]
    raise Error, 'Cannot load the association' if value.nil?

    attributes[name] = Collection.build(name, self, value)
  end

  define_method("#{name}=") do |value|
    if value.is_a?(Collection::Base)
      attributes[name] = value
    else
      attributes[name] = Collection.build(name, self, value)
    end
  end
end

#ownersObject



13
14
15
# File 'lib/typekit/element/association.rb', line 13

def owners
  @owners ||= []
end

#possessionsObject



9
10
11
# File 'lib/typekit/element/association.rb', line 9

def possessions
  @possessions ||= []
end