Class: Rod::Property::PluralAssociation
- 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
Instance Method Summary collapse
-
#association? ⇒ Boolean
Predicate indicating that this property is an association.
-
#define_c_accessors(builder) ⇒ Object
Defines the accessor of the association’s constituents (C struct field/fields that hold the association data).
-
#define_getter ⇒ Object
Defines the getter of the Ruby class which corresponds to this association.
-
#define_setter ⇒ Object
Defines the settor of the Ruby class which corresponds to this association.
-
#field? ⇒ Boolean
Predicate indicating that this property is a field.
-
#initialize(klass, name, options = {}) ⇒ PluralAssociation
constructor
Creates new plural association associated with
klass
with givenname
andoptions
. -
#layout ⇒ Object
Returns the memory layout of the C struct fields that correspond to this association.
-
#metadata ⇒ Object
Returns the metadata of the association in form of a hash.
-
#plural? ⇒ Boolean
Predicate indicating that this property is a plural association.
-
#polymorphic? ⇒ Boolean
Predicate indicating that this property is polymorphic.
-
#seal_c_accessors ⇒ Object
Make the C accessors private.
-
#singular? ⇒ Boolean
Predicate indicating that this property is not a singular association.
-
#to_c_struct ⇒ Object
Converts the association to fields in a C struct.
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,={}) super(klass,name,) end |
Instance Method Details
#association? ⇒ Boolean
Predicate indicating that this property is an association.
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_getter ⇒ Object
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 [:class_name] [:class_name] else "#{@klass.scope_name}::#{::English::Inflect.singular(name).camelcase}" end klass = [: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_setter ⇒ Object
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.
15 16 17 |
# File 'lib/rod/property/plural_association.rb', line 15 def field? false end |
#layout ⇒ Object
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 |
#metadata ⇒ Object
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.
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.
35 36 37 |
# File 'lib/rod/property/plural_association.rb', line 35 def polymorphic? @options[:polymorphic] end |
#seal_c_accessors ⇒ Object
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.
25 26 27 |
# File 'lib/rod/property/plural_association.rb', line 25 def singular? false end |
#to_c_struct ⇒ Object
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 |