Class: Spider::Model::Element
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#association ⇒ Object
Named association.
-
#association_type ⇒ Object
The model used for the association.
-
#autogenerated? ⇒ Boolean
True if the element is generated on save by the model or the mapper.
- #clone ⇒ Object
-
#condition ⇒ Object
The element’s Condition, if any.
- #embedded? ⇒ Boolean
-
#extended? ⇒ Boolean
True if the element type has been extended by passing a block to Model::BaseModel.element.
-
#has_single_reverse? ⇒ Boolean
True if the element has a reverse, and that reverse is not multiple.
-
#hidden? ⇒ Boolean
True if the :hidden attribute is set.
-
#initialize(name, type, attributes = {}) ⇒ Element
constructor
A new instance of Element.
-
#inline? ⇒ Boolean
True if the element model is an InlineModel.
-
#integrated? ⇒ Boolean
True if the element is integrated from another one.
-
#integrated_from ⇒ Object
If the element is integrated, the element from which it is taken.
-
#integrated_from_element ⇒ Object
If the element is integrated, the element corresponding to this in the model corresponding to the #integrated_from element.
-
#junction? ⇒ Boolean
True if the element model is a junction (for a many to many relationship).
-
#label ⇒ Object
Label.
-
#lazy ⇒ Object
Lazy attribute.
-
#lazy? ⇒ Boolean
True if lazy attribute is set.
-
#lazy_groups ⇒ Object
Returns the lazy groups this elements is in, as set by BaseModel#element with the :lazy attributes.
-
#mapper ⇒ Object
Mapper for the element’s #model.
-
#model ⇒ Object
The actual model used to represent the association.
-
#model? ⇒ Boolean
True if the element defines an association to another model.
-
#multiple? ⇒ Boolean
True if the element defines a 1|n -> n association.
-
#owned? ⇒ Boolean
True if only the defining BaseModel holds references to the associated.
-
#primary_key? ⇒ Boolean
True if the element is a primary key.
-
#read_only? ⇒ Boolean
True if the element is read only.
-
#required? ⇒ Boolean
True if the element must have a value.
-
#reverse ⇒ Object
The reverse element in the relationship to another model.
-
#storage ⇒ Object
Storage for the element’s #model.
- #to_s ⇒ Object
-
#type ⇒ Object
The element type, as per the second argument passed to the Model::BaseModel.element method.
-
#unique? ⇒ Boolean
True if no two model instances can have the same value for the element.
Constructor Details
#initialize(name, type, attributes = {}) ⇒ Element
Returns a new instance of Element.
11 12 13 14 15 |
# File 'lib/spiderfw/model/element.rb', line 11 def initialize(name, type, attributes={}) @name = name @type = type @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
9 10 11 |
# File 'lib/spiderfw/model/element.rb', line 9 def attributes @attributes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/spiderfw/model/element.rb', line 8 def name @name end |
Instance Method Details
#association ⇒ Object
Named association.
142 143 144 |
# File 'lib/spiderfw/model/element.rb', line 142 def association self.attributes[:association] end |
#association_type ⇒ Object
The model used for the association. The BaseModel will automatically create a junction model for many to many relationships, unless one is supplied with the :through attribute. This will be set for n <-> n relationships, and will be nil otherwise.
37 38 39 |
# File 'lib/spiderfw/model/element.rb', line 37 def association_type @association_type ||= self.attributes[:association_type] end |
#autogenerated? ⇒ Boolean
True if the element is generated on save by the model or the mapper.
137 138 139 |
# File 'lib/spiderfw/model/element.rb', line 137 def autogenerated? return (self.attributes[:auto] || self.attributes[:autoincrement]) end |
#clone ⇒ Object
193 194 195 |
# File 'lib/spiderfw/model/element.rb', line 193 def clone self.class.new(@name, @type, self.attributes.clone) end |
#condition ⇒ Object
The element’s Condition, if any. If a condition is set with the :condition attribute, the association to the element’s model will be filtered by it.
169 170 171 172 173 |
# File 'lib/spiderfw/model/element.rb', line 169 def condition cond = attributes[:condition] cond = Condition.new(cond) if (cond && !cond.is_a?(Condition)) return cond end |
#embedded? ⇒ Boolean
132 133 134 |
# File 'lib/spiderfw/model/element.rb', line 132 def self.attributes[:embedded] end |
#extended? ⇒ Boolean
True if the element type has been extended by passing a block to Model::BaseModel.element
118 119 120 |
# File 'lib/spiderfw/model/element.rb', line 118 def extended? self.attributes[:extended] end |
#has_single_reverse? ⇒ Boolean
True if the element has a reverse, and that reverse is not multiple
108 109 110 |
# File 'lib/spiderfw/model/element.rb', line 108 def has_single_reverse? return true if self.attributes[:reverse] && !model.elements[self.attributes[:reverse]].multiple? end |
#hidden? ⇒ Boolean
True if the :hidden attribute is set.
123 124 125 |
# File 'lib/spiderfw/model/element.rb', line 123 def hidden? self.attributes[:hidden] end |
#inline? ⇒ Boolean
True if the element model is an InlineModel
113 114 115 |
# File 'lib/spiderfw/model/element.rb', line 113 def inline? self.attributes[:inline] end |
#integrated? ⇒ Boolean
True if the element is integrated from another one. (See also BaseModel#integrate). Example:
class Address < BaseModel
element :street, String
element :area_code, String
end
class Person < BaseModel
element :name, String
element :address, Address
integrate :address
end
Person.elements[:street].integrated? => true
Person.elements[:street].integrated_from => :address
Person.elements[:street].integrated_from_element => :street
76 77 78 79 |
# File 'lib/spiderfw/model/element.rb', line 76 def integrated? return @is_integrated if @is_integrated != nil @is_integrated = self.attributes[:integrated_from] end |
#integrated_from ⇒ Object
If the element is integrated, the element from which it is taken. See also #integrated?.
82 83 84 |
# File 'lib/spiderfw/model/element.rb', line 82 def integrated_from self.attributes[:integrated_from] end |
#integrated_from_element ⇒ Object
If the element is integrated, the element corresponding to this in the model corresponding to the #integrated_from element. See also #integrated?.
88 89 90 |
# File 'lib/spiderfw/model/element.rb', line 88 def integrated_from_element self.attributes[:integrated_from_element] end |
#junction? ⇒ Boolean
True if the element model is a junction (for a many to many relationship).
30 31 32 |
# File 'lib/spiderfw/model/element.rb', line 30 def junction? self.attributes[:junction] end |
#label ⇒ Object
Label. Will use the :label attribute, or return the name split by ‘_’ with each word capitalized.
147 148 149 |
# File 'lib/spiderfw/model/element.rb', line 147 def label return self.attributes[:label] ? _(self.attributes[:label]) : Inflector.underscore_to_upcasefirst(@name.to_s) end |
#lazy ⇒ Object
Lazy attribute. (See #lazy_groups).
176 177 178 |
# File 'lib/spiderfw/model/element.rb', line 176 def lazy attributes[:lazy] end |
#lazy? ⇒ Boolean
True if lazy attribute is set. (See #lazy_groups).
181 182 183 |
# File 'lib/spiderfw/model/element.rb', line 181 def lazy? attributes[:lazy] end |
#lazy_groups ⇒ Object
Returns the lazy groups this elements is in, as set by BaseModel#element with the :lazy attributes. Lazy groups are used by the mapper to determine which elements to autoload: when an element in a lazy group is accessed, all the elements in the same group(s) will be loaded.
188 189 190 191 |
# File 'lib/spiderfw/model/element.rb', line 188 def lazy_groups return nil unless attributes[:lazy] && attributes[:lazy] != true return attributes[:lazy].is_a?(Array) ? attributes[:lazy] : [attributes[:lazy]] end |
#mapper ⇒ Object
Mapper for the element’s #model.
162 163 164 165 |
# File 'lib/spiderfw/model/element.rb', line 162 def mapper return nil unless model? return self.model.mapper end |
#model ⇒ Object
The actual model used to represent the association. Will return the #association_type or the #type.
24 25 26 27 |
# File 'lib/spiderfw/model/element.rb', line 24 def model return nil unless model? return association_type || type end |
#model? ⇒ Boolean
True if the element defines an association to another model.
57 58 59 60 |
# File 'lib/spiderfw/model/element.rb', line 57 def model? return @is_model if @is_model != nil @is_model = (type < Spider::Model::BaseModel || association_type) end |
#multiple? ⇒ Boolean
True if the element defines a 1|n -> n association.
42 43 44 |
# File 'lib/spiderfw/model/element.rb', line 42 def multiple? self.attributes[:multiple] end |
#owned? ⇒ Boolean
True if only the defining BaseModel holds references to the associated
128 129 130 |
# File 'lib/spiderfw/model/element.rb', line 128 def owned? self.attributes[:owned] end |
#primary_key? ⇒ Boolean
True if the element is a primary key.
93 94 95 |
# File 'lib/spiderfw/model/element.rb', line 93 def primary_key? @primary_key ||= self.attributes[:primary_key] end |
#read_only? ⇒ Boolean
True if the element is read only.
98 99 100 |
# File 'lib/spiderfw/model/element.rb', line 98 def read_only? self.attributes[:read_only] end |
#required? ⇒ Boolean
True if the element must have a value.
47 48 49 |
# File 'lib/spiderfw/model/element.rb', line 47 def required? self.attributes[:required] end |
#reverse ⇒ Object
The reverse element in the relationship to another model.
103 104 105 |
# File 'lib/spiderfw/model/element.rb', line 103 def reverse self.attributes[:reverse] end |
#storage ⇒ Object
Storage for the element’s #model.
156 157 158 159 |
# File 'lib/spiderfw/model/element.rb', line 156 def storage return nil unless model? return self.mapper.storage end |
#to_s ⇒ Object
151 152 153 |
# File 'lib/spiderfw/model/element.rb', line 151 def to_s return "Element '#{@name.to_s}'" end |
#type ⇒ Object
The element type, as per the second argument passed to the Model::BaseModel.element method. This may be different from #model.
19 20 21 |
# File 'lib/spiderfw/model/element.rb', line 19 def type @type end |
#unique? ⇒ Boolean
True if no two model instances can have the same value for the element.
52 53 54 |
# File 'lib/spiderfw/model/element.rb', line 52 def unique? self.attributes[:unique] end |