Class: Rod::Property::PluralAssociation

Inherits:
Base
  • Object
show all
Defined in:
lib/rod/property/plural_association.rb

Overview

This class defines the has_many (plural association) property. A has_many property has to define its name.

Instance Attribute Summary

Attributes inherited from Base

#name, #options

Instance Method Summary collapse

Methods inherited from Base

#copy, #define_finders, #difference, #has_index?, #index, #reset_index

Constructor Details

#initialize(klass, name, options = {}) ⇒ PluralAssociation

Creates new plural association associated with klass with given name and options.



10
11
12
# File 'lib/rod/property/plural_association.rb', line 10

def initialize(klass,name,options={})
  super(klass,name,options)
end

Instance Method Details

#association?Boolean

Predicate indicating that this property is an association.

Returns:

  • (Boolean)


20
21
22
# File 'lib/rod/property/plural_association.rb', line 20

def association?
  true
end

#define_c_accessors(builder) ⇒ Object

Defines the accessor of the association’s constituents (C struct field/fields that hold the association data).



52
53
54
55
56
57
# File 'lib/rod/property/plural_association.rb', line 52

def define_c_accessors(builder)
  field_reader("#{@name}_count",@klass.struct_name,c_type(:ulong),builder)
  field_reader("#{@name}_offset",@klass.struct_name,c_type(:ulong),builder)
  field_writer("#{@name}_count",@klass.struct_name,c_type(:ulong),builder)
  field_writer("#{@name}_offset",@klass.struct_name,c_type(:ulong),builder)
end

#define_getterObject

Defines the getter of the Ruby class which corresponds to this association.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rod/property/plural_association.rb', line 76

def define_getter
  # optimization
  name = @name.to_s
  class_name =
    if options[:class_name]
      options[:class_name]
    else
      "#{@klass.scope_name}::#{::English::Inflect.singular(name).camelcase}"
    end
  klass = options[:polymorphic] ? nil : class_name.constantize
  database = @klass.database
  @klass.send(:define_method,"#{name}") do
    proxy = instance_variable_get("@#{name}")
    if proxy.nil?
      if self.new?
        count = 0
        offset = 0
      else
        count = self.send("_#{name}_count",@rod_id)
        offset = self.send("_#{name}_offset",@rod_id)
      end
      proxy = CollectionProxy.new(count,database,offset,klass)
      instance_variable_set("@#{name}", proxy)
    end
    proxy
  end
  # count getter
  @klass.send(:define_method,"#{name}_count") do
    if (instance_variable_get("@#{name}") != nil)
      return instance_variable_get("@#{name}").count
    else
      if self.new?
        return 0
      else
        return send("_#{name}_count",@rod_id)
      end
    end
  end
end

#define_setterObject

Defines the settor of the Ruby class which corresponds to this association.



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rod/property/plural_association.rb', line 117

def define_setter
  # optimization
  name = @name.to_s
  @klass.send(:define_method,"#{name}=") do |value|
    proxy = send(name)
    proxy.clear
    value.each do |object|
      proxy << object
    end
    proxy
  end
end

#field?Boolean

Predicate indicating that this property is a field.

Returns:

  • (Boolean)


15
16
17
# File 'lib/rod/property/plural_association.rb', line 15

def field?
  false
end

#layoutObject

Returns the memory layout of the C struct fields that correspond to this association.



132
133
134
135
# File 'lib/rod/property/plural_association.rb', line 132

def layout
  "#{@name}[offset:#{sizeof(:ulong)}+" +
    "count:#{sizeof(:ulong)}]"
end

#metadataObject

Returns the metadata of the association in form of a hash.



40
41
42
# File 'lib/rod/property/plural_association.rb', line 40

def 
  @options.dup
end

#plural?Boolean

Predicate indicating that this property is a plural association.

Returns:

  • (Boolean)


30
31
32
# File 'lib/rod/property/plural_association.rb', line 30

def plural?
  true
end

#polymorphic?Boolean

Predicate indicating that this property is polymorphic.

Returns:

  • (Boolean)


35
36
37
# File 'lib/rod/property/plural_association.rb', line 35

def polymorphic?
  @options[:polymorphic]
end

#seal_c_accessorsObject

Make the C accessors private.



60
61
62
63
64
65
# File 'lib/rod/property/plural_association.rb', line 60

def seal_c_accessors
  @klass.private "_#{@name}_count"
  @klass.private "_#{@name}_count="
  @klass.private "_#{@name}_offset"
  @klass.private "_#{@name}_offset="
end

#singular?Boolean

Predicate indicating that this property is not a singular association.

Returns:

  • (Boolean)


25
26
27
# File 'lib/rod/property/plural_association.rb', line 25

def singular?
  false
end

#to_c_structObject

Converts the association to fields in a C struct.



45
46
47
48
# File 'lib/rod/property/plural_association.rb', line 45

def to_c_struct
  "  #{c_type(:ulong)} #{@name}_offset;\n" +
    "  #{c_type(:ulong)} #{@name}_count;\n"
end