Class: Spider::Model::Element
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#definer_model ⇒ Object
Returns the value of attribute definer_model.
-
#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.
12 13 14 15 16 |
# File 'lib/spiderfw/model/element.rb', line 12 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 |
#definer_model ⇒ Object
Returns the value of attribute definer_model.
10 11 12 |
# File 'lib/spiderfw/model/element.rb', line 10 def definer_model @definer_model 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.
143 144 145 |
# File 'lib/spiderfw/model/element.rb', line 143 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.
38 39 40 |
# File 'lib/spiderfw/model/element.rb', line 38 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.
138 139 140 |
# File 'lib/spiderfw/model/element.rb', line 138 def autogenerated? return (self.attributes[:auto] || self.attributes[:autoincrement]) end |
#clone ⇒ Object
203 204 205 |
# File 'lib/spiderfw/model/element.rb', line 203 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.
179 180 181 182 183 |
# File 'lib/spiderfw/model/element.rb', line 179 def condition cond = attributes[:condition] cond = Condition.new(cond) if (cond && !cond.is_a?(Condition)) return cond end |
#embedded? ⇒ Boolean
133 134 135 |
# File 'lib/spiderfw/model/element.rb', line 133 def self.attributes[:embedded] end |
#extended? ⇒ Boolean
True if the element type has been extended by passing a block to Model::BaseModel.element
119 120 121 |
# File 'lib/spiderfw/model/element.rb', line 119 def extended? self.attributes[:extended] end |
#has_single_reverse? ⇒ Boolean
True if the element has a reverse, and that reverse is not multiple
109 110 111 |
# File 'lib/spiderfw/model/element.rb', line 109 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.
124 125 126 |
# File 'lib/spiderfw/model/element.rb', line 124 def hidden? self.attributes[:hidden] end |
#inline? ⇒ Boolean
True if the element model is an InlineModel
114 115 116 |
# File 'lib/spiderfw/model/element.rb', line 114 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
77 78 79 80 |
# File 'lib/spiderfw/model/element.rb', line 77 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?.
83 84 85 |
# File 'lib/spiderfw/model/element.rb', line 83 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?.
89 90 91 |
# File 'lib/spiderfw/model/element.rb', line 89 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).
31 32 33 |
# File 'lib/spiderfw/model/element.rb', line 31 def junction? self.attributes[:junction] end |
#label ⇒ Object
Label. Will use the :label attribute, or return the name split by ‘_’ with each word capitalized.
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/spiderfw/model/element.rb', line 148 def label prev_text_domain = nil if @definer_model && @definer_model != Spider::Model::Managed prev_text_domain = FastGettext.text_domain FastGettext.text_domain = @definer_model.app.short_name if FastGettext.translation_repositories.key?(@definer_model.app.short_name) end l = self.attributes[:label] ? _(self.attributes[:label]) : Inflector.underscore_to_upcasefirst(@name.to_s) if prev_text_domain FastGettext.text_domain = prev_text_domain end l end |
#lazy ⇒ Object
Lazy attribute. (See #lazy_groups).
186 187 188 |
# File 'lib/spiderfw/model/element.rb', line 186 def lazy attributes[:lazy] end |
#lazy? ⇒ Boolean
True if lazy attribute is set. (See #lazy_groups).
191 192 193 |
# File 'lib/spiderfw/model/element.rb', line 191 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.
198 199 200 201 |
# File 'lib/spiderfw/model/element.rb', line 198 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.
172 173 174 175 |
# File 'lib/spiderfw/model/element.rb', line 172 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.
25 26 27 28 |
# File 'lib/spiderfw/model/element.rb', line 25 def model return nil unless model? return association_type || type end |
#model? ⇒ Boolean
True if the element defines an association to another model.
58 59 60 61 |
# File 'lib/spiderfw/model/element.rb', line 58 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.
43 44 45 |
# File 'lib/spiderfw/model/element.rb', line 43 def multiple? self.attributes[:multiple] end |
#owned? ⇒ Boolean
True if only the defining BaseModel holds references to the associated
129 130 131 |
# File 'lib/spiderfw/model/element.rb', line 129 def owned? self.attributes[:owned] end |
#primary_key? ⇒ Boolean
True if the element is a primary key.
94 95 96 |
# File 'lib/spiderfw/model/element.rb', line 94 def primary_key? @primary_key ||= self.attributes[:primary_key] end |
#read_only? ⇒ Boolean
True if the element is read only.
99 100 101 |
# File 'lib/spiderfw/model/element.rb', line 99 def read_only? self.attributes[:read_only] end |
#required? ⇒ Boolean
True if the element must have a value.
48 49 50 |
# File 'lib/spiderfw/model/element.rb', line 48 def required? self.attributes[:required] end |
#reverse ⇒ Object
The reverse element in the relationship to another model.
104 105 106 |
# File 'lib/spiderfw/model/element.rb', line 104 def reverse self.attributes[:reverse] end |
#storage ⇒ Object
Storage for the element’s #model.
166 167 168 169 |
# File 'lib/spiderfw/model/element.rb', line 166 def storage return nil unless model? return self.mapper.storage end |
#to_s ⇒ Object
161 162 163 |
# File 'lib/spiderfw/model/element.rb', line 161 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.
20 21 22 |
# File 'lib/spiderfw/model/element.rb', line 20 def type @type end |
#unique? ⇒ Boolean
True if no two model instances can have the same value for the element.
53 54 55 |
# File 'lib/spiderfw/model/element.rb', line 53 def unique? self.attributes[:unique] end |